I have a game with a UDP/TCP server and client. One UDP port (2406) for updating the client's location, and one TCP port (2407) for the chat. The problem here is at 2406.
When I play the clients in my local network, everything runs fine. But when an external client wants to join, I only receive the first package (the join command) and after that... nothing. I (logged in on local network) cannot see the external player. BUT they can see me. The chat works for both sides. So it's really related to the DatagramSocket. I'll try to post as much info as possible related to the UDP and not the TCP.
Anyone knows what the problem is here?
Ports are forwarded like UDP 2406, TCP 2407.
Server, sockets:
DatagramSocket socket = new DatagramSocket(2406, InetAddress.getLocalHost());
ServerSocket serversocket_chat = new ServerSocket(2407, 0, InetAddress.getLocalHost());
Server, receive thread:
byte[] buffer = new byte[1024];
DatagramPacket dp = new DatagramPacket(buffer, 1024);
while(true){
try{
this.socket.receive(dp);
String data = new String(dp.getData(), 0, dp.getLength()).trim();
String[] args = data.split(":");
String command = args[0];
String reply = null;
try{
reply = handleCommand(dp, command, args);
} catch( Exception e ){
System.err.println("Error while handling command: " + command);
e.printStackTrace();
}
if(reply != null){
reply += "\n";
DatagramPacket reply_packet = new DatagramPacket(reply.getBytes(), reply.length(), dp.getSocketAddress());
this.socket.send(reply_packet);
}
} catch (IOException e){
e.printStackTrace();
}
}
new Thread(chat_receive).start();
As soon as someone sends a message, the method handleCommand will find out what it is. Every message is a byte[] derived from a String. If the message is "cj:Hello", handleCommand finds command cs, username Hello. THIS is received by the server. After that, if that same person sends something, nothing will be received.
Client sockets:
private DatagramSocket socket;
private Socket socket_chat;
Client connecting:
this.socket = new DatagramSocket();
this.socket_chat = new Socket(ip, port+1);
Client sending:
private Runnable send = new Runnable() {
@Override
public void run() {
DatagramPacket dp;
String sendStringBuffered;
while(true){
if(sendString != null){
sendStringBuffered = sendString;
dp = new DatagramPacket(sendStringBuffered.getBytes(), sendStringBuffered.length(), ip, port);
try {
socket.send(dp);
} catch (IOException ex) {
Logger.getLogger(NewClient.class.getName()).log(Level.SEVERE, null, ex);
}
sendString = null;
}
}
}
};