简体   繁体   English

使用bufferedReader.readLine()从我的Socket输入流中读取时,它抛出SocketException,指出“连接重置”

[英]Reading from my Socket input stream using bufferedReader.readLine(), it throws an SocketException stating “Connection reset”

I am creating two sockets: a client socket and a server socket. 我正在创建两个套接字:客户端套接字和服务器套接字。

ServerSock class: ServerSock类:

public class ServerSock {

    public static void main(String args[]) throws Exception{

        //creating welcome socket @param: port number
        System.out.println("Server is started...");
        ServerSocket serverSocket = new ServerSocket(55555);

        System.out.println("Server is wating for client request...");

        //creating individual sockets for clients
        Socket socket0 = serverSocket.accept();

        System.out.println("Client connected...");

        //reading the input data
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket0.getInputStream()));

        //reading the data from bufferedReader
        String str = bufferedReader.readLine();

        //console the data
        System.out.println(" client data : " + str);
    }
}

ClientSocket class: ClientSocket类:

public class ClientSocket {

    public static void main(String args[]) throws Exception{

        //ip address of the server
        String ip = "localhost";
        //port number of the application
        int port = 55555;

        //creating a socket
        Socket socket = new Socket(ip, port);

        //data to send
        String data0 = "lakshan waruna";

        //converting data to ByteStream param: where to send the data
        OutputStreamWriter os = new OutputStreamWriter(socket.getOutputStream());

        //writing the data
        os.write(data0);
        os.flush();
    }
}

ServerSock runs until I start the client socket. ServerSock一直运行到启动客户端套接字为止。 It also prints the "client connected" statement in the ServerSock class. 它还在ServerSock类中打印“ client connected”语句。 But when it tries to read data from bufferedReader using the statement String str = bufferedReader.readLine(); 但是,当它尝试使用语句String str = bufferedReader.readLine();从bufferedReader读取数据时String str = bufferedReader.readLine(); , it throws a SocketException stating "connection reset". ,则抛出SocketException指出“连接重置”。 How can I fix this issue? 如何解决此问题?

Your problem is you don't close the Socket in the client, "Connection reset" is usually thrown when a remote Socket has ungracefully closed a connection which is the case here since you don't call Socket.close() . 您的问题是您没有在客户端中关闭Socket ,当远程Socket不良地关闭了连接时,通常会抛出“连接重置”(在这种情况下,因为您不调用Socket.close() If you do you'll notice it will work but you're probably thinking well why does that fix it? 如果您会注意到它会起作用,但您可能想的很好,为什么能解决此问题? Why not just give me the data then next read tell me the Socket has closed, well it does but since you're using readLine() which constantly reads until it has received \\n or \\r or if the given InputStream returns less than zero in read . 为什么不只给我数据,然后下次读取告诉我Socket已关闭,它确实可以,但是由于您使用的是readLine() ,它会一直读取直到收到\\n\\r或给定的InputStream返回小于零的值。 read Pretty much it is reading until it has received a full line (to declare a full line like System.out.println does you just add \\n at the end), and it has still yet to find a new line, if you add \\n at the end of the message you'll notice it will also work. 它几乎一直在读取,直到收到完整的一行为止(要声明为System.out.println类的完整行,您只是在末尾添加\\n ),如果您添加\\n ,它仍未找到新行\\n在消息的最后,你会发现它也可以工作。

Also, remove PrintWriter , you never use it. 另外,删除PrintWriter ,您将永远不会使用它。

ALWAYS CLOSE A SOCKET . 总是关闭袜子

Tested your code, worked for me. 测试了您的代码,为我工作。

Edited typo and included link to plausible information on question 编辑错字,并包含指向有关问题的合理信息的链接

Possible reasons for connection reset could be that the socket is closed before sending the data. 连接重置的可能原因可能是套接字在发送数据之前已关闭。

java.net.SocketException: Connection reset java.net.SocketException:连接重置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用BufferedReader.readline()从套接字读取 - Can not read from socket using BufferedReader.readline() Java中的套接字的BufferedReader.readLine()卡住了 - BufferedReader.readLine() from a socket in Java stuck BufferedReader.readLine()等待来自控制台的输入 - BufferedReader.readLine() waits for input from console 从慢速流读取时,BufferedReader.readLine()是否可能不读取整行? - Is it possible for BufferedReader.readLine() to not read a whole line when reading from a slow stream? 在执行BufferedReader.readLine()时:ZILB输入流的意外结束 - On doing BufferedReader.readLine() : Unexpected end of ZILB input stream 使用BufferedReader.readLine()读取inputStream太慢了 - Reading inputStream using BufferedReader.readLine() is too slow BufferedReader.readLine()给出错误java.net.SocketException:软件导致连接中止:recv失败 - BufferedReader.readLine() gives error java.net.SocketException: Software caused connection abort: recv failed 套接字,BufferedReader.readline()-为什么流未准备好? - Sockets, BufferedReader.readline() - why the stream is not ready? 通过套接字的BufferedReader.readLine()是否是阻塞操作? - Is BufferedReader.readLine() over a Socket a blocking operation? Java BufferedReader.readLine() 不等待用户输入 - Java BufferedReader.readLine() not waiting for user input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM