Mohammed Ghanem
Mohammed Ghanem

Reputation: 21

TCP Server Not Receiving Message From TCP Client (Java)

It seems like the server is not receiving the message sent from the client as it should. From my understanding the client is writing to the socket outputstream. And the server is reading from the socket inputstream. Please help.

Server Code:

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
static final int DEFAULT_PORTNUMBER = 1236;

public static void main(String[] args){
    int portnumber;

    if(args.length >= 1){
        portnumber = Integer.parseInt(args[0]);
    }else{
        portnumber = DEFAULT_PORTNUMBER;
    }

    //Setting a server socket and a possible client socket
    ServerSocket server = null;
    Socket client;

    try{
        server = new ServerSocket(portnumber);
    } catch (IOException e){
        e.printStackTrace();
    }
    while(true){

        try{
            System.out.println("Waiting for client...");
            client = server.accept();
            System.out.println("Client accepted... ");
            //Read data form the client
            BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));

            while(!br.ready()){
                System.out.println("No message from client");
            }
            String msgFromClient = br.readLine();
            //System.out.println("Message received from client = " + msgFromClient);

            //Send Response
            if(msgFromClient != null && !msgFromClient.equalsIgnoreCase("bye")){
                OutputStream clientOut = client.getOutputStream();
                PrintWriter pw = new PrintWriter(clientOut, true);
                String ansMsg = "Hello, " + msgFromClient;
                pw.println(ansMsg);
            }

            if(msgFromClient != null && msgFromClient.equalsIgnoreCase("Bye")){
                server.close();
                client.close();
                break;
            }
        } catch(IOException e) {
            e.printStackTrace();
        }
        //New thread for client
        /*new ServerThread(client).start();
        System.out.println("Client connection accepted... ");*/
    }

}
}

Client Code:

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;


public class TCPClient {
static final int DEFAULT_PORTNUMBER = 1236;
public static void main(String args[]){
    Socket client = null;
    int portnumber;

    //Default port number if not specified as an argument

    if(args.length >= 1){
        portnumber = Integer.parseInt(args[0]);
    }else{
        portnumber = DEFAULT_PORTNUMBER;
    }


    try {
        String msg = "";

        //Creating a client socket
        client = new Socket(InetAddress.getLocalHost(), portnumber);
        System.out.println("Client socket is created: " + client);

        //Creating an output stream for the client socket
        OutputStream clientOUt = client.getOutputStream();
        PrintWriter pw = new PrintWriter(clientOUt, true);

        //Creating an input stream for the client socket
        InputStream clientIn = client.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(clientIn));

        //Creating a buffered reader for standard input System.in
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter your name. Type Bye to exit.");

        //Read data from standard input and write to output stream
        msg = stdIn.readLine().trim();
        pw.print(msg);

        while(!br.ready()){
            //System.out.println("No Input From Server");
        }
        //Read data from input stream of client socket
        System.out.println("Message returned from the server = " + br.readLine());

        pw.close();
        br.close();
        client.close();

        //Stop operation
        if (msg.equalsIgnoreCase("Bye")) {
            System.exit(0);
        } else {
        }

    } catch (IOException e) {
        System.out.println("I/O error " + e);
    }



}
}

Note: I did disable firewall but that did not help.

Upvotes: 1

Views: 1415

Answers (1)

Mohammed Ghanem
Mohammed Ghanem

Reputation: 21

Found the answer PrintWriter or any other output stream in Java do not know "\r\n". It describes how printwriter doesn't flush properly with printwriter.print() but rather only works when you use printwriter.println().

Upvotes: 1

Related Questions