Using Socket for TCP/IP communication in Java

In order to build the connection from the server to the client, we need to use the socket APIs in the java.net and java.io package. This is a prototype project that I built following the tutorial from Oracle at http://docs.oracle.com/javase/tutorial/networking/sockets/  It turned out to be a highly useful tutorial to introduce the basics of establishing a TCP/IP connection between the client and the server using the socket API.

Quote: Lesson: All About Sockets, introduction

TCP provides a reliable, point-to-point communication channel that client-server application son the Internet use to communicate with each other. To communicate over TCP, a client program and a server program establish a connection to one another. Each program binds a socket to its end of the connection. To communicate, the client and the server each reads from and writes to the socket bound to the connection.

A client's connection request

The connection is made

(image credits from http://docs.oracle.com/javase/tutorial/networking/sockets/definition.html)

 

In the sample Echo application, on the client side

String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);

try (
    Socket echoSocket = new Socket(hostName, portNumber);
    PrintWriter out =
        new PrintWriter(echoSocket.getOutputStream(), true);
    BufferedReader in =
        new BufferedReader(
            new InputStreamReader(echoSocket.getInputStream()));
    BufferedReader stdIn =
        new BufferedReader(
            new InputStreamReader(System.in))
)

"out" is the connection going out from the client to the server, "in" is the connection coming in from the server. 

As shown in the main loop of the client

while ((userInput = stdIn.readLine()) != null) {
<span style="font-size: 16px; color: #444444; font-family: Georgia, 'Bitstream Charter', serif; line-height: 1.5;">                out.println(userInput);

                System.out.println("echo: " + in.readLine());

            }


For the server side,

int portNumber = Integer.parseInt(args[0]);

try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
)

this sets up the listening socket on the sever. "in" is reading from the client and "out" is writing to the client.

This is a basic essential tutorial on how to set up the two way connection between a server and client using java socket.
 

Advertisements
This entry was posted in HJ-Hadoop Improvements, Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s