Why is this Java UDP packet length too long? -


so have java server , client, data being sent server fine , server interperating it, found client takes ages respond server has sent it, after time looking around found data server sending client way longer data should sent.

the packet sent client has data sent has lot of white space after it, fix this, have ideas?

my code data simple loop of every client on server, adds client data string , string added packet:

class playerlist

public static string getstring() {     string message = "";      for(int x = 0; x < list.size(); x++)     {         player player = list.get(x);          if(message.equals(""))         {             message += player.name+";"+player.address+";"+player.pos[0]+";"+player.pos[1]+";"+player.fakerotation+";"+player.rotation+";"+player.rotationspeed+";"+player.speed+";"+player.sheildenabled+";"+player.sheildstrength+";"+player.health;         }         else         {             message += ","+player.name+";"+player.address+";"+player.pos[0]+";"+player.pos[1]+";"+player.fakerotation+";"+player.rotation+";"+player.rotationspeed+";"+player.speed+";"+player.sheildenabled+";"+player.sheildstrength+";"+player.health;         }     }      system.out.println(message);      return message; } 

class send

while(server.serverrunning)     {         for(int p = 0; p < playerlist.list.size(); p++)         {             player player = playerlist.list.get(p);              try             {                 byte[] buf = playerlist.getstring().getbytes();                  //send message client given address , port                 packet = new datagrampacket(buf, buf.length, player.address);                 server.socket.send(packet);             }             catch (ioexception e)             {                 system.out.println("can't send packet player: "+player.name);             }         }     } 

i know data recieved getstring method correct , has no white space have tested it, must happening when add string packet.

the intended data shows in output as: luke;127.0.0.1:63090;50.0;50.0;0.0;0.0;0.0;0.0;true;100;100

however actual data shows on client as: luke;127.0.0.1:63090;50.0;50.0;0.0;0.0;0.0;0.0;true;100;100 (lots of spaces here) ...line long, please switch wrapped mode see whole line...

the client code receive data is:

receivedata = new byte[clientsocket.getreceivebuffersize()];                 receivepacket = new datagrampacket(receivedata, receivedata.length);                 clientsocket.receive(receivepacket);                 receivemessage = new string(receivepacket.getdata()); 

getdata on datagrampacket returns entire buffer, may have data @ end. need call getlength() determine actual length of data received, , @ bytes getdata()

byte[] realdata = arrays.copyof( receivepacket.getdata(), receivepacket.getlength() ); 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -