Solved

java  - null DatagramSocket in RTS server puzzling me

Posted on 2014-03-18
8
417 Views
Last Modified: 2014-03-23
Hi

I am back to my RTS in Java. I was persuaded against using Python. I am doing initial client-server connections in TCP Sockets for reliability. Once a client notifies the server in TCP, a serverClient class is instantiated on the server to mange the communication between that client and the server. I have incorporated your advice as much as possible.

It is intended for each serverClient to communicate with the actual client over UDP.
For some baffling reason, the DatagramSocket on the serverClient never gets made, in serverclient.java , line 31. Its port is -1 - in the printf
I cant proceed until that is fixed.

The code is below. The server is on its own machine, along with its serverClients that it makes, and the DRWclient clients are on their own machines.

Any pointers?

Thanks
DRWserver.java
serverClient.java
DRWclient.java
0
Comment
Question by:beavoid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 36

Expert Comment

by:mccarl
ID: 39938924
For some baffling reason, the DatagramSocket on the serverClient never gets made, in serverclient.java , line 31
It's not so baffling, because it IS actually getting made, the fact that you can call .getPort() on it in the print statement means that the object is getting made.

However, you are creating the DatagramSocket "unbound" and I'm guessing what you actually mean to do is something like this...
UDPsocket = new DatagramSocket(UDPPORT);

Open in new window

ie. you have to pass the port to it that it should be listening on.
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 39939241
Why have you only got a DatagramSocket on one side of the connection?
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 39940251
getPort() returns a port only if the UDP socket is connected. What you are looking for most likely is

.getLocalSocketAddress()

which will return the port as part of the SocketAddress field.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
krakatoa earned 500 total points
ID: 39940495
This illustrates how things work with DatagramSockets:

import java.net.*;

class DatagramPeer  {
        
        private DatagramSocket au_Send;
        static DatagramPeer dgp;
        private int port = 30314; // pop this into the DatagramSocket constructor to see diff effect
        

        DatagramPeer() {
           

            try {

                au_Send = new DatagramSocket(); // like this, the System allocates a port.

	System.out.println("DatagramPeer's socket is at address : "+this.au_Send.getLocalSocketAddress());
	
            } catch (SocketException exSocket) {
                exSocket.printStackTrace();
            }
        }

        
public static void main(String[] args){

dgp = new DatagramPeer();
PeerClass pc = new PeerClass(dgp.au_Send.getLocalSocketAddress());

}


static class PeerClass{

private DatagramSocket dgs;

PeerClass(SocketAddress sa){
	try{
		dgs = new DatagramSocket();dgs.connect(sa);
		System.out.println("dgs is "+dgs.isConnected()+" connected.");
		System.out.println("This inner PeerClass's socket is NOW connected, on port "+dgs.getPort());
		System.out.println("The DatagramPeer's socket is NOT connected, and so returns "+dgp.au_Send.getPort()+" for the port.");
		dgp.au_Send.connect(dgs.getLocalSocketAddress());
	}

	catch(Exception ep){ep.printStackTrace();}

	System.out.println("PeerClass is connected to remote socket at : "+dgs.getRemoteSocketAddress());
	System.out.println("PeerClass's own local socket is at address : "+dgs.getLocalSocketAddress());
	System.out.println("But now DatagramPeer's socket is connected too, and so returns "+dgp.au_Send.getPort());
}

}

}

Open in new window

0
 

Author Comment

by:beavoid
ID: 39944364
I like what I see. Works well!
Thanks

but how much longer am I going to get away with programming this all on one machine ! !

!

It seems to work adequately with 2 clients. Will that very soon just become a disaster with  > 2 clients?
Can I do dev with 2 client engines on my PC; and the server on my Macbook? I can make this gameplay initially just a Jframe of 800x800, to move letters around, so that each window is in perfect synch . . until the network looks solid?

Thanks
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 39944735
That's a much broader question of course, and we - I at least - am not familiar with the rest of your model to pass any comment. But on the face of it, having another machine dedicated to serving sounds like the better option. Maybe others here can be more helpful on this subject.
0
 

Author Closing Comment

by:beavoid
ID: 39948200
Excellent!
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 39948430
:)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question