frogcd frogcd - 15 days ago 13
Android Question

How to obtain TCP header from socket's inputStream() in Android?

Android/Java provides socket APIs for receiving data from TCP server, like:

socket.getInputStream().read(payload);


The payload is data stripped off TCP header as well as IP header. My question is that possible to get the TCP header? Thanks.

Answer

You can't access TCP headers as explained here: Reading and writing TCP header (options) in Java

The thing is that without monitoring your network, you won't be able to access that information from the Java API.

What you might want to try is looking at this library if you really need the TCP headers http://jnetpcap.com.

This library is able to read the packets in real time. You would just have to match the packet you received with your socket in Java to the correct packet received by the library.

Comments