Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  what am I doing wrong? (newbie- please help)  (Read 990 times)
0 Members and 1 Guest are viewing this topic.
Offline uderman

Senior Newbie




Java games rock!


« Posted 2004-10-18 19:33:36 »

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:

1  
2  
3  
4  
5  
6  
7  
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)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
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
Offline crystalsquid

Junior Member




... Boing ...


« Reply #1 - Posted 2004-10-21 17:09:27 »

You are only sending the packet to yourself! Tongue

1  
socket = new DatagramSocket(5000); 

This is opening port 5000.

1  
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:
1  
2  
3  
4  
5  
6  
7  
// 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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
// 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
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (26 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (34 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!