开发者

it doesn't return anything from server!

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();


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();    // or ...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 note: instances of PrintWriter never throw IOExceptions, you should test for errors calling checkError(), which also flushes the stream.


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);


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.


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜