1

//编辑 13:25 客户端控制台

Connect to server<br />
Repsonded: Tue May 22 13:23:28 CEST 2012<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Repsonded: Tue May 22 13:23:38 CEST 2012<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />
Connect to server<br />
Not run: java.net.SocketException: Software caused connection abort: recv failed<br />
Disconnect to server<br />

服务器控制台

Server Initialized<br />
111111<br />
111111<br />
111111<br />
111111<br />
111111<br />
111111<br />
111111<br />
111111<br />
111111<br />
111111<br />
111111<br />
Error(run): java.net.SocketException: Connection reset by peer: socket write error<br />


Server load data and not return repead "Repsonded: Tue May 22 13:23:38 CEST 2012" or client not ouput print.
4

1 回答 1

0

问题在于您的服务器实现。您没有在 run 语句中循环,检查来自该连接的新传入消息。您阅读了一次信息,然后在 finally 语句中关闭连接:

 @Override
    public void run() {

        try {

            BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
            InputStreamReader isr = new InputStreamReader(is);
            ...
        finally {
            try {
                connection.close();

当然,您的服务器有一个循环检查传入的连接请求,因此如果客户端在发送每条消息后再次尝试连接到服务器,但您的客户端只发送一个请求(在主语句的开头),这将起作用。

所以,我会建议你做什么:服务器端,在run语句中设置流等,一旦设置好就进入一个无限循环while(true),在该循环中让服务器从输入流中读取,如果read()确实如此不返回 null,打印消息。这将导致服务器检查来自该客户端的传入数据,一旦你得到一个不为空的字符串,它将打印它(如果你愿意,它会触发响应)。

客户端也是如此,您需要一个循环来侦听来自服务器的传入消息

于 2012-05-22T09:39:39.603 回答