frogcd frogcd - 10 months ago 75
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:


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 Source

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

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.