tags:

views:

106

answers:

5

Hi, I have 2 important classes(client and server) and I will write something in my text area and by clicking on the send button I will call the active method of the client class and I will send that text to my client class,every thing is ok and that text also will be printed on the server console but I can not echo that text from server to client,please help me thanks.

client class:( a part of that)

 os = new PrintWriter(c.getOutputStream(), true);


 is = new BufferedReader(new InputStreamReader(c.getInputStream()));

 public static void active() {

 String teXt = MainClient.getText();

 os.println(teXt);

 String line = is.readLine();
        System.out.println("Text received: " + line);
        os.flush();
        is.close();
        is.close();
        c.close();

server class:( a part of that)

      BufferedReader streamIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
        PrintWriter streamOut =new PrintWriter(client.getOutputStream());
        boolean done = false;
        String line =null;
        while (!done ) {

            line = streamIn.readLine();
            if (line.equalsIgnoreCase("bye")) {
                done = true;
            } else {
                System.out.println(line);
                streamOut.println(line);
            }
        }

        streamIn.close();
        client.close();
        server.close();
A: 

Check this Sun Tutorial On Client Server, in the last part it talks about Multithreading BTW

medopal
A: 

How frequently is the input stream being read? From the code, it looks like there is a single read, probably before anything has been sent from the server, and that's it. You'll probably have to do more consistent polling of the server if you're going to to use the approach you've taken.

Something like:

while (line = is.readLine() != null ) {
    System.out.println("Text received: " + line);

}

Matthew Flynn
I have done it but still it return nothing on the client's console.
Johanna
+1  A: 

You need to " os.flush(); " before reading the server answer.

Because according to your client code, you prepare the text to send with

 String teXt = MainClient.getText();

 os.println(teXt);

Then you wait for server answer by :

String line = is.readLine();
System.out.println("Text received: " + line);

Then you send your text to the server :

os.flush();

try :

String teXt = MainClient.getText();

os.println(teXt);
os.flush();
String line = is.readLine();
System.out.println("Text received: " + line);
Nettogrof
it doesn't work!!
Johanna
actually the PrintWriter is being created with autoFlush enabled: `os = new PrintWriter(c.getOutputStream(), true);` so there is no need to call flush after the println.
Carlos Heuberger
A: 

Your server code implementation is wrong, streamIn,client and streamOut are never closed because of infinite loop.

Refer article mentioned by medopal for more help.

+1  A: 

actually Nettogrof is going the correct way, but you must also flush the server side:

        line = streamIn.readLine();
        if (line.equalsIgnoreCase("bye")) {
            done = true;
        } else {
            System.out.println(line);
            streamOut.println(line);
            streamOut.flush();    // better ...checkError();
        }

or just create the PrintWriter with autoFlush set to true:

    PrintWriter streamOut =new PrintWriter(client.getOutputStream(), true);

One note: you should also test if readLine() is returning null since the client will close the connection without sending a "bye".

A second a note: instances of PrintWriter never throw IOExceptions, you should test for errors calling checkError(), which also flushes the stream.

Carlos Heuberger
Thanks a lot for your correct answer!
Johanna