2011-08-02 60 views
0

我创建了一个ServerSocket并读取来自不同客户端的消息。其中一位客户坚持认为,我的应用程序在发送消息后行为不正确。我设法得到一个TCP跟踪,并看到他们真的发送了他们的消息,但是我的服务器应用程序没有从套接字读取任何数据。我得到一个线程转储,并且在那里一切似乎都正常:java - 无法从套接字读取

"Reader Thread" daemon prio=3 tid=0x0ae8a000 nid=0x999 runnable [0x14525000] 
    java.lang.Thread.State: RUNNABLE 
     at java.net.SocketInputStream.socketRead0(Native Method) 
     at java.net.SocketInputStream.read(SocketInputStream.java:129) 

什么原因导致无法从流中读取消息?其他客户不会遇到这种问题。

private byte[] readByte(int readCount) throws Exception { 
    int b; 
    int readedCount = readCount; 
    byte[] receiveBuffer = new byte[readCount]; 
    while(true) { 
     if(readCount > 0) { 
      b = inputStream.read(receiveBuffer, readCount - readedCount, readedCount); 
     } else { 
      b = 0; 
     } 
     if(b < 0) { 
      throw new UnrecoverableSocketException("Connection is Broken"); 
     } else { 
      readedCount = readedCount - b; 
     } 
     if(readedCount == 0) { 
      return receiveBuffer; 
     } 

    } 
} 
+0

顺便说一句,我可以在TCP转储中看到TCP ACK消息 –

回答

0

大部分readByte()方法都可以使用DataInputStream.readFully()来实现,并且您可以获得数百万次测试数量的好处。

1

可能与流中实际可用的数据量不匹配。如果您的客户端发送了10个字节,但您期望12您的代码会卡住等待这两个额外的字节。

+0

实际上,这是SMPP协议的实现。在这个协议中,客户端必须发送预定义的字节数,这是头信息。这个头告诉我们整个消息包含的字节数。所以应用程序试图读取剩余的字节。我使用wireshark检查了TCP转储,消息似乎是正确的。 –