Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: NAT Punch-Through with UDP

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Thanked 0 Times in 0 Posts

    Default NAT Punch-Through with UDP


    I'm trying to implement a NAT Punch-Through system where in clients can connect to each other without users needing to port forward. However, I've come across some problems with which I'm hoping someone may be able to help me with. I'll try my best to describe the situation and problem.

    Essentially, I have two java applications; a server and a client. In order to test the system, I run two instances of the client and one of the server. The client can run in one of two modes, as it can either 'host' a session or 'join' a session. When testing the system, I have one client host and one join. What I'm attempting to do is have the server 'introduce' the two clients so that they can freely communicate without the server, but more importantly without the user having to do any port forwarding.

    So far, I've got to the stage where the clients can connect to the server, while the hosting client is able to upload its information to the server and this information is passed on to the second client when it connects. The server is able to freely communicate with the client, without the need of any port forwarding on the clients end. However, my problems begin when I attempt to connect from one client to another (using the same socket that was used to contact the server). Despite each client having one anothers information, the packets never seem to get through. What I'm doing is having the server obtain the IP/PORT for both clients using packet.getAddress() and packet.getPort(), but when the clients send packets to each other, they are never received.

    The second client attempting to receive the packet from the hosting client does so like this:
    DatagramPacket receiver = new DatagramPacket(new byte[1024],1024,this.host_ip,this.host_port);
    String ping = (new String(receiver.getData())).trim();

    While the hosting client sends the packet like this:
    send = "ping".getBytes();
    DatagramPacket sendPacket = new DatagramPacket(send,send.length,ip,port);

    I'm not really sure where I'm going wrong and I'd really appreciate any help available. Maybe there's something wrong in the architecture that I'm trying to implement, although to me it seems fine.

    Last edited by Aaronds; April 17th, 2011 at 07:23 AM.