Wednesday, 24 February 2016

java.net lesson 2 : creating a server and two clients and make the server receive a message one client only


Creating a Server and Two clients using java.net package


Here we write to code to highlight the problem in the code of lesson 1. The problem in that code was that the server sits in a loop ( Socket socket = serverSocket.accept(); ) and can serve one client at a time. It can not make a connection with another client untill it finishes the client in hand. When the current iteration of the infinte loop is over then the server can accept a request from a new client and can serve that client by making a socket and repeating the whole process again for this client. 

In this lesson we have a server that gets messages from two clients but displays the messages of the first client only on its console although we can see on the console of the clients that both the clients have successfully made a socket (connection) with the server and that botht are displaying the messages that they are sending to the server each time the user types a message in the two GUIs and click at the send button. In the next lesson we use multithreading to solve this problem.

The code given below displays how to send messages to a Server using Swing GUIs and how to make clients make connection to the server and send messages to them but not receive any messages from the Server. 


For the source code click here.


No comments:

Post a Comment