Solved

java  - null DatagramSocket in RTS server puzzling me

Posted on 2014-03-18
8
404 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
  • 5
  • 2
8 Comments
 
LVL 35

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 16

Expert Comment

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

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
 
LVL 16

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 16

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 16

Expert Comment

by:krakatoa
ID: 39948430
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now