线程" main"中的例外情况java.net.SocketException:java.io.BufferedReader.readLine上的连接重置(未知来源)

时间:2016-02-25 03:19:11

标签: java sockets connection-reset

我写了一个基本代码,其中发送者正在向接收者发送消息,接收者正在打印它。我是一个天真的套接字程序员。我调查了各种帖子,但仍无法解决此问题。 这是我的代码:

TCPServer.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.*;
import java.io.*;
import java.util.*;


import org.omg.CORBA.Any;


public class TCPServer {

    public static void main(String args[]) throws IOException{
        TCPServer tcpServer=new TCPServer();
        ServerSocket serverSocket=new ServerSocket(8777);
        Socket sock=serverSocket.accept();
        BufferedReader inputSenderBuffer=new BufferedReader(new InputStreamReader(sock.getInputStream()));
        DataOutputStream outputToSenderBuffer=new DataOutputStream(sock.getOutputStream());
        outputToSenderBuffer.flush();
        //outputToSenderBuffer.close();
        char[] inputArray=new char[2000];

        int i,lengthMessage;
        int a=1;
        System.out.println("We are here");
        while(a<1000)
        {
            System.out.println("Inside this while loop");
            String inputSender=inputSenderBuffer.readLine();
            lengthMessage=inputSender.length();
        for(i=0;i<lengthMessage;i++)
        {
            System.out.println("putting the received message into buffer");
            inputArray[i]=inputSender.charAt(i);
            System.out.println(inputArray[i]);
        }
        }
    }
}

TCPClient.java

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;

public class TCPClient {

    public static void main(String args[])
    {

        try {
            Socket sock=new Socket("localhost",8777);
            DataOutputStream outputToReceiverBuffer=new DataOutputStream(sock.getOutputStream());
            System.out.println("Sending test message");
            int i=1000;
            while(i<1000)
            {
            outputToReceiverBuffer.writeBytes("1AAAAAAAAAA");
            outputToReceiverBuffer.flush();
            //outputToReceiverBuffer.close();
            }


        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Exception at the TCPServer.java console

Exception in thread "main" java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at TCPServer.main(TCPServer.java:31)

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

你没有写任何东西:

int i=1000;
while(i<1000)

你永远不会关闭套接字。因此,当发件人退出时,连接将被重置, et voila。

当你解决这个问题时,你正在读行:

String inputSender=inputSenderBuffer.readLine();

但你不是在写行:

outputToReceiverBuffer.writeBytes("1AAAAAAAAAA");
outputToReceiverBuffer.flush();

您应该使用DataOutputStream.writeBytes()

而不是BufferedWriter.write()/newLine().

您也没有检查readLine()的结果为null,这表示对等方已关闭连接,因此您应该停止读取并关闭套接字。您应该依靠它而不是任意计数器来控制您的读取循环。