java thread infinite loop,socket programming

I have a question concerning about mechanism of flush of socket outputstream in infinite loop in run method of thread. My code works that when client socket send message to server and run method in server, running in the 'infinite loop', receives message by readLine() method. Receiving data on the server side while running in the infinite 'while' loop is working fine, but when it sends message to client message, like return message for confirming, it doesn't work after using write() and flush() method, which is contained in same infinite 'while' loop.

I was struggling to find solution for this problem, and I found interesting sitiuation, when infinite 'while' loop on the server side stops running, all the messages that server flushed to client starts to be sent to client. So I figured that it was problem of queue and thread infinite loop doesn't let the message to be flushed to the other side until infinite loop ends for safety. So I made another class with thread for sending server message to client.It seems to be working because after it flushes the message, the run method ends.But I have to make that class every time when sending message to client.

Here's sample code that uses same concept with my original code, running on the separate main method

Client side:

try {
    // System.out.println("client_working!!") ;

    while (true) {
        writer.write("rkdgusdnr");
        writer.flush();
        // writer.flush();

        System.out.println("client_Working!!");
        sleep(500);
    }

} catch (InterruptedException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

try {
    reader.close();
    writer.close();
    client.close();
    JOptionPane.showMessageDialog(null, "client closed!!");
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Server side:

try {
    client = server.accept();

    reader = new BufferedReader(new InputStreamReader(
            client.getInputStream()));
    writer = new BufferedWriter(new OutputStreamWriter(
            client.getOutputStream()));

    String contents = null;

    while (true) {
        // System.out.println("Server_received") ;
        if ((contents = reader.readLine()) != null) {
            System.out.println(contents + "n");
            writer.write("Server_received");
            writer.flush();
        } else {
            break;
        }
        System.out.println("Server_working!!");
        sleep(500);
    }
} catch (IOException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}

try {
    reader.close();
    writer.close();
    client.close();
    server.close();
    JOptionPane.showMessageDialog(null, "server closed!!");
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

You're reading lines but you're not writing lines. You need to send a line terminator.

链接地址: http://www.djcxy.com/p/64454.html

上一篇: Play Framework,使用Angular进行路由[SCALA]

下一篇: java线程无限循环,套接字编程