I am using TCP sockets to communicate data between a server and client program using a specific port number on the same computer (localhost).
I need a software that can capture the data being sent/received through that socket?
(or)
What's the simplest way of sniffing packets from a specified port in Java?
If you are up to some coding (and not just running the wireshark/tcpdump) then you have few choices. If you want stick to Java, then the only (?) option to use raw sockets is via JNI and there are few libraries that can help, for example:
Tcpdump
can also be used directly, if the volume of traffic is not high, obviating the need to usewireshark
. Just something likeshould be all you need (lo0 is the loopback interface on all Unix/Linux systems; also change the port number of course).
I suggest using Wireshark. It's easy to use and runs on many platforms.
http://www.wireshark.org/
The easiest way is to replace the
InputStream
/OutputStream
from a socket in one of the programs with a proxy implementation, that either prints/logs or "tees" to the original and a print/log stream.But there's plenty of sniffers out there if you really want to get messy.
If you don't mind getting down and dirty with the command line you could try netcat. It'll let you listen on a port and dump the output to a file if you like.
You can also make it send fake data and record the response.
I often use it as a pretend HTTP proxy (and configure Firefox to use it) to discover what is being sent over the wire.
Run your program like so:
java -Djavax.net.debug=all helloworld.java
The switch is fully documented on the Oracle JSSE page
Pros:
Cons:
java.net.*
e.g.OutputStream
orInputStream
This will not work if you're using raw sockets. This is documented in the link.