Feb
26
2012

Socket Programming in Java (Part 2)

After a prolonged break forced on by my busy schedule am back with my series of stones on the basics of socket programming in Java. In my previous post I told you about the bare bones of socket programming. Questions such as, “what are sockets?”, “what protocols will we be using” was dealt with in my previous post, if you haven’t gone through that I suggest you do before reading further on in this article. In this stone we will get on to understanding of how to program sockets in Java. We will be using the TCP/IP protocol since it is more widely used than UDP/IP. I suggest you make sure that you java.net along with java.io package is imported when you start off with the socket program.

TCP/IP Sockets

As I told you in my previous post, Sockets are end-points to a two way communication link between any two applications running on a network. You have a client and a server each using its own socket to communication with each other. To start with socket programming, you should learn how to open a socket. To open the socket you could use the following piece of code.

Socket client;
try{
client = new socket(“my machine name”, Portnum);
}
catch( IOException)
{
System.out.println(e);
}

Here “my machine name” is the machine you are trying to open a connection to and PortNum is the port number. Note that port numbers from 0-1023 are reserved for standard services such as FTP, email and HTTP therefore take this into consideration when you assign a port number here. The creation of the socket throws the IOException thus the use of a try and catch block.

Once the socket has been created you need to create an InputStream to receive response from the server and an OutputStream to send response back to the user. For the inputstream I will use a BufferedReader and for the OutputStream I will use the BufferedWriter. Creation of both these streams may again throw an IOException so you again need to put them in a try and catch block as I did before in the case of sockets. You can create them by using the following piece of code.

BufferedReader in = new BufferedReader(new     InputStreamReader(client.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));

To read in from the Buffered Reader use this piece of code.

    String nextLine;
    nextLine=in.readLine();

Similarly to write onto the BufferedWriter use the following piece of code.

    out.write("send");

After all the read and write operations have been performed you will need to close the input and output streams along with the socket. Always remember to close the input and output stream first and then only move onto to the output stream. To close them use their close method as shown below:

      in.close();
      out.close();
      client.close();

Before we end this tutorial, I would like to list a few other methods that the class socket has which you could find useful.



And with this we complete the basics of socket programming. I leave putting together all these components together to build an application to you. Watch this space for more.

Happy coding!!

Share

Related Posts

About the Author: Prerak Pradhan

An IT student, primarily interested in Web development, Graphic Design and Animation.

Leave a comment

*