1

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.

Eric Darchis
  • 24,537
  • 4
  • 28
  • 49
frogcd
  • 61
  • 1
  • 9
  • 2
    Please refer to this question/answer on SO [reading-and-writing-tcp-header-options-in-java](http://stackoverflow.com/questions/14267923/reading-and-writing-tcp-header-options-in-java) Hope it helps. – João Rebelo Nov 18 '16 at 13:21

1 Answers1

2

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.

Community
  • 1
  • 1
Kevin G.
  • 388
  • 1
  • 6
  • From Java/Android description, it's truly unable to obtain stripped header. Thanks. – frogcd Nov 18 '16 at 14:15
  • I've just seen that you intend to use it on Android. The library might not be able to work on Android because of the architecture of the system. – Kevin G. Nov 18 '16 at 14:18
  • I would prefer to get header info using C, which is faster than Java to take care of memory and pointers. – frogcd Nov 18 '16 at 15:12
  • @frogcd have you found any way to read tcp header in android without root privilege? – Al-Alamin Dec 03 '17 at 06:53