Java-Gaming.org Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (654)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Networking & Multiplayer / Re: Java UDP - Client doesn't receive DatagramPacket without forwarding ports on: 2015-05-18 18:59:48
Thanks for your answers!

It's working fully fine after editing the code with KaiHH's latest answer in mind. I never used .getPort() which seemed to be the cause of the issue.
I'm gonna keep on working on the code to improve some features/logic/performance, and i really think i got a good helping hand in starting out.

I only got a little side thought/question..

Right now the client side looks like this:
> 1 DatagramSocket
> 1 Thread for sending
> 1 Thread for receiving

And the host looks like this:
> 1 DatagramSocket
> X Thread(s) for sending (depending on how many clients there are connected)
> 1 Thread for receiving

I get the feeling that the host won't be able to receive messages from all clients at the same time, so my current plan is doing something like this for the host:
> 1 DatagramSocket (a)
> 1 Thread for receiving incoming/initial connections from socket (a)
When a client is accepted, create two threads for sending(below) and receiving on another port, then send back a message to the client (from socket (a)) with the "new" port to start sending to.
> X Thread(s) for sending (depending on how many clients there are connected, on another port than socket (a))
> X Thread(s) for receiving (on another port than socket (a))

Would i be wrong in thinking this way? Is it fine to have 1 single socket for receiving data from multiple clients at the same time?
If socket.receive(); hasn't been called the incoming messages would just disappear right? Or is there some kind of automatic "queue" for incoming data that the socket can later .receive()?
I just want to be sure that i'm not committing some horrible sin.

Huge thanks, and best regards!
2  Game Development / Networking & Multiplayer / Re: Java UDP - Client doesn't receive DatagramPacket without forwarding ports on: 2015-05-18 15:17:58
Huge thanks for the detailed answer, i rewrote the code a bunch based on this.
I'm still getting the same issue that CLIENT can reach the HOST, but HOST can't reach the CLIENT.

Just to be perfectly clear on what i'm currently doing, in case i misunderstood something important:
> I'm running a single DatagramSocket for HOST on port 1234 (for both sending/receiving messages)
> When connecting as CLIENT i create two threads. One for sending and one for receiving. These threads use the same single DatagramSocket which is bound to the same port as the host (1234)

So all in all, HOST gets the initial message from the client on port 1234. HOST then sends back a message to CLIENT on port 1234 using the same DatagramSocket the HOST received on.
The initial message comes from CLIENT on port 1234 to HOST on port 1234. HOST sends back using the same socket it received on towards the same socket CLIENT sent on (port 1234) but the CLIENT still never receives anything. Is there something crucial i've missed from your last reply?
3  Game Development / Networking & Multiplayer / Java UDP - Client doesn't receive DatagramPacket without forwarding ports on: 2015-05-16 23:59:48
Hello there!
I am making a 2D game engine in pure Java in which includes a built-in network/multiplayer feature. The network feature is self-hosted by a player, where other players/clients can connect to the host. The network feature is UDP based because of the importance of net speed. Another note is that i don't want any external libraries involved in this. I want to do it purely with what the standard Java library has to offer.

I don't want the clients/end-users having to manually forward any ports in order to make this work. The host should only need to do that at most.

Things are working out perfectly when testing locally between two running applications on the same computers. But the issue begins when trying it out with other people from around the world.

This is a rundown of how things are going down when trying it out:

Quote
> HOST opens a DatagramSocket on port 1234, waiting for CLIENT to send a message (this is used to accept clients and create "Connections" with new threads for each client)
> CLIENT opens a DatagramSocket on port X (automatically assigned to a free one)
> CLIENT creates two threads using the same DatagramSocket. One for listening and one for sending
> CLIENT sends a message to HOST
> HOST receives message (where it also gets the IP/port of CLIENT)
> HOST creates two threads using the same DatagramSocket. One for listening and one for sending ()
> HOST sends message to CLIENT
> .........
> CLIENT never receives message

I have confirmed that the IP/port for sending/receiving between the client and host are correct. There is no one sending a message to the wrong IP/port.

I can send as many messages i like without any change. The host has port forwarded port 1234 for incoming packets. The client hasn't port forwarded anything in the example up above.

Just to make sure this one time i asked one of the clients/testers to forward a port, which made it work just fine. So the issue is definitely related to the ports.

I tried doing TCP instead of UDP, using Socket instead of DatagramSocket which made it work just fine as long as the host had the port forwarded to begin with. The only backside was that it became practically unplayable due to slow network speed, so i am desperately looking for a solution to this UDP issue.

I also tried to use both TCP and UDP, using the same TCP Socket port for the UDP DatagramSocket port. Where i first established a connection using a TCP Socket just to later create a UDP DatagramSocket for sending/receiving on the same port. This did not work either.

Connection class (inside Network.java):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
class Connection
{
  String NAME;
  InetAddress IP;
  int PORT;    
  DatagramSocket DSOCKET;
  boolean ENABLED = true;

  public Connection(InetAddress ip, int port,String name)
  {
    IP = ip;
    PORT = port;
    NAME = name;
    try
    {
      DSOCKET = new DatagramSocket();
      receive();
      send();
    }catch(Exception e){}
  }
  public void send()
  {
    Thread sendthread = new Thread()
    {
      public void run()
      {
        try
        {
          byte[] sendData;
          while(ENABLED)
          {
              try
              {
                  String message = "Hello there!";
                  sendData = new byte[message.getBytes().length];
                  sendData = message.getBytes();
                  DatagramPacket sendPacket = new DatagramPacket(sendData,sendData.length,IP,PORT);
                  DSOCKET.send(sendPacket);
              }catch(Exception e){System.out.println("NetSendError: " + e);}
          }
          DSOCKET.close();
        }catch(Exception e){}
      }  
    };
    sendthread.start();
  }
  public void receive()
  {
    Thread thread = new Thread()
    {
      public void run()
      {
        byte[] receiveData;
        try
        {
          DSOCKET.setSoTimeout(1000);
          while(ENABLED)
          {
            receiveData = new byte[4096];
            DatagramPacket receivePacket = new DatagramPacket(receiveData,receiveData.length);
            DSOCKET.receive(receivePacket);
            String message = new String(receivePacket.getData());
            System.out.println("CLIENT received message: " + message);
          }
        }catch(Exception e){}
      }
    };
    thread.start();
  }
}


Network.java:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
import java.util.Vector;
import java.net.*;

public class Network
{
  boolean HOSTING = false;
  public static Vector<Connection> CONNECTIONS = new Vector<Connection>();
 
  public static void main(String[] args)
  {
    starthost();
    try
    {
      InetAddress ip = InetAddress.getByName("193.150.248.134");
      connect(ip,1234);
    }catch(Exception e){}
  }

  //called by host when starting a hosted session
  public static void starthost()
  {
    boolean HOSTING = true;
    Thread thread = new Thread()
    {
      public void run()
      {
        try
        {
          DatagramSocket socket = new DatagramSocket(1234);
          byte[] receiveData;
          try
          {
            while(HOSTING)
            {
              try
              {
                receiveData = new byte[4096];
                DatagramPacket receivePacket = new DatagramPacket(receiveData,receiveData.length);
                socket.receive(receivePacket);
                int ii = CONNECTIONS.size();
                boolean exists = false;
                for(int i=0;i<ii;i++)
                {
                  if(CONNECTIONS.get(i).IP.equals(receivePacket.getAddress()))
                  {
                    exists = true;
                    break;
                  }
                }
                if(!exists)
                {
                  //if this is the first time the client sends, create a connection for sending
                  CONNECTIONS.add(new Connection(receivePacket.getAddress(),1234,"HOST"));
                }
                else
                {
                  //if the client exists in the CONNECTION list, print out the received message
                  String message = new String(receivePacket.getData());
                  System.out.println("HOST received message: " + message);
                }
              }catch(Exception e){System.out.println("NetStartHostError: " + e);}
            }
            socket.close();
          }catch(Exception ee){}
        }catch(Exception e){}
      }
    };
    thread.start();
  }

  //called by clients when connecting to host
  public static void connect(InetAddress ip,int port)
  {
    CONNECTIONS.add(new Connection(ip,port,"CLIENT"));
  }
}


With all of this, my following questions are:

Quote
- Am i doing something wrong in the code up above?
- Is there a way of working around this? (without any external libraries)
- Should i be using the same DatagramSocket for sending and receiving? Does it make a difference?
- Is there just no way around this? Does every client needs to port forward manually? If there isn't, is there a "pure-java" way of port forwarding by code?
- Why does it work with TCP and not UDP under the same circumstances (host has forwarded ports, client hasn't)
- When a client sends a DatagramPacket from a port, shouldn't it be able to receive packets from the same port?

I'm kinda considering giving up on the whole UDP thing and going TCP-only since that actually works. Maybe there's some methods of speeding it up significantly?
After more than a week of searching and testing i'm finally creating a thread here. I'd really appreciate some help if there is any available.

Best regards!
4  Games Center / Showcase / PlatPong on: 2013-12-07 06:53:28
Hey there, i'm pretty new to the whole game dev thing and would like to show off one of my games.
This little bugger right here is called.. PlatPong!



It's pretty much what you would expect of a basic pong game, though i added a neat little bunch of special effects and sounds.
I also added a little brick breaker segment which respawns after a while, as you can use them to build up some crazy combos! (which usually results in me narrating stuff in a desperate attempt to sound like the unreal tournament announcer  Roll Eyes)

Things can get pretty fast after a while when the pace picks up.
After a smaller while into the game, "stars" will appear which you can hit in order to get extra points and build up a meaner combo.
You can also play against the AI or your most beloved friend on the same computer, as your high-score will automatically save.



Controls: W,S and/or UP,DOWN arrow for movement. ENTER/SPACE in order to continue/select.
I also made the game by using the awesome libGDX library, damn it's good!

Link to the game: https://dl.dropboxusercontent.com/u/13123326/PlatPong.zip

Have fun! And don't be too hard on me.  Wink
Pages: [1]
 
bilznatch (27 views)
2015-08-04 11:03:17

SHC (44 views)
2015-08-01 03:58:20

Jesse (25 views)
2015-07-29 04:35:27

Riven (48 views)
2015-07-27 16:38:00

Riven (26 views)
2015-07-27 15:35:20

Riven (28 views)
2015-07-27 12:26:13

Riven (19 views)
2015-07-27 12:23:39

BurntPizza (42 views)
2015-07-25 00:14:37

BurntPizza (56 views)
2015-07-24 22:06:39

BurntPizza (35 views)
2015-07-24 06:06:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!