简体   繁体   中英

Read data from tcp socket published by another tool

One tool is pushing data to a port and java socket should read it. I check if the data is received using Netcat nc -lv ip port

And my java code to read the data is as follows:

ServerSocket server = new ServerSocket(port);
System.out.println("Server started"); 
System.out.println("Waiting for a client ..."); 

Socket socket = server.accept(); 
System.out.println("Client accepted"); 

// takes input from the client socket 
DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); 
while (true) {
     String line = in.readUTF(); 
     System.out.println(line); 
}

The programs waits forever for the requests, though Netcat command shows data. Any help is greatly appreciated.

Thanks in advance

Shakti

Ideally server.accept(); call will be waiting for some request on that port. Server.accept() should be used in the case of server side, where you will be listening on some port and the clients will make request to that port.

In your case you should read from a socket instead of creating a server socket.

You can refer to the below links to solve your purpose

Read Data from a Java Socket

https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html

The contract of DataInput.readUTF() is that the length of the string to be read are contained in the first two bytes:

First, two bytes are read and used to construct an unsigned 16-bit integer in exactly the manner of the readUnsignedShort method . This integer value is called the UTF length and specifies the number of additional bytes to be read .

If your other tool is not encoding the UTF data string in exactly the same way, it is very likely that your server will mis-interpret the first bytes of data sent as the length of data to read, and simply be hanging waiting for the remaining data to arrive.

For example, lets say your tool sends the string 'Hello World', encoded in ASCII . In hex, that is

48 65 6C 6C 6F 20 57 6F 72 6C 64

Your server receives the data, and the DataInput class starts to decode it as UTF8 according to the contract. The first two bytes are read as an unsigned short:

utf_length = (((48 & 0xff) << 8) | (65 & 0xff))

This yields a length of 18533 bytes. Once the remaining 9 bytes of the stream are read, the server will simply block waiting for the remaining 18524 bytes to arrive, as that is what it is expecting. My guess is this is the reason why your program simply hangs.

Since you do not say what the client tool is, or how it encodes the data you are reading, it is hard to know if this is the cause or not.

If this is not the correct answer, please update your question with additional detail, or even some test data which might help future visitors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM