I'm making a UDP client/server program. The client collects data and sends it to the server.
The client sends a datagram with a random port number to the server on a specific UDP port 6789. The client and server then use the random port to share information.
The problem I have is that only one client can connect. I feel that it is because port 6789 never gets reopened for the next client.
This is the code just for the server receiving the initial connection on port 6789.
public class MonitorServer extends Thread{
public static void main(String[] args) {
try{
DatagramSocket connectSocket = new DatagramSocket(6789);
while( true )
{
String portString = "";
int portInt = 0;
byte [] buffer = new byte[100];
System.out.println("portTest 01 " +portInt);
DatagramPacket portPacket = new DatagramPacket(buffer, buffer.length);
connectSocket.receive(portPacket);
portString = new String (portPacket.getData());
connectSocket.disconnect();
connectSocket.close();
System.out.println("portTest 02 " +portString);
portString = portString.replaceAll("[^0-9.,]+","");
portInt = Integer.parseInt(portString);
System.out.println("portTest 03 " +portString);
ClientConnection c = new ClientConnection(portInt);
new Thread(c).start();
System.out.println("portTest 04 " +portString); //<<-- never prints
}
}
catch (Exception e) {}
}
}
It seems like this section of code only runs once. I added System.outs to troubleshoot and it looks like it stops at:
ClientConnection c = new ClientConnection(portInt);
new Thread(c).start();
System.out.println("portTest 04 "); //<-- Never prints
I need it to keep running to accept connections. What am I doing wrong?