October 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Java Socket Programming in Client/Server Applications

  • September 23, 2009
  • By Sridhar M S
  • Send Email »
  • More Articles »

Writing the Server Using Sockets

Code Listing 1 provides a Java example for writing a server component using sockets. In this code, the server binds itself with a ServerSocket on to a pre-determined port. This port will be the one to which clients can request connections. The server then awaits client connection requests on the ServerSocket. When it receives them, it will create a new socket and hand the connection over to it.

The code uses the accept() method on the ServerSocket class, designating the socket as the one responsible for communication with the client. All information to and from the client will be sent and received via this socket. The getInputStream() and getOutputStream() methods are used for the communication with the client.

Figure 1 shows a screenshot of the server console when it starts.


Figure 1. The Server Console When It Starts:
Here is a screenshot of the server console when it starts.

Writing the Client Using Sockets

Code Listing 2 provides a Java example for writing a client component using sockets.

In this code, the client inputs the server name and the port through which it deliver arguments for creating a socket connection. When the socket is created successfully, it can then talk to the server with the input/output streams. The exception handling has to be effective to pinpoint any problems in the connection.

Figure 2 shows a screenshot of the server console when the client starts, Figure 3 shows a screenshot of the client console, Figure 4 shows a screenshot of the server console when the client starts communicating, and Figure 5 shows a screenshot of the client console during communication.


Figure 2. Server Console When the Client Starts:
Here is a screenshot of the server console when the client starts.


Figure 3. The Client Console:
Here is a screenshot of the client console.


Figure 4. The Server Console When the Client Starts Communicating:
Here is a screenshot of the server console when the client starts communicating.


Figure 5. The Client Console During Communication:
Here is a screenshot of the client console during communication.

As you start using the application, you can think of numerous enhancements to this example.

Care and Feeding of Your Applications

It is important to close all open resources, such as file handlers, socket connections, and any other open entities because these will lead to memory-leak issues in the long run.

Code Download

  • JavaSockets_src

    For Further Reading

  • "All About Sockets" (from java.sun.com)

    About the Author

    Sridhar M S is a Java developer from Bangalore, India. He holds a Master's degree in Computer Science.





  • Page 2 of 2



    Comment and Contribute

     


    (Maximum characters: 1200). You have characters left.

     

     


    Sitemap | Contact Us

    Rocket Fuel