text: 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
  IntroductionIn 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.
  Hardware and Software RequirementsDownload and install the following:
  - Oracle Java SE 8 JDK from this link.
  - NetBeans 8.0 from this link.
  - PuTTY Telnet and Secure Shell (SSH) Client from this link.
  Prerequisites
  Before starting this tutorial, you should:
  - Have started NetBeans 8.0.
  - Have extracted the InitialSocketProgram.zip file (The file contains Client, MultiClientServer and SingleClientServer projects.)
  - Have downloaded the source for EchoServer.java, EchoClient.java, Server.java, RequestHandler.java.
  - Have extracted the CompleteProject.zip file.
  Creating a Java Application
  In this section, you create a Java application to demonstrate socket programming in Java.
  In NetBeans IDE 8.0, select New Project from the File menu.
  - On the Choose Project page, perform the following steps:
  - Select Java from Categories.
  - Select Java Application from Projects.
  - Click Next.
  - On the Name and Location page, perform the following steps:
  - Enter SingleClientServer as the project name.
  - Select Create Main Class.
  - Enter com.example.EchoServer. (The package name and the class name are implied and match the UI to create a Java project in NetBeans.)
  - Click Finish.
  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. 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.
  Here's an overview of how to create the simple server program:
  - Create and open a server socket.
  - Wait for the client request.
  - Open an input stream and an output stream to the client.
  - Communicate with the client.
  - Close the streams and then close the socket.
  In the NetBeans IDE, perform the following steps:
  - Open the provided SingleClientServer project.
  - Expand
  Source Packages,and then expand
  com.example.
  - On the Projects tab, double-click EchoServer.java.
  Modify EchoServer.java.
  Create and open a server socket.
  ServerSocket serverSocket = new ServerSocket(portNumber);
  - Wait for the client request.
  Socket clientSocket = serverSocket.accept();.
  Create input and output streams to the socket.
  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.
  Here are the descriptions:
  - 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: 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.
  - Click New.
  - Enter Server as the configuration name.
  - Click Browse and select com.example.EchoServer.
  - Enter 8005 as the argument.
  - Click OK.
  You successfully generated a server configuration in NetBeans IDE.
  Creating a Client Program
  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.
  - 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.
  Modify EchoClient.java.
  Create and open a client socket.
  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).
  Socket echoSocket = new Socket(hostName, portNumber);
  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.
  Here are the descriptions:
  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.
  - Click New.
  - Enter Client as the configuration name.
  - Click Browse and select com.example.EchoClient.
  - Enter 127.0.0.1 8005 as the argument.
  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.
  You successfully set the server configuration to run the server program.
  Right-click the SingleClientServer project and select Run.
  - Verify the output.
  On the Projects tab, right-click the Client project, select Set Configuration, and then select Client.
  You successfully set the client configuration to run the client program.
  Right-click the Client project and select Run.
  Enter the following user input, and then press Enter: Hi. Welcome to SocketProgramming.
  - Switch to the server window and verify the output.
  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.
  You successfully ran the server in the NetBeans IDE. After the server starts, you can start the client program.
  The input text is echoed in the client window and displayed in the console.
  You successfully configured and executed the single client/server program.
  Extending the Server to Implement Multiple Clients
  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:
  The thread reads from and writes to the client connection as
  needed.
  while (true) {
  accept a connection;
  create a thread to deal with the client;
  }
  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.
  - On the Projects tab, double-click Server.java.
  Modify Server.java.
  - Create and open a server socket.
  ServerSocket serverSocket = new ServerSocket(portNumber);
  The portNumber argument is the port on which the server is running.
  - Accept the connection.
  Socket clientSocket = serverSocket.accept();
  - Create a thread object to process the client request.
  executor = Executors.newFixedThreadPool(5);
  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.
  Here are the descriptions:
  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.
  Line 18: The statement in the try-with-resources block creates the serverSocket object.
  Line 19: The thread pool of five threads is created by using the Executor interface.
  - Lines 21 to 25: The while loop keeps looping to check for new client requests.
  - Line 22: The server socket waits and accepts a connection to the client.
  - Line 23: The socket class creates the instance of the runnable interface by passing the client socket object.
  - Line 24: The runnable interface assigns the thread from the thread pool to service the worker instance.
  Lines 26 to 29: You use this catch block to handle exceptions. In this example, you're handling IOException.
  - Lines 30 to 33: The finally block closes the executor if it still exists.
  Open and modify the provided RequestHandler.java.
  Implement the Runnable interface to RequestHandler.java:
  public class RequestHandler implements Runnable
  Create the input and output streams to the socket for communicating with the server:
  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.
  Here are the descriptions:
  - Line 21: The thread.start() method in Server.java calls the run()method in RequestHandler.java.
  Lines 22 and 23: The try-with-resources block creates the BufferReader and BufferWriter objects.
  Line 24: The thread object gets the current thread instance name and prints the name in the console.
  - Lines 26 to 32: The while loop reads a line at a time from the standard input stream by using the readline method.
  Line 27: The replaceAll()method replaces all non-alphanumeric characters with an empty string to avoid special characters.
  Line 28: The thread instance receives the message displays in the console.
  Line 29: The user input text is written into the buffered writer object.
  Line 30: The writer object adds a new line in the console.
  - Line 31: The writer object flushes the buffer.
  Lines 33 to 37: You use the catch block to handle exceptions. In this example, you're handling IOException and the generic Exception.
  On the Projects tab, right-click the MultiClientServer project, select Set Configuration, and then select Customize.
  - Click New.
  - Enter Server as the configuration name.
  - Click Browse and select com.example.Server.
  - Enter 8005 as the argument.
  - Click OK.
  On the Projects tab, right-click the MultiClientSocket
  project, select Set
  Configuration, and then select
  Server.
  To start the multiple client/server, right-click the MultiClientServer project and select Run.
  Connecting to the Server by Using PuTTY as a Client
  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.
  - Start the PuTTY Telnet connection.Note: If the server isn't running, the PuTTY client window closes automatically. Ensure that the server is running before you start the client.
  - Select Telnet Session.
  - Click Load to load the Telnet configuration.
  - Click Open.
  The Putty client opens.
  You successfully started the PuTTY client, which is connected to the server through the socket.
  In the command-prompt window, enter Hi, and then press Enter.
  In the NetBeans IDE, observe your server console and verify the output.
  - Repeat step 2 twice to open two more clients.
  - Switch to the server window in the NetBeans IDE and verify the output.
  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.
  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.
  The server received the input messages echoed from the three clients, and the echo message is displayed in the console.
  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.
  Summary
  In this tutorial, you learned to:
  - Create a Java SE project
  - Set up a PuTTY client
  - Create a single client/server program
  - Create multiple client/server programs
  - Deploy and run the client/server program.
  Resources
  - To learn more about socket programming in Java, see Java SE Tutorial: All About Sockets.
  - To learn more about Java SE, refer to additional OBEs in the Oracle Learning Library.
  Credits
  - Curriculum Developer: Shilpa Chetan.