Solved

java  - null DatagramSocket in RTS server puzzling me

Posted on 2014-03-18
8
398 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
noX challenge 17 76
java set up 1 46
Non-recursive backtracking, using a stack 1 48
maven example error 3 35
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
As we all know Counter Strike is a very popular computer game. Usually it is played over a network for which people create a server and users join it but it is interesting to know that one can creates a dedicated server which not only hosts the game…
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:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

707 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

15 Experts available now in Live!

Get 1:1 Help Now