java获取超时异常_Java,获得一个奇怪的Socket异常:“断管”和“操作超时”

我有一个客户端服务器应用程序,它有16个线程(8个客户端,8个服务器),每个客户端都有一个1-1匹配的TCP连接(所以有8个TCP流)。

我有服务器发送X数量的随机字节然后关闭。同时客户端只读取X数据量然后关闭。 X是预定的。我也使用dummynet进行带宽限制,但我正在使用大量带宽(100Mbps)离开管道。有时候它可以正常工作,其他时候我会得到这些例外。我在3.5分钟左右的时间内将1GB传输均匀地传输到所有8个连接。

客户端抛出这个异常:

java.net.SocketException: Operation timed out

at java.net.SocketInputStream.socketRead0(Native Method)

at java.net.SocketInputStream.read(SocketInputStream.java:129)

at miccbull.parralleltcp.client.StreamWorker.run(StreamWorker.java:43)服务器抛出这个异常:

java.net.SocketException: Broken pipe

at java.net.SocketOutputStream.socketWrite0(Native Method)

at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)

at java.net.SocketOutputStream.write(SocketOutputStream.java:124)

at miccbull.parralleltcp.server.TransmissionWorker.run(TransmissionWorker.java:51)服务器代码:

public void run() {

OutputStream os = null;

try {

socket = sSocket.accept();

os = socket.getOutputStream();

} catch (IOException e2) {

e2.printStackTrace();

}

//send the data

for(int i = 0; i < Server.TRANSMISSIONS; i++){

System.out.println(sSocket.getLocalPort() + " sending set " + (i+1) + " of " + Server.TRANSMISSIONS);

try {

os.write(new byte[Server.FILE_SIZE/Server.numStreams/Server.TRANSMISSIONS]);

os.flush();

} catch (IOException e) {

e.printStackTrace();

System.out.println("Closing server (at write bytes) with socket id: " + sSocket.getLocalPort());

}

}

System.out.println("Worker " + sSocket.getLocalPort() + " done");

//close the socket

try {

socket.close();

sSocket.close();

} catch (IOException e) {

e.printStackTrace();

}

}客户代码:

public void run(){

byte[] bytes = new byte[1024]; //number of bytes to read per client stream

InputStream is = null;

//connect to server

try {

connectionSocket = new Socket("localhost", id);

is = connectionSocket.getInputStream();

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

//read file from server

int value = 0;

int bytesRead = 0;

try {

while(bytesRead < (Client.FILE_SIZE/Client.NUM_STREAMS)){

value = is.read(bytes, 0, 1024);

if(value == -1){

System.out.println("******************************** Read is -1 for client " + id);

}

else if(value == 0){

System.out.println("******************************** Read is 0 for client " + id);

}

bytesRead += value;

}

} catch (IOException e) {

System.out.println("**** Exception in read in client " + id + " and value value was: " + value);

if(bytesRead == (Client.FILE_SIZE/Client.NUM_STREAMS)){

System.out.println("************ NOT ACTUALLY BAD" + id);

}

e.printStackTrace();

}

//Finished download

Client.workerDone();

System.out.println("Worker " + id + " done received " + bytesRead);什么导致这些例外?

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值