J
James Kimble
I'm trying to sniff for local port traffic on a Linux machine. I have
an application that uses UDP to communicate with it's various
distributed parts and I want to view the packets in order to replace a
part of this thing with something of my own. (no nothing sinister,
boring, but not sinister).
I can use tcpdump to view traffic by port number on the local host. I
need to be able to capture the packet data and manipulate it and for
that I need something better. I wrote a simple java program (below)
that creates a socket and a datagram and tries to start receiving on a
port. However I always get a "BindException: Address already in use
occured" error. Isn't there some way to just listen to traffic without
actually binding to the port and interfering with traffic (like
tcpdump)? Any help would be much appreciated....
My current program is simply:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.net.*;
import java.util.*;
public class PortListener
{
protected static DatagramSocket socket = null;
public static void main ( String args[] )
{
if ( args.length != 1 )
{
System.out.println ("\nUsage: java PortListener <port
number>\n");
System.exit(1);
}
int data_port = new Integer(args[0]).intValue();
try
{
socket = new DatagramSocket(data_port);
}
catch (SocketException se)
{
System.out.println ("Socket exception: " + se + "
occured\n" );
}
catch (IOException ioe)
{
System.out.println ("IO exception: " + ioe + " occured\n"
);
}
while (true)
{
System.out.println ("\nListening to " + data_port + "\n");
try
{
byte[] buf = new byte[256];
// Receive request
DatagramPacket packet = new DatagramPacket(buf,
buf.length);
socket.receive(packet);
// Get the client at "address" and "port"
InetAddress address = packet.getAddress();
int port = packet.getPort();
System.out.println ( "\nWe recieved packet from address
" +
address + " on port " + port +
"\n" );
}
catch (IOException e)
{
e.printStackTrace();
}
}
socket.close();
}
}
an application that uses UDP to communicate with it's various
distributed parts and I want to view the packets in order to replace a
part of this thing with something of my own. (no nothing sinister,
boring, but not sinister).
I can use tcpdump to view traffic by port number on the local host. I
need to be able to capture the packet data and manipulate it and for
that I need something better. I wrote a simple java program (below)
that creates a socket and a datagram and tries to start receiving on a
port. However I always get a "BindException: Address already in use
occured" error. Isn't there some way to just listen to traffic without
actually binding to the port and interfering with traffic (like
tcpdump)? Any help would be much appreciated....
My current program is simply:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.net.*;
import java.util.*;
public class PortListener
{
protected static DatagramSocket socket = null;
public static void main ( String args[] )
{
if ( args.length != 1 )
{
System.out.println ("\nUsage: java PortListener <port
number>\n");
System.exit(1);
}
int data_port = new Integer(args[0]).intValue();
try
{
socket = new DatagramSocket(data_port);
}
catch (SocketException se)
{
System.out.println ("Socket exception: " + se + "
occured\n" );
}
catch (IOException ioe)
{
System.out.println ("IO exception: " + ioe + " occured\n"
);
}
while (true)
{
System.out.println ("\nListening to " + data_port + "\n");
try
{
byte[] buf = new byte[256];
// Receive request
DatagramPacket packet = new DatagramPacket(buf,
buf.length);
socket.receive(packet);
// Get the client at "address" and "port"
InetAddress address = packet.getAddress();
int port = packet.getPort();
System.out.println ( "\nWe recieved packet from address
" +
address + " on port " + port +
"\n" );
}
catch (IOException e)
{
e.printStackTrace();
}
}
socket.close();
}
}