Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java套接字连接阻塞过程_Java_Sockets - Fatal编程技术网

java套接字连接阻塞过程

java套接字连接阻塞过程,java,sockets,Java,Sockets,我正在使用两个使用套接字进行通信的java进程 从服务器端,我使用它发送信息: public void send(Serializable order) { try { if (this.clientSocket != null && this.clientSocket.isBound() && this.clientSocket.isConnected()) { String json =

我正在使用两个使用套接字进行通信的java进程

从服务器端,我使用它发送信息:

public void send(Serializable order)
{
    try
    {
        if (this.clientSocket != null && this.clientSocket.isBound() && this.clientSocket.isConnected())
        {
            String json = "";
            json = mapper.writeValueAsString(order);
            log.info("sending to client : " + order.getClass());

            json = convertToUTF8(json);

            this.output.write(json + "\n");
            this.output.flush();
            log.info("Message sent to client");
        }
        else
        {
            log.info("no client connected");
        }
    }
    catch (Exception e)
    {
        log.fatal("Exception while trying to send message to client : " + e.getMessage(), e);
    }
}
这里的输出是:

private BufferedWriter output;
在客户端,我尝试读取这样的数据:

while (this.running)
        {   
            try
            {
                String msg = in.readLine();

                log.debug("MSG VALUE IS : |"+msg+"|\n\n**********\n");

                if ("".equalsIgnoreCase(msg) || msg == null)
                {
                    break;
                }
以下是一个:

private BufferedReader in;
这里的问题是,一段时间后,服务器进程被阻塞,如果我运行netstat命令,我可以看到recv-Q和send-Q值不是0

但我无法重现这种情况,所以我想知道是什么造成了这种情况,是有办法处理这种情况,还是我必须改变读取数据的方式


提前感谢。

通常,如果使用服务器端代码的阻塞IO操作,则每个客户端都需要有自己的工作线程,负责发送到该客户端


另一种选择是使用NIO或一些使用NIO的框架来允许您的服务器进行多路传输。

为了避免阻塞,您应该对每个请求使用AD

非常简单的例子:

public class App {


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

        ServerSocket serverSocket = null; // consider it is properly initialized

        while (true /* stub health condition */) {

            Socket clientSocket = serverSocket.accept(); // the blocking call
            final Worker worker = new Worker(clientSocket);

            Thread workerThread = new Thread() {

                @Override
                public void run() {
                    // handle request here
                    worker.send(new Serializable(){} /* stub */);
                }

            };

            workerThread.start();
        }

    }


}

class Worker {

    private Socket clientSocket;

    Worker (Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    public void send(Serializable order) {
        // logic
    }

}

我认为这不是一个与线程相关的问题,因为我一次只允许有一个客户端,所以我的serversocket和socket在同一个线程中,它可能会连续运行几天,然后再堆叠到socket中,我不知道如何检查错误,因为我没有异常,无论是在服务器端,还是在客户端,一切都被卡住了,直到我杀死了其中一个进程,根据netstat,连接仍然建立