   Java UDP Client Port Incrementing itself!
  Hey there im quite a noob to networking, im transmitting 8kb packets over a LAN for my own RTS game as part of a coursework. But im encountering an issue I have never heard of for some reason or other the port i recieve data on incrememnts every single time I recieve a packet. Im trying to debug an issue where when you get ingame the host keeps talking and the client keeps listening but no one seems to recieve any data with it being sent two and a half times a second.Whilst I was studying the issue I realised the packet p (shown below) on the client listening thread is being recieved on a different port every time it gets data...  int buffer_size = 8192;  byte buffer[] = new byte[buffer_size];  DatagramPacket p = new DatagramPacket(buffer, buffer.length);  //This code throws an error because you cant bind the port privately but still works with the port.  try{ dsRecieve = new DatagramSocket(lobbyPlayerDataRef.getPortNumber());  }  catch (SocketException se)  { lobbyKillListNetworkingThread = true; throw se;  }  try{ dsRecieve.setSoTimeout(1000); //Lets assume we are getting lots of data we dont want to reconnect to the port we do want //to repeat the below as fast as possible. while (lobbyKillListNetworkingThread == false) {dsRecieve.receive(p);//If the broadcasting occurs before this terminates we could send ourselves so we check//again packetsif (lobbyKillListNetworkingThread == false){    if (isHost == true)    {The packet p has a port value for where it came from after afew minutes of hosting the port states its port is 52144 both the client and host are setup to recieve on port 3120.The host has a very simple mechanism for sending all of their data in an 8kb file to overwrite the clients.  if (isHost == true)  { byte[] transmit; PlayerData tempPDPointer; synchronized(lobbyPlayerDataRef) {transmit = lobbyPlayerDataRef.convertObjectToPacketData();tempPDPointer = lobbyPlayerDataRef.getNextPlayer(); } //This isnt locked to a port. dsSender = new DatagramSocket(); while (tempPDPointer != null) {DatagramPacket packet = new DatagramPacket(transmit, transmit.length, InetAddress.getByName(tempPDPointer.getPlayerIP()), tempPDPointer.getPortNumber());dsSender.send(packet);tempPDPointer = tempPDPointer.getNextPlayer(); } dsSender.close();  }  else...Im terrified to find something that seems so crucial going on at this late stage in my project. Does anyone have any idea why the port is shifting on the data recieved?An 8kb file over a 100mbps lan even on UDP should be recieved correctly being broadcast as one bulk packet if its sent 2.5 times a second and the timeout time is 10 seconds, at least im confident thats the case :P anyone able to invalidate that?Im working in netbeans JDK 5 something something..Please help the rookie!Thanks EO   
  That's not an expected behaviour, without an in-depth look at the code, I can only assume you re-bind the socket or create a new one everytime you receive a packet.EDIT : hmm I don't know really. I don't understand the code that much.   ;  You create a new socket each time you send or receive data.Socket is created once for entire life time of application.abstract class Server implements Runnable {  public Server() { s = new DatagramSocket();}  public Server(int port) { s = new DatagramSocket(port);}  public void run() {    while (running) { DatagramPacket p = new DatagramPacket(); s.receive(s, p); handle(s, p);    }  }  public abstract void handle(DatagramSocket sock, DatagramPacket packet);  private volatile boolean running = true;  private final DatagramSocket s;};Then extend this class for client and server, and implement the only method 'handle()'.In there, examine the sender, examine the packet, do whatever needs to be done, then send reply on sock.   ;  Thanks guys I have made the modifications to my project to rectify this issue. Now I can get back to figuring out why data is being sent and not recieved when you go ingame.Much appreciated!   