Search code examples
javasocketstcp

Client/Server Programming


I am practicing a simple java program where I am demonstrating simple client server interaction. The fist part of message from server gets transferred. Then program just continues to run and does not execute? Do we need to create a new socket for each individual traffic?

Server code

server = new ServerSocket(4587);
System.out.print("Starting the Server on port " + server.getLocalPort() + "\n");
System.out.println("Waiting for client...");

Socket client = server.accept();
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
BufferedReader br1 = new BufferedReader(new InputStreamReader(client.getInputStream()));
br.write("Hello, you are connected to Server. What is your name?");
br.write("\n");
br.flush();



while((s=br1.readLine())!=null)
    {

    }
     br.write("Thank you ");
     br.newLine();
    br.flush();
    }

Client code

String stdin;
System.out.println("Attempting to connect to " + hostname + ":" + port);
client = new Socket("localhost", 4587);
System.out.println("Connection Established");

BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
while ((stdin = br.readLine()) != null) {
    System.out.println(stdin);
}

BufferedWriter br1 = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
br1.write("Mike");
br1.write("\n");
br1.flush();

while ((stdin = br.readLine()) != null) {
    System.out.println(stdin);
}

Server Output

Starting the Server on port4587
Waiting for client....

Client Output

 Attempting to connect to :123
 Connection Established
 Hello you are connected to Server, What is ur name

If this could help..after this both loop


Solution

  • Your server will first create a connection with the client through the accept method. If you wish to have multiple clients you will need to change your code accordingly to accept that.

    On the client side, you're using \n to delineate the end of a message. This will work fine. Every time you send a new message use \n to indicate the end of the message.

    On the server side, you should continue reading from I/O until you see the \n. At that point you have received the entire message. Process it and than start listening again.

    Edit:

    Since you are waiting for the name of the client, you could simply do the following on the server:

    BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
    BufferedReader bin = new BufferedReader(new InputStreamWriter(client.getInputStream()));
    // Wait for incoming name from client.
    String name = bin.readline();
    System.out.println(name);
    // Send a reply.
    bout.write("Thank you\n");
    bout.flush();
    

    Similarly, on the client (assuming bin and bout are defined the same as above):

    // Send name to server.
    bout.write("Name\n");
    bout.flush();
    // Get a response from the server and print to console.
    String response = bin.readline();
    System.out.println(response);