Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and writing TCP header (options) in Java

What I'm trying to do is: Writing client and server for sending a String via TCP with Java, so far no problem using Socket and Input-/Output-Streams.

But now I'm trying to add 3 numbers to the "Options" field in the tcp header enter image description here. I can't find any helpful tutorials or something else on how to customize the header and how to read it.

Any suggestions?

like image 920
Grt Avatar asked Jan 10 '13 21:01

Grt


2 Answers

You can't:

  • at application level, you do not have access to the TCP headers;
  • Java does not have raw sockets either.

You can somewhat influence some of the TCP behavior by setting different socket options, though. But not manipulating TCP headers directly.

like image 91
fge Avatar answered Sep 19 '22 00:09

fge


Not without utilizing external library like jNetPcap. jNetPcap does allow you to format and send your own packet.

jNetPcap Installation Guide (Eclipse)

jNetPcap Installation Guide (NetBeans)

A very rough sample code for formatting and sending packet. Rough it may be, but it shows you the possibilities to customize the packet.

Edit: Forgot to mention that jNetPcap is a Java wrapper for libpcap. They are meant to be utilized to read the packet in the first place.

like image 29
David J. Y. Tang Avatar answered Sep 21 '22 00:09

David J. Y. Tang