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
Solved

java  - null DatagramSocket in RTS server puzzling me

Posted on 2014-03-18
8
410 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 17 51
Problem to Alipay 10 42
jsp CRUD operations with and without prepared statement also hibernatge 1 8
junit as external jar or library 7 22
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

808 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