如何在java中设置Socket写入时间? [英] How can I set Socket write timout in java?

查看:208
本文介绍了如何在java中设置Socket写入时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java中处理套接字时遇到问题。
我正在运行具有多个客户端连接的TCP服务器。

I have a problem with handling socket in java. I am running a TCP server with multiple client connections.

出于性能原因,我使用了一个简单的线程池来处理数据包。

For performance reason, I used a simple thread pool to handle packets.

请参阅下面的代码

public enum LazyWorkCenter {
        instance;

        LazyWorkCenter() {
            lazyWorker = new NamedThreadPoolExecutor(3,3, 0L,TimeUnit.MILLISECONDS, "LazyWorker");
        }
        private ExecutorService lazyWorker ;


        public void executeLazy(Runnable lazyTask) { 
            lazyWorker.execute(lazyTask);
        }

    }

public class TcpServerForClient { 

    DataOutputStream out = null;
    DataInputStream in = null;

    public void onConnect(ServerSocket socket) throws IOException {
        Socket client = server.accept();
        client.setSoTimeout(1000 * 10);
        out = new DataOutputStream(client.getOutputStream());
        in = new DataInputStream(client.getInputStream());
    }

     public void sendToClient( byte[] buffer) {
            Runnable lazy = () -> {
                out.write(buffer);
                out.flush();
            };

            LazyWorkCenter.instance.executeLazy(lazy);
        }
}

多个线程可能访问 sendToClient

这段代码通常可以正常工作,但有时候(可能是洪水泛滥?)它会挂起而没有任何异常,直到我手动关闭客户端连接。
我关闭客户端连接后,我得到了大量的异常泛滥 SocketException:Broken pipe 来自 out.write(buffer);

this code usually works fine, but some times(probably when flooding?) It hangs without any Exception until I shutdown client connection manually. After I shutdown client connection, then I got tons of exception flooding SocketException: Broken pipe from out.write(buffer);

7053555 [LazyWorker-1]  09:57:35.268 [ERROR] [LazyWorker-1@c.t.s.TcpServerForClient:44] - error found
java.net.SocketException: Broken pipe
        at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.8.0_91]
        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109) ~[na:1.8.0_91]
        at java.net.SocketOutputStream.write(SocketOutputStream.java:153) ~[na:1.8.0_91]
        at java.io.DataOutputStream.write(DataOutputStream.java:107) ~[na:1.8.0_91]
        at java.io.FilterOutputStream.write(FilterOutputStream.java:97) ~[na:1.8.0_91]

我的主要问题是服务器可能会挂起,直到客户端退出连接。
我想如果我可以设置一个写入时间,那么Server可能会自行关闭连接,但我找不到合适的方法。

My major Problem is that server could hang until client quit connection. I guess If I can set a time out for writing then Server might close out connection by itself but I could not find appropriate way for this.

我也试过 socket.setSoTimeOut(); 但似乎只是从客户端接收数据。

I have tried socket.setSoTimeOut(); as well but it seems only for receiving data from client.

任何提示或建议都将非常感激。
如果需要更多代码或信息,请告知我们。
提前致谢。

Any hint or advice will be very appreciated. Please let me know If more code or information is needed to figure this out. Thanks in advance.

推荐答案


如何在java中设置Socket写入时间?

How can I set Socket write timout in java?

你不能。 Java中没有套接字写入超时。除非你将hyperjump转换为NIO,非阻塞模式,选择器等等。

You can't. There is no socket write timeout in Java. Unless you make the hyperjump to NIO, non-blocking mode, Selectors, etc.


我从 out.write(buffer);

你应该只得到一个。当你遇到这个例外时,你应该关闭套接字。

You should only get one. You should close the socket when you get this exception.

这篇关于如何在java中设置Socket写入时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆