我有一个客户端服务器应用程序,它有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);什么导致这些例外?