java - Receiving/sending datagram socket data -


So I'm doing a program in Java that sends and receives data with the help of Datagram and Datagram packets. The problem is that whenever I am sending / receiving data - then different data in this program changes, I am also sending it, but only in some cases such as:

 < Code> Sending: 378 Receiving: 3786 Remittance: Receiving 374: 3742 Remittance: 360 Recipient: 3604   

But in rare cases, such as:

 : Receiving 376: 376 Send: 372 Receiving: 372 Send: 344 Receiving: 344   

I call it two strings Sending am, before it changed to the string:

  String message = integer. Toasting (Sync 1) + "+ + Integer. Tosting (Coordination 2);   

Then to be able to send it with a Datagram packet in a byte array:

  byte [] b = message.getBytes (charset forName ("UTF-8"); Datagram packet packet = new datagram packet (B, B.Length, RemoteHost, Remote Port); Try {datagramSocket.send (Packet);} hold (exception e) {e.printStackTrace ();}   

This is my thread that receives data:

  private Datagram socket d Socket; Private Datagram packet packet; Private byte [] buffer; Private Paint Programs program; Public receiverstread (Int. MyPort, Paint Programs Program) {this.prog = prog; {buffer = try new byte [256]; DSocket = new Datagram (myPort); packet = new datagram packet (buffer, buffer lamps);} hold (exception e) {e.printStackTrace ();} this.start ();} @ override public wide run (try {to {try} {dSocket.receive (packet); String data = new string (buffer, "UTF-8"); prog.handlePacket (date );} Catch (IOException e) {e.printStackTrace (); }} While (! This.currentThread (). Interwired ()); }   

And this is my method that is the packet "handles" received:

  string [] xy = data.split (""); Pointpoint = new point (integer parasont (xi [0] .tiram ()), integer .centre (xi [1] .tim ());   

Then I add the thing created with two coordinates in my program. I do not know where this wrong in this process is good with some new perspective! Thank you.

  string data = new string (buffer, "UTF-8");   

should be

  string data = new string (packet.getData (), packet.getOffset (), packet.getLength (), "UTF -8 ");    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -