UDP Client Server Java -


i have simple udp client , server codes. when server gets request client, want form reply out of data send client additional data, shown in code.

import java.io.*; import java.net.*;  public class udpclient {  public static void main(string args[]){      // args give message contents , destination hostname     datagramsocket asocket = null;     try {         asocket = new datagramsocket();             byte [] m = args[0].getbytes();         inetaddress ahost = inetaddress.getbyname(args[1]);         int serverport = 8211;                                                                datagrampacket request =             new datagrampacket(m,  args[0].length(), ahost, serverport);         asocket.send(request);                                           byte[] buffer = new byte[1000];         datagrampacket reply = new datagrampacket(buffer, buffer.length);            asocket.receive(reply);         system.out.println("reply: " + new string(reply.getdata()));         }catch (socketexception e){system.out.println("socket: " + e.getmessage());     }catch (ioexception e){system.out.println("io: " + e.getmessage());     }finally {if(asocket != null) asocket.close();} }                    

}

public class udpserver {

  public static void main(string args[]){          datagramsocket asocket = null;         try{             asocket = new datagramsocket(8211);                     // create socket @ agreed port             byte[] buffer = new byte[1000];             while(true){                   buffer = ("hello "+new string(request.getdata()).touppercase()+" goodbye").getbytes();                  system.out.print("replying client: "+buffer);                  datagrampacket reply = new datagrampacket(buffer, buffer.length,                      request.getaddress(), request.getport());                 asocket.send(reply);             }         }catch (socketexception e){system.out.println("socket: " + e.getmessage());         }catch (ioexception e) {system.out.println("io: " + e.getmessage());         }finally {if(asocket != null) asocket.close();}     } 

}

the problem is, output get: replying client: hello paul , same received @ client side, goodbye being omitted. why being omitted? interestingly, if this:

buffer = ("hello "+"goodbye " +new string(request.getdata()).touppercase()).getbytes(); 

the output hello goodbye paul , same sent client.

simply invoking new string(request.getdata()) dangerous. datagrampacket request isn't going string, it's going entire packet. should use other methods provided datagrampacket real data, before converting string.

byte[] packet = new byte[request.getlength()];  packet = arrays.copyofrange(request.getdata(), request.getoffset(), request.getlength() + request.getoffset());  //packet contains string portion of request, use instead 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -