Solved

java  - null DatagramSocket in RTS server puzzling me

Posted on 2014-03-18
8
411 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 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
Industry Leaders: 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 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

733 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