Reliable UDP in java -


i working on assignment make udp reliable using java. how can add timeout , re-transmission handle data-grams discarded , add sequence numbers client can verify reply appropriate request ??

this client code

import java.net.*; import java.io.*; public class echoclient { // udp port service bound     public static final int service_port = 7;   // max size of packet public static final int bufsize = 256; public static void main(string args[]){                  if (args.length != 1)                  {                          system.err.println ("syntax - java echoclient hostname");                          return;                   } string hostname = args[0]; // inetaddress specified hostname     inetaddress addr = null;         try         {             // resolve hostname inetaddr             addr = inetaddress.getbyname(hostname);         }         catch (unknownhostexception uhe)         {             system.err.println ("unable resolve host");             return;         }          try         {             // bind free port             datagramsocket socket = new datagramsocket();              // set timeout value of 2 seconds             socket.setsotimeout (2 * 1000);              (int = 1 ; <= 10; i++)             {                 // copy data our packet                 string message = "packet number " + ;                 char[] carray = message.tochararray();                                   byte[] sendbuf = new byte[carray.length];                 (int offset = 0; offset < carray.length ; offset++)                 {                     sendbuf[offset] = (byte) carray[offset];                 }                  // create packet send udp server                 datagrampacket sendpacket = new datagrampacket(sendbuf, carray.length, addr, service_port);                  system.out.println ("sending packet " + hostname);                  // send packet                 socket.send (sendpacket);                  system.out.print ("waiting packet.... ");                  // create small packet receiving udp packets                 byte[] recbuf = new byte[bufsize];                 datagrampacket receivepacket = new datagrampacket(recbuf, bufsize);                  // declare timeout flag                 boolean timeout = false;                  // catch interruptedioexception thrown                 // while waiting receive udp packet                 try                 {                     socket.receive (receivepacket);                 }                 catch (interruptedioexception ioe)                 {                     timeout = true;                 }                  if (!timeout)                 {                     system.out.println ("packet received!");                     system.out.println ("details : " + receivepacket.getaddress() );                      // obtain byte input stream read udp packet                     bytearrayinputstream bin = new bytearrayinputstream (                       receivepacket.getdata(), 0, receivepacket.getlength() );                      // connect reader easier access                     bufferedreader reader = new bufferedreader (                        new inputstreamreader ( bin ) );                      // loop indefinitely                     (;;)                     {                         string line = reader.readline();                          // check end of data                         if (line == null)                             break;                         else                             system.out.println (line);                     }                                }                 else                 {                     system.out.println ("packet lost!");                 }                  // sleep second, allow user see packet                 try                 {                     thread.sleep(1000);                 }catch (interruptedexception ie) {}             }         }         catch (ioexception ioe)         {             system.err.println ("socket error " + ioe);         }     } } 

what can adding import tcp headers sequence number, windows udp message body make more tcp. here solution might you.


Comments

Popular posts from this blog

php - Passing multiple values in a url using checkbox -

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -