Chapter 4 - Network Programming
Chapter 4 - Network Programming
Chapter 4 - Network Programming
their functionality
create servers using server sockets and clients using client
sockets
develop a client/server applications.
2
Java provides:-
Stream-based communications
a process establishes a connection to another
process. While the connection is in place, data
flows between the processes
Connection-based protocol - Uses TCP
Packet-based communications – use UDP
Individual packets transmitted
Client-server relationship
Client requests some action to be performed
Server performs the action and responds to client
Request-response model
Common implementation: Web browsers and Web
servers
3
Java Networking is a concept of connecting two or more computing devices
4
A socket, in network terminology, is an end-point for communication
between two processes on the network.
Java Socket programming is used for communication between the
the Socket class for creating a client socket. Two programs on the
Internet communicate through a server socket and a client socket
using I/O streams.
Networking is tightly integrated in Java. The Java API provides the
clients. The client sends requests to the server, and the server
responds. The client begins by attempting to establish a connection
to the server. The server can accept or deny the connection. Once a
connection is established, the client and the server communicate
through sockets.
The server must be running when a client attempts to connect to the
server. The server waits for a connection request from the client. The
statements needed to create sockets on a server and on a client are
shown in Figure 4.1.
7
To establish a server, you need to create a server socket and attach it to
a port, which is where the server listens for connections. The port
identifies the TCP service on the socket. Port numbers range from 0 to
65536, but port numbers 0 to 1024 are reserved for privileged services.
For instance, the email server runs on port 25, and the Web server
usually runs on port 80. You can choose any port number that is not
currently used by other programs.
The following statement creates a server socket serverSocket:
9
Fig 4.2. connection-oriented communication pattern using sockets
10
After a server socket is created, the server can use the following
The server and client exchange data through I/O streams on top of the socket.
13
To get an input stream and an output stream, use the getInputStream()
15
The server program can use the InetAddress class to obtain the
information about the IP address and host name for the client.
Occasionally, you would like to know who is connecting to the server.
You can use the InetAddress class to find the client’s host name and IP
address. The InetAddress class models an IP address.
You can use the following statement in the server program to get an
InetAddress inetAddress =
socket.getInetAddress();
16
Next, you can display the client’s host name and IP address, as follows:
System.out.println("Client's host name is "
+
inetAddress.getHostName());
System.out.println("Client's IP Address is "
+
inetAddress.getHostAddress());
17
A server can serve multiple clients. The connection to each client is handled
by one thread.
Multiple clients are quite often connected to a single server at the same time.
socket programming.
DatagramSocket class
21
Java DatagramPacket class
22 ExampleSender ExampleReceiver
23