Domanda

I m making a tcp client server chat program. my server is made by thread and its code is as below:

    System.out.println("Server binded at "+((client.getInetAddress()).getLocalHost()).getHostAddress()+":9867");
    System.out.println("Run the Client");
    //ready to accept client request
    //opening the input stream to read data from client connection
    in= new BufferedReader(new InputStreamReader(client.getInputStream()));
    System.out.println(in.readLine());

                //using output stream responsing data 

                out=new PrintStream(client.getOutputStream());
                out.print("Welcome by server\n");

                System.out.println("1");

                in= new BufferedReader(new InputStreamReader(client.getInputStream()));
    System.out.println(in.readLine());                        


                while(! in.readLine().trim().equals("*")) {

                    //using output stream responsing data 
                    System.out.println("3");
                   BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
                    str = bufferRead.readLine();                    
                    out.println(str);
                    out.flush();

                    //opening the input stream to read data from client connection
            in= new BufferedReader(new InputStreamReader(client.getInputStream()));
                      System.out.println(in.readLine());

        }

My client file is: (simple with no thread)

client =new Socket("127.0.0.1", 9867);

                System.out.println("Client connected ");
                //getting the o/p stream of that connection
                out=new PrintStream(client.getOutputStream());
                //sending the message to server
                out.print("Hello from client\n");

                //reading the response using input stream
                in= new BufferedReader(new InputStreamReader(client.getInputStream()));
                System.out.println(in.readLine());

                bufferRead = new BufferedReader(new InputStreamReader(System.in));
                str = bufferRead.readLine();                    
                out=new PrintStream(client.getOutputStream());
                out.print(str);
                out.flush();

                while(! in.readLine().trim().equals("*")) {

                   // opening the input stream to read data from server connection
                   in= new BufferedReader(new InputStreamReader(client.getInputStream()));
                    System.out.println("4");
                    System.out.println(in.readLine());
                    System.out.println("5");

                    out=new PrintStream(client.getOutputStream());
                    bufferRead = new BufferedReader(new InputStreamReader(System.in));
                    str = bufferRead.readLine();                    
                    out.print(str);
                    out.flush();

                }

Now a strange thing happens for me. when i comment out the while loops on both files, the program works fine. But when i un comment it my client side after sending msg to the server comes in while loop and my server keeps on waiting for the response.

so the output is: (client)

Client connected 
Welcome by server
l;skc
4

and of server is:

Server binded at 192.168.1.242:9867
Run the Client
Hello from client
1

I think it is due to the use of thread/asynchoronous process.Plz help

Edited code

server side:

System.out.println("Server binded at "+((client.getInetAddress()).getLocalHost()).getHostAddress()+":9867");
            System.out.println("Run the Client");
            //ready to accept client request
            //opening the input stream to read data from client connection
            in= new BufferedReader(new InputStreamReader(client.getInputStream()));
            System.out.println(in.readLine());

                        //using output stream responsing data 

                        out=new PrintStream(client.getOutputStream());
                        out.print("Welcome by server\n");

                        System.out.println("1");

                        str = in.readLine();
            System.out.println(str);                        


                        while(!str.trim().equals("*")) {

                            //using output stream responsing data 
                            System.out.println("3");
                           BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
                            str = bufferRead.readLine();                    
                            out.println(str);
                            out.flush();

                            //opening the input stream to read data from client connection
                    in= new BufferedReader(new InputStreamReader(client.getInputStream()));
                              System.out.println(in.readLine());

                }

client side:

client =new Socket("127.0.0.1", 9867);

            System.out.println("Client connected ");
            //getting the o/p stream of that connection
            out=new PrintStream(client.getOutputStream());
            //sending the message to server
            out.print("Hello from client\n");

            //reading the response using input stream
            in= new BufferedReader(new InputStreamReader(client.getInputStream()));
            System.out.println(in.readLine());

            bufferRead = new BufferedReader(new InputStreamReader(System.in));
            str = bufferRead.readLine();                    
            out=new PrintStream(client.getOutputStream());
            out.print(str);
            out.flush();

            while(! str.trim().equals("*")) {

               // opening the input stream to read data from server connection
                str = in.readLine();
                System.out.println("4");
                System.out.println(str);

                out=new PrintStream(client.getOutputStream());
                str = bufferRead.readLine();                    
                out.print(str);
                out.flush();

            }
È stato utile?

Soluzione

For your example I would highly recommend the knock knock protocol via the following link http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html

For me the glaringly obvious major error in your code is

   while(! str.trim().equals("*"))

when it should be

while ((str = in.readLine()) != null) {
System.out.println("Server: " + str);

This means that your thread will be continuously listening for input via the input stream and then you can do whatever you want with the str variable after the response is returned from the server

Altri suggerimenti

First clue. See what you can do with this:

This line:

 System.out.println(in.readLine());

will read a line from the incoming connection and echo it. When it does that, the line content is gone and if you try to call "in.readLine()" again, it will try to read another line, not the same line again.

Further, this line:

in= new BufferedReader(new InputStreamReader(client.getInputStream()));

would typically only appear one time. When you build the reader, you only need to use it. You don't have to rebuild it over and over.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top