You are on page 1of 5

Java Networking

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
Hands on Lab: Java Networking

Topic Details
Overview In this Hands on Lab, you will learn Java Networking
Technology using hands-on classroom activities.
Key Concepts • Socket Programming
• Working with a URL
Difficulty Advanced
Duration Approximately 45 minutes
Notes Access to a Java Integrated Development Environment
such as Netbeans or Eclipse with Java Development Kit
1.8 (JDK1.8) installed is required to complete this Lab.

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

2
Section 1: Socket Programming

1. Introduction:
• In this Section, you are introduced to networking using Sockets.
• A socket is one endpoint of a two-way communication link between two programs (or processes) running on a
network. A socket is the combination of an IP address and a port number of a device on a network
• When processes communicate over a network, Java technology uses the stream model.
• A socket can hold two streams: one input stream and one output stream.
• A process sends data to another process through the network by writing to the output stream associated with the
socket.
• A process reads data written by another process by reading from the input stream associated with the socket.

2. Setting up the Connection


• To set up the connection, one computer (the server) must be running a program that is waiting for a connection, and
second computer (a client) must try to reach the first.
• Transmission Control Protocol/Internet Protocol, or TCP/IP is used.

3. Java Networking Model


• In the Java programming language, TCP/IP socket connections are implemented with classes in the java.net
package.
• Java provides the ServerSocket class for creating a server socket and Socket class for creating a client socket.

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

3
Set up the TCP/IP Server

TCP/IP Server applications rely on the ServerSocket and Socket networking classes provided by the Java programming language.

The Server receives the data, produces a result, and then sends the result back to the client.

Create a new project in your chosen java IDE, and enter the code shown below. Correct any syntax errors.

Set up the TCP/IP Client:

The Client side of a TCP/IP application relies on the Socket class. The connection between the client and the server is established by
the Socket class

The client attaches to the server and prints everything sent by the server to the console.

The two parameters in the Socket class constructor are for the IP address and port number of the server. In this case 127.0.0.1 is the
server IP address (assumes that the server is running on the same computer as the client), and 9999 is the port used on the server.

If the server is running on a different computer within your network from the client, enter the actual IP address of the server.

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

4
Test the Connection

First start the Server application by running the NetworkServer class. This is set up as an infinite loop, so will continue to output the
“Hello Network World!” message to any clients that connect.

Run the NetworkClient class, and the message from the server should be displayed in the console window.

Stop the server application running by using the stop feature of your IDE.

Section 2: Working with a URL

• URL is an acronym for Uniform Resource Locator. It is a reference to a resource on the Internet.
• Java programs that interact with the Internet also may use URLs to find the resources on the Internet they wish to access.
• Java programs can use a class called URL in the java.net package to represent a URL address.

Step 1: Create a URL

Within your Java programs, you can create a URL object that represents a URL address. The URL object always refers to an absolute
URL but can be constructed from an absolute URL, a relative URL, or from URL components.

Step 2: Reading from a URL


You can read from a URL using the openStream() method.

Step 3: Connecting to a URL

If you want to do more than just read from a URL, you can connect to it by calling openConnection() on the URL. The openConnection()
method returns a URLConnection object that you can use for more general communications with the URL.

Step 4: Reading from and Writing to a URL Connection

How to connect to a URL and get results back.

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

You might also like