Java SE 8: Getting Started with Socket Programming
Overview
Purpose
This tutorial shows you how to use Java Platform, Standard
Edition 8 (Java SE 8) and NetBeans 8 for socket programming over
TCP/IP networks.
Time to Complete
Approximately 120 minutes
Introduction
In this tutorial, you learn how to write client/server applications
for lower-level network communications. In client/server
applications, the server provides the service and the client uses
that service. Communication takes place over the TCP/IP network,
where a client program and a server program establish a connection
with one another. Each program binds a socket at the end of the
connection. The java.net
package provides two classes: The Socketclass implements
the client side of the connection, and the ServerSocketclass implements
the server side of the connection.
A Java SE 8 project named SingleClientServer
is created in NetBeans. You're now ready to use the EchoServer.java
file to write a server program.
Creating a Server Program
In this section, you write a program named EchoServer to
connect to the server. The EchoServer
example creates a
ServerSocket class instance to connect to the echo
client. It reads input from the client and then responds to the
client that requested the connection. The server echoes the input
back through the socket to the client.
The server accepts the connection from the client, binds a new
socket to the same local port, and sets its remote endpoint to the
client's address and port. It needs a new socket so that it can
continue to listen to the original socket for connection requests
when the attention needs for the connected client.
ServerSocket serverSocket = new
ServerSocket(portNumber);
The portNumber argument
is the logical address through which the application
communicates over the network. It's the port on which the
server is running. You must provide the port number through
which the server can listen to the client. Don't select port
numbers between 0 and 1,023 because they're reserved for
privileged users (that is, super user or root). Add the server
socket inside the try-with-resources
block.
The accept()method
waits until a client starts and requests a connection on the
host and port of this server.
When a connection is requested and successfully established,
the
accept()method returns a new Socket
object. It's bound to the same local port, and its remote
address and remote port are set to match the client's. The
server can communicate with the client over this new object
and listen for client connection requests.
out = new
PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
Communicate with the client.
Receive data from the client: (inputLine =
in.readLine())
Send data to the client: out.println(inputLine);
Close the stream, and then close the socket.
The
EchoServer
example creates a server socket and waits for a client request.
When it receives a client request, the server connects to the
client and responds to it.
Open EchoServer.java
and paste in your NetBeans IDE project file.
Lines 12 to 15: The if
statement checks for the length of the command-line
argument. If the number of arguments isn't equal to two, then
it prints a system error in the console.
Lines 17: The first argument from the command line is
received as the port number.
Lines 18 to 21: The statement in the
try-with-resources block establishes the socket
connection between the client and the server and opens a PrintWriter
and a BufferReader
on the socket. It's advantageous to create the server
socket, the client socket, and the input and output streams
in the statement, because the Java runtime automatically
closes the input and output streams and the server socket.
Lines 24 to 26: The while
loop uses the readline()
method to read one line at a time from the standard
input stream.
Lines 28 to 30: These lines show the catch
block used for handling exceptions. The constructor for ServerSocket
throws IOException
if it can't listen on the specified port (for example, the
port is being used).
On the Projects tab, right-click the SingleClientServer
project, select Set Configuration, and
then select Customize.
You successfully generated a server configuration in NetBeans
IDE.
Creating a Client Program
In this section, you write a program named EchoClientto
connect to the echo server. The EchoClient
example creates a socket to connect to the echo server. It reads
input from the user on the standard input stream, and then forwards
that text to the echo server by writing the text to the socket. The
server echoes the input back through the socket to the client. The
client program reads and displays the data passed from the server.
The EchoClient
example writes to and reads from its socket, sending data to and
receiving data from the echo server.
The client knows the host name of the machine on which the server
is running. It also knows the port number on which the server is
listening. To make a connection request, the client tries to
connect with the server on the server's machine and port. Because
the client also needs to identify itself to the server, it binds
to a local port number that it will use during this connection.
The system typically assigns the port number.
View Image
Here's an overview of how to create the simple client program:
Create and open a client socket.
Open an input stream and an output stream to the socket.
Read from and write to the stream according to the server's
protocol.
Close the streams and then close the socket.
Only step c varies from client to client, depending on the
server. The other steps are the same. In this example, the EchoClient class
connects with one server and handles one client instance.
In the NetBeans IDE, perform the following steps:
Open the provided Client
project.
Expand Source Packages, and then expand
com.example.
On the Projects tab, double-click EchoClient.java.
Socket echoSocket = new
Socket(hostName, portNumber);
The hostName
argument is the machine where you are trying to open a
connection, and portNumber
is the port on which the server is running. Don't
select port numbers between 0 and 1,023 because they're
reserved for privileged users (that is, super user or
root).
Create the input stream and output to the socket for
communicating with the server.
PrintWriter out = new
PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader(echoSocket.getInputStream()));
Read from and write to the stream according to the server's
protocol.
Receive data from the server: (userInput =
stdIn.readLine())
Send data to the server: out.println(userInput);
Close the stream and then close the socket.
Note: The Java runtime closes these resources
automatically when you create them in the try-with-resources
block. If you don't use try-with-resources,
then you need to manually close the streams and the socket.
The EchoClient example
creates a socket, gets a connection to the server, reads input
from the user, and forwards that text to the server by writing
into the socket.
Open EchoClient.java
and paste in your NetBeans IDE project file.
Lines 12 to 15: The if
statement checks for the length of the command-line
argument. If the number of arguments isn't equal to two,
then it prints a system error on the console.
Line 16: The first argument from the command line is
received as the port number.
Line 17: The second argument from the command line is
received as the port number.
Lines 18 to 21: The statement in the
try-with-resources block establishes the socket
connection between the client and the server and opens a PrintWriter
and a BufferReader
on the socket.
Lines 23 to 26: The while
loop uses the readline
method to read one line at a time from the
standard input stream. It reads a line from the BufferedReader
object stdIn,which
is connected to the socket. The readline
method waits until the server echoes the
information back to the echoClient.
When readline
returns, echoClient
prints the information to the standard output.
Lines 27 to 33: Theses lines show the catch
block used for handling exceptions. Here, you are handling UnknownHostException
and
IOException exceptions.
On the Projects tab, right-click the Client
project, select Set
Configuration, and then select Customize.
You successfully generated a client configuration in the NetBeans
IDE.
Running a Single Client/Server Program
You already configured a single client and a single
server. In this section, you run the client/server program. You
start the server program first, and then you start the client
program.
On the Projects tab, right-click the SingleClientServer
project, select Set Configuration, and
then select Server.
The input text is echoed in the client window and displayed in
the console.
Switch to the server window and verify the output. View Image
The server successfully listened to the client program at
local port 8005 and echoed the input message received for the
client. The echo message received from the client is displayed
in the console.
Stop the server when you finish running the client/server
application.
View Image
You successfully configured and executed the single client/server
program.
Extending the Server to Implement Multiple Clients
In this section, you write a server program called Server,which
connects to EchoClient.java.
The Server program
creates a Serversocket,
gets a connection to the echo client by using a thread
instance, reads input from the client, and responds to the client
that requested the connection. To keep the server example simple,
you designed it to listen for and handle a single connection
request.
Multiple client requests can come into the same port and,
consequently, into the same Serversocket.
Because client connection requests are queued at the port,
the server must accept the connections sequentially. However, the
server can service them simultaneously by using threads (one thread
for each client connection).
Here is the basic flow of the logic in such a server program:
while (true) {
accept a connection;
create a thread to deal with the client;
}
The thread reads from and writes to the client connection as
needed.
In this example, the server class functionality is split into two
classes: Server.java
and
RequestHandler.java.
Server.java
loops forever, listening for client connection requests on
a ServerSocket.
In the NetBeans IDE, perform the following steps:
Open the provided MultiClientServer
project.
Expand Source Packages, and then expand
com.example.
The Server example
creates a server socket, waits for a client request, and
connects to the client by spawning a new thread object. In this
example, you use a fixed thread pool with five threads.
Open Server.java
and paste in your NetBeans IDE project file.
Lines 12 to 15: The if
statement checks for the length of the command-line
argument. If the number of arguments isn't equal to two,
then it prints a system error in the console.
Line 16: The first argument from the command line is
received as the port number.
BufferedReader in = new
BufferedReader(new
InputStreamReader(client.getInputStream()));
BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(client.getOutputStream()));
Read from and write to the stream according to the server's
protocol:
Receive data from the server: (userInput =
in.readLine())
Send data to the server: writer.write("You entered :
" + userInput);
Close the stream and then close the socket connection.
The RequestHandler
object communicates with the client by reading from and
writing to the socket. Let's look at the example program.
Open RequestHandler.java
and paste in your NetBeans IDE project file.
Verify the output of the server window. View Image
You successfully generated a server configuration in the NetBeans
IDE and started the server.
Connecting to the Server by Using PuTTY as a Client
In this section, you connect to the server by using PuTTY as the
client program. PuTTY is a free, open-source software that allows
connection to various servers. It supports different network
protocols like SSH, Telnet, and raw socket connection.
Note: You can also use the client program that you wrote.
Double-click putty.exe, and then perform the following
steps:
On the boot screen, enter 127.0.0.1
in the IP address field.
Enter 8005
in the Port field.
Enter Telnet
Session in the Saved Sessions field.
Click Save.
View Image
You successfully saved the Telnet session details.
The first client (Client 1) successfully connected to the
server, and communication between the client and server is
verified. You have already started the server and opened one
instance of the client through PuTTY. Let's increase the number
of clients to three to observe multiple client interactions with
a single server.
Repeat step 2 twice to open two more clients. View Image
Switch to the server window in the NetBeans IDE and verify the
output. View Image
The server successfully spawned two more threads (thread-2
and thread-3) to serve requests of the clients (Client 2 and
Client 3) that are connected on port 8005.
Enter Hello in the console of Client 2 console and
press Enter. Enter Welcome in the console of Client
3 and press Enter. Verify the output. View Image
When you press Enter, the input text is echoed in the client
window.
Switch to the server window in the NetBeans IDE and verify
the output in the console.