Java UDP Optimizing -


i've been having trouble udp. massive delay between host , client when server stations close client. works fine when local host. here video of issue server error. here game server code / client code. if need packets i'm happy supply. apologize if did wrong first time using stackoverflow.

    package com.holdout.game.net;      import java.io.ioexception;     import java.net.datagrampacket;     import java.net.datagramsocket;     import java.net.inetaddress;     import java.net.socketexception;     import java.net.unknownhostexception;      import com.holdout.game.game;     import com.holdout.game.entities.playermp;     import com.holdout.game.net.packets.packet;     import com.holdout.game.net.packets.packet00login;     import com.holdout.game.net.packets.packet01disconnect;     import com.holdout.game.net.packets.packet02move;     import com.holdout.game.net.packets.packet.packettypes;      public class gameclient extends thread {          private inetaddress ipaddress;         private datagramsocket socket;         private game game;          public gameclient(game game, string ipaddress) {             this.game = game;             try {                 this.socket = new datagramsocket();                 this.ipaddress = inetaddress.getbyname(ipaddress);             } catch (socketexception e) {                 e.printstacktrace();             } catch (unknownhostexception e) {                 e.printstacktrace();             }         }          public void run() {             while (true) {                 byte[] data = new byte[1024];                 datagrampacket packet = new datagrampacket(data, data.length);                 try {                     socket.receive(packet);                 } catch (ioexception e) {                     e.printstacktrace();                 }                 this.parsepacket(packet.getdata(), packet.getaddress(), packet.getport());             }         }          private void parsepacket(byte[] data, inetaddress address, int port) {             string message = new string(data).trim();             packettypes type = packet.lookuppacket(message.substring(0, 2));             packet packet = null;             system.out.println(message);             switch (type) {             default:             case invalid:                 break;             case login:                 packet = new packet00login(data);                 handlelogin((packet00login) packet, address, port);                 break;             case disconnect:                 packet = new packet01disconnect(data);                 system.out.println("[" + address.gethostaddress() + ":" + port + "] "                         + ((packet01disconnect) packet).getusername() + " has left world...");                 game.level.removeplayermp(((packet01disconnect) packet).getusername());                 break;             case move:                 packet = new packet02move(data);                 handlemove((packet02move) packet);             }           }          public void senddata(byte[] data) {             if (!game.isapplet) {                 datagrampacket packet = new datagrampacket(data, data.length, ipaddress, 1331);                 try {                     socket.send(packet);                 } catch (ioexception e) {                     e.printstacktrace();                 }             }         }          private void handlelogin(packet00login packet, inetaddress address, int port) {             system.out.println("[" + address.gethostaddress() + ":" + port + "] " + packet.getusername()                     + " has joined game...");             pl      ayermp player = new playermp(game.level, packet.getx(), packet.gety(), packet.getusername(), address, port);                 game.level.addentity(player);             }              private void handlemove(packet02move packet) {                         this.game.level.moveplayer(packet.getusername(), packet.getx(), packet.gety(), packet.getnumsteps(),                                 packet.ismoving(), packet.getmovingdir());             }          }        package com.holdout.game.net;  import java.io.ioexception; import java.net.datagrampacket; import java.net.datagramsocket; import java.net.inetaddress; import java.net.socketexception; import java.util.arraylist; import java.util.list;  import com.holdout.game.game; import com.holdout.game.entities.playermp; import com.holdout.game.net.packets.packet; import com.holdout.game.net.packets.packet00login; import com.holdout.game.net.packets.packet01disconnect; import com.holdout.game.net.packets.packet02move; import com.holdout.game.net.packets.packet.packettypes;  public class gameserver extends thread {      private datagramsocket socket;     private game game;     private list<playermp> connectedplayers = new arraylist<playermp>();      public gameserver(game game) {         this.game = game;         try {             this.socket = new datagramsocket(1331);         } catch (socketexception e) {             e.printstacktrace();         }     }      public void run() {         while (true) {             byte[] data = new byte[1024];             datagrampacket packet = new datagrampacket(data, data.length);             try {                 socket.receive(packet);             } catch (ioexception e) {                 e.printstacktrace();             }             this.parsepacket(packet.getdata(), packet.getaddress(), packet.getport());         }      }      private void parsepacket(byte[] data, inetaddress address, int port) {         string message = new string(data).trim();         packettypes type = packet.lookuppacket(message.substring(0, 2));         packet packet = null;         switch (type) {         default:         case invalid:             break;         case login:             packet = new packet00login(data);             system.out.println("[" + address.gethostaddress() + ":" + port + "] "                     + ((packet00login) packet).getusername() + " has connected...");             playermp player = new playermp(game.level, 100, 100, ((packet00login) packet).getusername(), address, port);             this.addconnection(player, (packet00login) packet);             break;         case disconnect:             packet = new packet01disconnect(data);             system.out.println("[" + address.gethostaddress() + ":" + port + "] "                     + ((packet01disconnect) packet).getusername() + " has left...");             this.removeconnection((packet01disconnect) packet);             break;         case move:             packet = new packet02move(data);             this.handlemove(((packet02move) packet));         }     }      public void addconnection(playermp player, packet00login packet) {         boolean alreadyconnected = false;         (playermp p : this.connectedplayers) {             if (player.getusername().equalsignorecase(p.getusername())) {                 if (p.ipaddress == null) {                     p.ipaddress = player.ipaddress;                 }                 if (p.port == -1) {                     p.port = player.port;                 }                 alreadyconnected = true;             } else {                 // relay current connected player there new                 // player                 senddata(packet.getdata(), p.ipaddress, p.port);                  // relay new player connect player                 // exists                 packet = new packet00login(p.getusername(), p.x, p.y);                 senddata(packet.getdata(), player.ipaddress, player.port);             }         }         if (!alreadyconnected) {             this.connectedplayers.add(player);         }     }      public void removeconnection(packet01disconnect packet) {         this.connectedplayers.remove(getplayermpindex(packet.getusername()));         packet.writedata(this);     }      public playermp getplayermp(string username) {         (playermp player : this.connectedplayers) {             if (player.getusername().equals(username)) {                 return player;             }         }         return null;     }      public int getplayermpindex(string username) {         int index = 0;         (playermp player : this.connectedplayers) {             if (player.getusername().equals(username)) {                 break;             }             index++;         }         return index;     }      public void senddata(byte[] data, inetaddress ipaddress, int port) {         if (!game.isapplet) {              datagrampacket packet = new datagrampacket(data, data.length, ipaddress, port);             try {                 this.socket.send(packet);             } catch (ioexception e) {                 e.printstacktrace();             }         }     }      public void senddatatoallclients(byte[] data) {         (playermp p : connectedplayers) {             senddata(data, p.ipaddress, p.port);         }     }      private void handlemove(packet02move packet) {         if (getplayermp(packet.getusername()) != null) {             int index = getplayermpindex(packet.getusername());             playermp player = this.connectedplayers.get(index);             player.x = packet.getx();             player.y = packet.gety();             player.setmoving(packet.ismoving());             player.setmovingdir(packet.getmovingdir());             player.setnumsteps(packet.getnumsteps());             packet.writedata(this);         }     }  } 


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -