what am I doing wrong? (newbie- please help)

hello.

I am making a JOGL networked aplication, and here is what I am trying to do:

I have a few JSliders, that controls the atributes of a JOGL 3D object( a cube). So, if the user changes the position of the JSlider, it must send this information to all the other users, and then get the atributes changed.

here is what I done, but it isn]t working:

on the contructor:

try {
                socket = new DatagramSocket(5000);
            }

        catch(SocketException socketException) {
        socketException.printStackTrace();
        }

on the state changed metod of the SliderHandler class (couse I olnly have to send informations when someone chage the atributes value)

try{

                byte sendData[] = pacoteString.getBytes();

                sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getLocalHost(),5000);
                socket.send(sendPacket);


                byte reciveData[] = new byte[100];
                receivePacket =new DatagramPacket( reciveData, reciveData.length);

                socket.receive(receivePacket);

                pacoteString = new String(receivePacket.getData(), 0, receivePacket.getLength());

                token = new StringTokenizer(pacoteString);

                flag = Integer.parseInt(token.nextToken());

                valor = Float.parseFloat(token.nextToken());
                System.out.println(valor);


                if(flag == ROTACAO_X_CUBO)
                   renderer.setrotacaoCuboX(valor);

                else if (flag == ROTACAO_Y_CUBO)
                     renderer.setrotacaoCuboY(valor);

                else if (flag == ROTACAO_Z_CUBO)
                     renderer.setrotacaoCuboZ(valor);

                else if (flag == TAMANHO_X_CUBO)
                     renderer.settamanhoCuboX(valor);

                else if (flag == TAMANHO_Y_CUBO)
                     renderer.settamanhoCuboY(valor);

                else if (flag == TAMANHO_Z_CUBO)
                     renderer.settamanhoCuboZ(valor);

                else if (flag == POSICAO_X_CUBO)
                     renderer.setposicaoCuboX(valor);

                else if (flag == POSICAO_Y_CUBO)
                     renderer.setposicaoCuboY(valor);

                else if (flag == POSICAO_Z_CUBO)
                     renderer.setposicaoCuboZ(valor);

                else if (flag == COR_VERMELHO_CUBO)
                     renderer.setcorCuboVermelho(valor);

                else if (flag == COR_VERDE_CUBO)
                     renderer.setcorCuboVerde(valor);

                else if (flag == COR_AZUL_CUBO)
                     renderer.setcorCuboAzul(valor);
                }

            catch (IOException ioException){

                  ioException.printStackTrace();
                  }
        }

Should I made a diferent code for the client and the server? Couse the way it is now, it’s all the same.
What is happning is that it’s only sending the packet to itself, like there isn’t any other user runing the program.
I don’t have much experience, so any help would be very apreciated.
thx,

uderman

You are only sending the packet to yourself! :stuck_out_tongue:

socket = new DatagramSocket(5000); 

This is opening port 5000.

sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getLocalHost(),5000); 

This is sending to THIS machine at port 5000! The IP you are passing the function needs to be the destination IP you need.

You should use a different socket for the server than the clients - you will have all sorts of issues if 2 apps try to bind to the same socket.

In fact, the clients port should NOT be set - it will be allocated a free one (allowing you to run multiple clients on the same machine)
For example, use 5000 for the server, and let the client set its own:

Client code:


// open our own socket
socket = new DatagramSocket();

...blah blah ...

// Send data to SERVER
sendPacket = new DatagramPacket(sendData, sendData.length, <SERVER IP HERE>, 5000); 

And on the server:


// open our own socket
socket = new DatagramSocket(5000);

...blah blah ...

// Send data to SERVER
for(int i=0; i<nClients; i++)
{
    sendPacket = new DatagramPacket(sendData, sendData.length, <CLIENT[i] IP HERE>, <CLIENT[i] PORT No. HERE>); 
    socket.send(sendPacket); 

When the client logs in, you should record the IP and socket the client has sent the message with. After login, you should always check the IP matches the login IP from each client (to prevent spoofing).

Note also that NAT firewalls may change the port ID at any point, so whenever you receive a message (& have verified it is genuine) you should copy the port ID from the message again.

Hope this helps,

Dom