views:

801

answers:

5

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?

+4  A: 

I suggest using Wireshark. It's easy to use and runs on many platforms.

http://www.wireshark.org/

Lennart
Unfortunately, it won't work when client and server on the same machine.
Gennady Shumakher
It will. Just select the loopback interface for capturing.
Lennart
http://wiki.wireshark.org/CaptureSetup/Loopback (section 'Alternatives') - an alternative is to add a route to your local machine going through the network gateway
mjustin
+1  A: 

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.

Tom Hawtin - tackline
+1  A: 

Wireshark is great. You might also try DonsProxy.

Don Branson
+1  A: 

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.

Darren Greaves
+1  A: 

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:

  • jNetPcap - wrapper around the native libpcap/winpcap libraries, exposing all of their functions and structures
  • RockSaw - API for using raw sockets
Anonymous