Java Port Listening

I'm trying to create a program that will listen on a port for any packets coming through.. see, this other server program already accesses the same port (to send and receive packets).. I just want to listen to the port for any packets coming into the the other program.. then my program will do some work, but it never sends out through the port..

I've already written a few port accessing programs.. I did an example HTTPD Server, and UDP Heartbeat & Pulse Server.. so I've got the basics down.. but when I try to access the port in this current program, it gives me the error "Address in use: Cannot bind".. meaning the other server is using it, but I'm just interested in "listening" to the port, thats all.. is there some other way around this problem?.. this is what I have so far.. it just listens on the port and outputs any data that comes in (in theory)..



import java.util.*;

public class QStats {

public static void main( String [] argv ) throws IOException {

DatagramSocket s = new DatagramSocket( Integer.parseInt( argv[0] ) );

while( true ) {

DatagramPacket packet = new DatagramPacket( new byte [1024], 1024 );

s.receive( packet );

String message = new String( packet.getData() );

System.out.println( message.trim() );




I do believe they are UDP Packets, I tried TCP/IP, I didn't get anything.. if anyone has any suggestions as to just "listening" on a port thats being used by another program, that'd be great.. Thanks!


  • Hi!

    I posted a little socket tutorial on my homepage.

    Try the section Resources/Tutorials. Here you can download a

    little TCP/IP server and client. Sourcecode is available.

    The link is added below.



  • : I posted a little socket tutorial on my homepage.

    : Try the section Resources/Tutorials. Here you can download a

    : little TCP/IP server and client. Sourcecode is available.

    Thanks for the info.. I know how to do some basic TCP/IP and UDP client/servers.. but in my current project, I need to listen on a port that another program is also listening on..

    ok.. here's the run down.. I know there are other programs out there that do this already.. but I still want to write one for myself.. we all know the game Quake.. my friend is going to be putting up a dedicated Quake server soon.. I wanted to write an app that would sniff the socket the Quake server listens on and read the packets as they come through.. I'd take the packets apart and keep a running statistic on the players using the server.. kill ratios, total number of deaths, total number of frags, overall rankings.. stuff like that..

    but to do that, I need to just listen on the same port that Quake does.. now, I know its using TCP/IP now.. and I can get MY listen server running on the port at the same time that the Quake server is running.. no problem.. but when the packets come in, they go to the Quake server and my listen server never even sees it.. even though its listening on the same port.. any ideas here?

  • In the same computer you cant start two serversockets for the same port.

    You have no choice. So you cant listen for a port twice in the same PC (quake server and your server).

    If you replace the quake server with yours you can have security problems. Probably the quake server

    wait for some control messages and he reply with some control messages too. If the quake-client or server

    don't get this security messages they cut the line.



  • well.. what if I put the quake server behind my stats server.. had everyone who wanted to connect to the quake server connect to my stats server.. then any packets that come into that socket I forward on to my Quake server.. then it sends back out over the socket it received the forwarded packets through..

  • This solution sounds well.

    Try it.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!