0

I have been experiencing this issue for a while now. A quick explanation of the system:

A simple application will read in data over a tcp connection. The application uses Socketchannel object to establish a data communication line. The connection to hardware is established, and the application processes roughly between 400 - 700 packets before throwing the error:

at sun.nio.ch.SocketDispatcher.read0(Native Method) at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43) at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:218) at sun.nio.ch.IOUtil.read(IOUtil.java:186) at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:359)

I suspect it has to do with the operating system (windows 7) buffer, although I have tried a few work arounds (increasing JVM heap size, and creating additional registry variables for TCP settings), but with no luck.

Inspecting packet data using wireshark, the following information can been seen just as the error occurs

[TCP Window Full] 5001 > 49995 [PSH, ACK] Seq=123177 Ack=1 Win=200 Len=176
[TCP ZeroWindow] 49995 > 5001 [ACK] Seq=1 Ack=123353 Win=0 Len=0

Which is then followed by a series of

[TCP Dup ACK 144#1] 5001 > 49995 [ACK] Seq=123353 Ack=1 Win=200 Len=0

until the connection is terminated.

I am hoping perhaps someone has had a similar issue in the past,or could offer some guidance on something I may be overlooking.

Here is sample code where data is read, and the error is thrown.

    try{
            do{
                socketChannel.read(syncBuf); 
                start = syncBuf.get(0);
                int b = start;
                System.out.println("start byte is " + b);
                syncBuf.clear(); 
            }while(start != INIT_BYTE);
        }catch(Exception e){      
            e.printStackTrace();
        }

        packetBuf.order(ByteOrder.LITTLE_ENDIAN);
        packetBuf.clear();

        try{
            while(socketChannel.read(packetBuf) != 206){
                nrBytesRead += socketChannel.read(packetBuf);
            }
        }catch(Exception e){
            ApplicationLogger.log("Error at INIT_BYTE loop ", "ERROR");
        }

Many thanks.

Php Pete
  • 762
  • 3
  • 14
  • 29

1 Answers1

0

I think you have a problem here:

while(socketChannel.read(packetBuf) != 206)
{
    nrBytesRead += socketChannel.read(packetBuf);
}

socketChannel.read(packetBuf) will return the number read this time, not the total, and I guess that's what 206 is, the total read.

You probably want to do that:

do
{
    nrBytesRead += socketChannel.read(packetBuf);
}
while (nrBytesRead != 206)
Djon
  • 2,230
  • 14
  • 19