Socket Programing TCP: The "knock knock Server"

Viewed 43

THE CONCEPT:
I am programming a (simple) server-client application, using TCP sockets. The server is meant to answer back to the client appropriately, depending on the message it initially received, to create this way an interface for the client/user. I have been using this oracle tutorial example as my guide.

THE BUG:
The client is not able to receive the response from the server although I am sure that the server is sending it with no errors caused, because the output of the PrintWriter.checkError() function is false, meaning no errors were triggered.

MY BEST GUESS:
At first, I thought the problem was that the server is sending the response before the client blocks to receive it using the BufferedReader.readLine(), but it seems it ain't so, because if you think that the socket stream uses the FIFO spirit", which makes the TCP protocol asynchronous, meaning that the server writes the message to structure and then the client is not obligated to read it right after it was written. Therefore, I don't think that timing is the problem here.

So what am I missing? Thank you.

Client-side :

public static void main(String args[]){        
    try{
        Socket socket = new Socket(this.getServerIp(),this.getTcpServPort());
        socket.setReuseAddress(true);
        //to get input from user
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        //to send to server 
        PrintWriter out = new PrintWriter(socket.getOutputStream());
        //to receive from server)
        InputStreamReader in = new InputStreamReader(socket.getInputStream());
        BufferedReader brin = new BufferedReader(in);

             while(true){
             System.out.print("$");
             String str = new String();
             str = bf.readLine();
                if(str.equals("exit")){
                    break;
                }
             else if(str.equals("1")){
                     out.println(str);
                     out.flush();   
                     String resp;
                     //-> block and wait for response 
                     resp=brin.readLine();
                     if(resp==null){System.out.println("EOF");}
                     System.out.println(resp);
                 }   
                else{
                 System.out.println("Invalid command, type --help ");
             }  
            }//end of while 
       bf.close();
       in.close();
       brin.close();
       socket.close();
    }catch(IOException e){
        System.out.println(e.getMessage());
    }
}//end of main

Server-side :

 public static void main(String args[]){ 
    try{
        ServerSocket serverSocket = new ServerSocket(servPort);
        
        Socket clSocket = serverSocket.accept();
        clSocket.setReuseAddress(true);

        InputStreamReader in = new InputStreamReader(clSocket.getInputStream()); 
        BufferedReader bf = new BufferedReader(in); 

        //newer
        PrintWriter out = new PrintWriter(clSocket.getOutputStream());

              while(true){ 
              String str=bf.readLine(); 
              System.out.println(str);             
              if(str.equals("exit")){ 
                break;
              }else if(str.equals("1")){ 
                  out.write("ack1"); 
                  out.flush();
                  System.out.println(out.checkError()); //false is normal
                  System.out.println("sent ack1");              
              }              
            }//end of while (hearing)
        out.close();
        clSocket.close(); 
        serverSocket.close();
        }catch(IOException e){
          System.out.println(e.getMessage());
        }//end of try-catch block      
}//end of main


0 Answers
Related