如何在循环中继续读取InputStream使用的Socket连接?

时间:2016-10-28 01:43:42

标签: java sockets io

在服务器站点中,我使用循环继续读取从客户端发送的数据,但是当我从客户端发送数据时,服务器站点抛出异常。

我调试服务器站点可以从客户端读取数据的代码,但是当它从客户端完成读取时,循环无法停止,我想这个异常是由于它造成的!

如何解决这个问题,当MAX_INPUT小于发送到服务器的数据时,服务器可以继续正确读取它!

正如答案所说,我在finally块中的客户端站点中添加socket.close(),它修复了连接重置问题。

同时,我设置了服务器站点MAX_INPUT = 8,该服务器可以继续读取输入字节并将其发送回客户端。但结果是客户无法收到完整的数据:

Server's response is: This is 
Server's response is: This is 
Server's response is: This is Seq.0 request send to server!
Server's response is: This is Seq.3 request send to server!
Server's response is: This is Seq.4 request send to server!

或者有时服务器站点会抛出异常,例如:

java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at com.simple.server.Server$Handler.run(Server.java:44)
at java.lang.Thread.run(Unknown Source)

服务器站点代码:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class Server implements Runnable {
    public final static int port = 8080;
    public final static String host = "127.0.0.1";
    public final static int MAX_INPUT = 1024;

    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("Server begin to listen to port to accept!\n");
            while (!Thread.interrupted()) {
                Socket socket = serverSocket.accept();
                new Thread(new Handler(socket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static class Handler implements Runnable {

        private Socket _socket;

        Handler(Socket socket) {
            _socket = socket;
        } 

        public void run() {
            try {

                InputStream in = _socket.getInputStream();
                OutputStream out = _socket.getOutputStream();

                int read;
                byte[] buf = new byte[MAX_INPUT];
                // I use a loop to continue to read 
                // data from client
                while ((read = in.read(buf)) != -1)
                    out.write(buf, 0, read);

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    _socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
     }

    public static void main(String args[]) {
        new Thread(new Server()).start();
    }

}

客户端站点代码:

public class Client implements Runnable {

    public final static String host = "127.0.0.1";
    public final static int port = 8080;
    public final static int MAX_INPUT = 1024;

    private int seq;

    Client(int seq) {
        this.seq = seq;
    }

    public void run() {
        try {

            Socket client = new Socket(host, port);


            OutputStreamWriter writer = new  OutputStreamWriter(client.getOutputStream());
            InputStreamReader reader = new InputStreamReader(client.getInputStream());
            writer.write("This is Seq." + seq + " request send to server!");
            writer.flush();

            char[] buf = new char[MAX_INPUT];
            int len = reader.read(buf);
            String response = new String(buf, 0, len);

             System.out.println("Server's response is: " + response);
         } catch (IOException e) {
             e.printStackTrace();
         }
    }

    public static void main(String args[]) {

        Thread threads[] = {
            new Thread(new Client(0)), 
            new Thread(new Client(1)), 
            new Thread(new Client(2)),
            new Thread(new Client(3)), 
            new Thread(new Client(4))
         };

        for(Thread thread : threads)
            thread.start();
     }
 }

抛出异常:

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at com.simple.server.Server$Handler.run(Server.java:44)
at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:1)

您的服务器正在读取输入,直到流结束并在其结束时回显它。您的客户端正在发送一个请求并读取一个响应,并且从不关闭套接字。因此,当客户端进程退出时,它遇到了连接重置,而不是服务器遇到流结束。

解决方案:关闭客户端中的套接字。