Hello everybody,

Im using datagrams to send a DatagramPacket, however, unless the router has ports forwarded, the clients dont recieve the packets. Is there a way to set this up so that my clients dont have to port forward on their router?

How to main stream gams like LOL or WOW do it so no port forwarding is required?

Thanks for any help, below is code for sending / recieving packets, incase that will help.

package clientCode;

import java.io.*;
import java.net.*;


public class ClientListenThread extends Thread
{

    DatagramSocket clientSocket;

    public void run()
    {
        int testrecieved = 0;

        System.out.println("ClientListenThread is now running");
        try
        {
            clientSocket = new DatagramSocket(9877);
            byte[] receiveData = new byte[1024];
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            while (true)
            {

                System.out.println("Client waiting for a packet to come in");


                clientSocket.receive(receivePacket);
                String recievedPacket = new String(receivePacket.getData());
                testrecieved++;
                System.out.println("Total Packets Recieved from server so far: " + testrecieved);

                ClientPacketParser parse = new ClientPacketParser(recievedPacket);
                parse.start();
            }

        }
        catch (SocketException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        clientSocket.close();

    }

}




package serverCode;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import packet.Packet;

//this is just a reposity of the possible responses that the server will send to a connected client
//everything in / about this class should be referenced statically.
//this is the only place where the server sends information
public class ServerResponses
{
    //this is the servers confirmation response to an ID1 initial connection, it leaves the data null, and just tells the client
    //that connection was successfull
    public static void id1InitialConnection(InetAddress clientAddress)
    {
        System.out.println("Server accepting client: " + clientAddress);
        Packet response = new Packet ("server", 1, "");
        sendPacket (response, clientAddress);
    }
    //this is the servers failed response to an ID1 initial connection, it leaves the data null, and just tells the client
    //that connection was NOT sucessful
    public static void id2InitialConnection(InetAddress clientAddress)
    {
        System.out.println("Server Rejecting client");
        Packet response = new Packet ("server", 2, "");
        sendPacket (response, clientAddress);
    }



    // a method for sending a packet from the server to an ip. Might be modified
    // later to send it not based on ip but rather to a username for simplicity,
    public static void sendPacket(Packet sendingPacket, InetAddress ip)
    {
        try
        {

            DatagramSocket clientSocket = new DatagramSocket();
            byte[] sendData = new byte[1024];
            sendData = sendingPacket.getTransmission().getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ip, 9877);
            clientSocket.send(sendPacket);
            clientSocket.close();

        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
        catch (SocketException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

Recommended Answers

All 3 Replies

I found this UDP hole punching from googling. There is no example, so you need to read the algorithm and try to follow it. Still, there is a reason why the client needs to do port forwarding.

how can i see ouput f dis progrm..... in netbeans??

That's not a complete prograqm, it's just the interesting bits, so you can't run it.
ps
Many of our members have English as their second (or third...) language, so abbreviated language will be unreasonably difficult for them. Please keep to proper English.
DaniWeb Member Rules include:
"Do post in full-sentence English"
http://www.daniweb.com/community/rules

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.