java socket 空闲时间,如何在java中设置Socket写入时间?

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.

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值