You are on page 1of 14

Java Course

Module 11 Core Java API: Networking

Module Objectives
At the end of this module, participants will be
able to:
Define the basic concepts about TCP/IP
networking.
Establish a network connection.
Send data across the network.

TCP/IP and UDP Networking


Applications that communicate over the Internet use the TCP
protocol or the UDP protocol.
TCP is a protocol that is best applicable for connections that
require reliability and guaranteed sequence of delivery.
UDP is a connectionless protocol. It does not care about the
sequence that the data is sent, or even if it was received at all.

TCP/IP and UDP Networking (cont.)


Client App
Server IP: 192.168.0.1
9999

Client App

3333
80
Client App
Server IP: 192.168.0.2
80

Client App

9999
Client App

Individual nodes in a
network are identified by
an IP address.
Network connections
created by applications
running on a single
computer on the network
can be further identified by
an assigned port number.
The data is transferred
through streams.

Establishing a Connection
For TCP connections, the URL, URLConnection, Socket, and
ServerSocket classes are used.
For UDP connections, the DatagramPacket, DatagramSocket,
and MulticastSocket are used.

Using URLs
A URL is a Uniform Resource Locator and is used as a
reference to any resource on the Internet.
A URL is composed of a protocol and a resource identifier.
The protocol refers to a set of rules governing the format of
messages that are exchanged between computers.
The resource identifier indicates where the resource is to be
found.
Protocol

Resource Name

URL

http

www.accenture.com

http://www.accenture.com

Using URLs (cont.)


Create an instance representing a specific URL using the URL
and URLConnection classes.
URL accentureURL = new URL ("http://www.accenture.com");
URLConnection connection = accentureURL.openConnection();

Open a stream in order to obtain that resource from the


specified URL.
BufferedReader in = new BufferedReader(new InputStreamReader(accentureURL.openStream()));

Using URLs (cont.)


To send data to a URL resource, set the connection object to be
able to send data and obtain an output stream instead.
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());

After reading/writing to the URL, close the stream.


in.close();
out.close();

** Refer to the URLSample.java sample code

Using Sockets
Sockets represent the end points between two applications in a
network.
When a socket is created it is bound to an IP address and a port
number.
One end point acts as the server that waits for connections
(represented by ServerSocket class).
One or more end points acts as a client and that seeks to
connect to a server (represented by Socket class).
** Refer to the ServerSample.java and ClientSample.java sample code

The InetAddress Class


The InetAddress class is the representation of an IP address
that is used by the TCP and UDP protocols.
Instances of this class are returned by various factory methods:
InetAddress.getLocalHost() returns the InetAddress of the local
machine.
InetAddress.getByAddress() returns an InetAddress based on the
host name given.
InetAddress.getByAddress(byte[]) returns an InetAddress based
on the IP address passed as an array of bytes.
** Refer to the InetAddressSample.java sample code

10

Creating a ServerSocket
Instantiate a ServerSocket instance and bind it to a specific port
number.
ServerSocket server = new ServerSocket(9999);

Wait for a connection from a client.


client = server.accept();

Use the returned Socket object to obtain an output or input


stream to the client.
out = new PrintWriter(client.getOutputStream(), true);

Close any open streams, Sockets, and ServerSockets when


finished.

11

Creating a Client Socket


Instantiate an instance of the Socket class and pass the network
address and the port of the server the application.
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);

Retrieve input or output streams bound to the socket.


BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

Close all the streams and sockets when finished.

12

Questions and Comments

13

Activity
1)

2)

3)

Create a simple chat server that will allow


multiple telnet clients to connect to your
server and chat.
Text entered through the telnet client
should be broadcast to all other clients
connected to the server.
To open a telnet client just type telnet
<host> <port> in the console Substitute
the host and part values with whatever
you are using.

Hint: A common algorithm for the server is


1) Create a ServerSocket.
2) Wait for connections.
3) For every client that connects, create a
new thread to read input from that client.
4) Go back to step 2.
14

You might also like