我有一个客户端服务器应用程序,它有16个线程(8个客户端,8个服务器),每个都有1-1个匹配的TCP连接(所以8个TCP流)。
我让服务器发送X量的随机字节然后关闭。同时客户端只读取X数据然后关闭。 X是预定的。我也使用虚拟网络进行带宽限制,但我正在离开带有足够带宽(100Mbps)的管道。有时它工作正常,有时我得到这些例外。我在大约3.5分钟内在所有8个连接上均匀地传输1GB传播。
客户端抛出此异常:
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)
您的客户端永远不会停止阅读,因为您没有测试read()返回&lt; 0
答案 1 :(得分:-1)
以下是我在您的代码中看到的内容:
我已经确认,对socket.getOutputStream()
和socket.getInputStream()
的每次调用都会返回与套接字关联的不同的流。你应该每个插槽调用一次,并将结果放在一个局部变量中。
OutputStream outputStream = socket.getOutputStream();
// send the data
for (int i = 0; i < 10; i++) {
....
outputStream.write(...);
您应该从run()
方法返回许多地方,但我怀疑您知道这一点。
您没有检查read()
的返回值,看它是否在EOF上返回-1。您不应该只假设您获得正确的字节数。
我假设您正在编写空字符块,原因很简单。