J.E.D.I.
Figure 10.1: ClientServer model
10.1.5 Sockets
The last general networking concept well be looking at before plunging into Java networking is regarding sockets. Most Java network programming uses a particular type
of network communication known as sockets.
A socket is a software abstraction for an input or output medium of communication. It is through the use of sockets that Java performs all of its low-level network
communication. These are communication channels that enable you to transfer data through a particular port. In short, a socket refers to an endpoint for communication
between two machines.
10.2 The Java Networking Package
The java.net package provides classes useful for developing networking applications. For a complete list of network classes and interfaces, please refer to the Java API
documentation. Well just focus on these four classes: ServerSocket, Socket, MulticastSocket, and DatagramPacket classes.
10.2.1 The ServerSocket and the Socket Class
The ServerSocket Class provides the basic functionalities of a server. The following table describes two of the four constructors of the ServerSocket class:
Introduction to Programming II Page 141
J.E.D.I.
ServerSocket Constructors
ServerSocketint port Instantiates a server that is bound to the specified port. A port of 0 assigns the server
to any free port. Maximum queue length for incoming connection is set to 50 by default. ServerSocketint port, int backlog
Instantiates a server that is bound to the specified port. Maximum queue length for incoming connection is is based on the backlog parameter.
Table 34: ServerSocket constructors
Here now are some of the classs methods:
ServerSocket Methods
public Socket accept Causes the server to wait and listen for client connections, then accept them.
public void close Closes the server socket. Clients can no longer connect to the server unless it is opened
again. public int getLocalPort
Returns the port on which the socket is bound to. public boolean isClosed
Indicates whether the socket is closed or not.
Table 35: ServerSocket methods
The succeeding example is an implementation of a simple server, which simply echoes the information sent by the client.
import java.net.; import java.io.;
public class EchoingServer { public static void mainString [] args {
ServerSocket server = null; Socket client;
try { server = new ServerSocket1234;
1234 is an unused port number } catch IOException ie {
System.out.printlnCannot open socket.; System.exit1;
}
whiletrue { try {
client = server.accept; OutputStream clientOut = client.getOutputStream;
PrintWriter pw = new PrintWriterclientOut, true; InputStream clientIn = client.getInputStream;
BufferedReader br = new BufferedReadernew
Introduction to Programming II Page 142
J.E.D.I.
InputStreamReaderclientIn; pw.printlnbr.readLine;
} catch IOException ie { }
} }
}
While the ServerSocket class implements server sockets, the Socket class implements a client socket. The Socket class has eight constructors, two of which are already
deprecated. Let us have a quick look at two of these constructors.
Socket Constructors
SocketString host, int port Creates a client socket that connects to the given port number on the specified host.
SocketInetAddress address, int port Creates a client socket that connects to the given port number at the specified IP
address.
Table 36: Socket constructors
Here now are some of the classs methods:
Socket Methods
public void close Closes the client socket.
public InputStream getInputStream Retrieves the input stream associated with this socket.
public OutputStream getOutputStream Retrieves the output stream associated with this socket.
public InetAddress getInetAddress Returns the IP address to which this socket is connected
public int getPort Returns the remote port to which this socket is connected.
public boolean isClosed Indicates whether the socket is closed or not.
Table 37: Socket methods
The succeeding example is an implementation of a simple client, which simply sends data to the server.
import java.io.; import java.net.;
public class MyClient { public static void mainString args[] {
try { Socket client = new Socket133.0.0.1, 1234;
Introduction to Programming II Page 143
J.E.D.I.
Socket client = new SocketInetAddress.getLocalHost, 1234;
InputStream clientIn = client.getInputStream; OutputStream clientOut = client.getOutputStream;
PrintWriter pw = new PrintWriterclientOut, true; BufferedReader br = new BufferedReadernew
InputStreamReaderclientIn; BufferedReader stdIn = new BufferedReadernew
InputStreamReaderSystem.in; System.out.printlnType a message for the server: ;
pw.printlnstdIn.readLine; System.out.printlnServer message: ;
System.out.printlnbr.readLine; pw.close;
br.close; client.close;
} catch ConnectException ce { System.out.printlnCannot connect to the server.;
} catch IOException ie { System.out.printlnIO Error.;
} }
}
Running the EchoingServer makes it ready for accepting message from the client. Once a client, such as MyClient, sends a message to the server, the server echoes the message
back to the client. Heres a sample run of MyClient once EchoingServer has been started:
Type a message for the server: first message to server
Server message: first message to server
Introduction to Programming II Page 144
J.E.D.I.
10.2.2 The MulticastSocket and the DatagramPacket Class