How can I set Socket write timout in java?

别说谁变了你拦得住时间么 提交于 2020-01-20 07:02:29

问题


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.

Please see code below

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);
        }
}

multiple threads might access to sendToClient.

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]

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.

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.


回答1:


How can I set Socket write timout in java?

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

I got tons of exception flooding SocketException: Broken pipe from out.write(buffer);

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



来源:https://stackoverflow.com/questions/38710742/how-can-i-set-socket-write-timout-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!