Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 714
  • Last Modified:

Check for duplicated UDP Packet

Hi Experts,
I have a DatagramClient class and DatagramServer class.
DatagramClient send 1 byte of data using UDP
DatagramServer receives and resend the packet back to the client as echo service
When DatagramClient receive the packet, how can I write a checksum to check if the packet is duplicated one? And how can I add the checksum to the packet?
Thanks
LA
0
LanAnh
Asked:
LanAnh
  • 7
  • 5
  • 4
2 Solutions
 
CEHJCommented:
You can also use:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/CRC32.html

The principle is the same in each case
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
>>
DatagramClient send 1 byte of data using UDP
DatagramServer receives and resend the packet back to the client as echo service
>>

Incidentally, 1 byte of data is not much of a packet ;-) You don't need to checksum that. Just check byteSent == byteReceived
0
 
LanAnhAuthor Commented:
Thank you all for your quick responds and information, I am now exploring all of these websites. I am a beginner,so a bit slow. I have to send 1000 packets of size 1 byte each packet, and I have to count how many duplicated packets overall when receiving.
Cheers
LA
0
 
CEHJCommented:
>>. I have to send 1000 packets of size 1 byte each packet

Are you sure? If so, you don't need to checksum (see my previous comment)
0
 
LanAnhAuthor Commented:
Hi CEHJ
Do you mean about the content of the data packet?
If so, it won't work. The loop run for 1000 times (counter), what if each time it send an emtpy byte packet like this ' '. And receive the echo back. If I check the content of the received packet, they will all be the same. I thought I need to have a check sum and increment the value of checksum, so that each packet will have unique checksum value, So I can compare my sending packet checksum with my receiving packet checksum. During the loop, If the packet is lost, the counter won't be incremented.
At the end of the program, I have to count the number of packets lost and the number of duplicated packets and average of roundtrip time.
Here is my program

      public static void getService(InetAddress addr){
                          
              try {                      
                    // Bind UDP socket to any free port
                    socket = new DatagramSocket();
                    
                    //set timeout for receiving packet to 5 miliseconds        
                    socket.setSoTimeout(5);
                    
                    //Start timer
                    long x1 = System.currentTimeMillis();
                    
                  int counter =0;
                  while(counter < 1000){            

                        //create data for packet
                        byte[] sendbuf = new byte[1];                  
                        sendbuf[0] = (byte)(String.valueOf(counter)).charAt(0);
                  
                        //create a packet with data to send to the UDP server
                        DatagramPacket sendPacket = new DatagramPacket(sendbuf,BUFSIZE,addr,SERVICE_PORT);
                   
                         //send the packet
                         socket.send(sendPacket);
                   
                         //create receiving packet
                         byte[] recbuf = new byte[1];
                         DatagramPacket receivePacket = new DatagramPacket(recbuf,BUFSIZE);
                   
                         //timeout flag
                         boolean timeout = false;             
                         
                         try{
                               //receive the packet
                               socket.receive(receivePacket);
                         
                         } catch(InterruptedIOException ioe){
                               //data lost
                               timeout = true;                                           
                         }
                           if(!timeout){
                                 //increment counter when receving packet                                
                                     counter++;                                                                

                                       // some code here to count duplicated packet

                          }else {                                
                                //count number of lost packet.
                                num_lost++;
                          }
                    }

                    //stop timer                    
                    long x2 = System.currentTimeMillis();
                    //calculate average round trip time
                    long total = (x2 -x1);
                    avg_rt = ((double)total/(double)counter);       
                             
              } catch (SocketException se){
                    System.err.println("Socket error: " + se);
                    
              } catch(IOException ie){
                    System.err.println("IOException : " + ie);
              }
              
      }
0
 
objectsCommented:
Code I posted above included calculkating the checksum

Heres another example:
http://www.javaalmanac.com/egs/java.util.zip/CalculateChecksum.html

Sounds like you also need to add a sequence number to your packet. Each time you send a new packet you would increment the sequence number by one.
0
 
LanAnhAuthor Commented:
objects,
How I can add sequence number to the packet?
Cheers
LA
0
 
objectsCommented:
something like:

byte[] sendbuf = new byte[3];              
sendbuf[0] = (byte)(String.valueOf(counter)).charAt(0);
sendbuf[1] = counter & 0xff;
sendbuf[2] = (counter >> 8)  & 0xff;
0
 
CEHJCommented:
>>I thought I need to have a check sum and increment the value of checksum, so that each packet will have unique checksum value

I think there's some confusion between checksum and sequence number here: the checksum's really to protect the integrity of the packet whereas the sequence number is to ensure the message arrives in such a way that it can be ordered and missing packets be resent if possible. Are you looking for both or just one?
0
 
LanAnhAuthor Commented:
Sorry CEHJ, I think I am confused mysefl. I think I need to sequence number in order to detect dupplicated packets. When receiving packet, I would compare it with the sending one, if it is smaller sequence number, I assume that is dupplicated packet. I am not quite sure with this assumption as the previous packet could be lost and resend again. Also, my packet size to be sent allows only one byte so, here is my codes. Please give me some help if there is still something wrong with them.

public class DatagramClient{
      
      /**
       * UDP port to which echo service is bound.
       */
      public static final int SERVICE_PORT = 1024;
      
      /**
       * Maximum buffer size for the packet
       */
      public static final int BUFSIZE = 1;
      
      /**
       * Socket used for reading and writing UDP packets
       */  
      private static DatagramSocket socket;
      
      /*
       * Average Round Trip Time
       */
      private static double avg_rt=0;
      
      /*
       * Number of lost packets
       */
      private static int num_lost=0;
      
      /*
       * Number of duplicated packets
       */
      private static int num_dup=0;       
      
      

      public static void main(String args[]){
              
             if(args.length !=1){
                    
                    System.err.println("Syntax - java DatagramClient hostname");
                    return;
              }              
              
              String hostname = args[0];
              InetAddress      addr = null;
                            
              try {
              
                    //get an InetAddress for the specific hostname
                    addr = InetAddress.getByName(hostname);
                    
                          
                    //send packet and get echo service from server
                   getService(addr);
                    
              } catch (UnknownHostException uhe){
                    
                    System.err.println("Unable to resolve host");
                    return;
              }         
             
             System.out.println("Average Round Trip Time : " + avg_rt);
             System.out.println("Number of lost packets: " + num_lost);
             System.out.println("Number of duplicated packets: " + num_dup);
                    
      }
      
      /**
       * Send packet to server and receive packet from server
       * Count lost packets and average round trip time of a packet of 1 byte size
       *  
       * @param addr InetAddress remote machine (or server)
       * @return void
       */
      public static void getService(InetAddress addr){
                          
              try {                      
              
                    // Bind UDP socket to any free port
                    socket = new DatagramSocket();
                    
                    //set timeout for receiving packet to 5 miliseconds        
                    socket.setSoTimeout(5);
                    
                    
                     //Start timer
                    long x1 = System.currentTimeMillis();
                    
                     
                  int counter =0;
                  while(counter < 1000){      
                        
                        //create data for packet
                        byte[] sendbuf = new byte[1];                          
                        sendbuf[0] = (byte)(counter & 0xff);
            
                        
                        //create a packet with data to send to the UDP server
                        DatagramPacket sendPacket = new DatagramPacket(sendbuf,BUFSIZE,addr,SERVICE_PORT);
                                     
                        //send the packet
                        socket.send(sendPacket);
                                     
                        //create receiving packet
                        byte[] recbuf = new byte[1];
                         DatagramPacket receivePacket = new DatagramPacket(recbuf,BUFSIZE);
                         
                                                        
                         //timeout flag
                         boolean timeout = false;             
                         
                        try{
                               //receive the packet
                               socket.receive(receivePacket);      
                               byte[] rec = receivePacket.getData();
                               
                               //check if duplicated packet
                               if( rec[0]  < (byte)(counter & 0xff))
                                     num_dup++;
                         
                         } catch(InterruptedIOException ioe){
                               //data lost
                               timeout = true;                                           
                         }
                               
                           if(!timeout){
                                 
                                 // increment counter                          
                                counter++;                    
                                                                                  
                          }else {                                
                                //count number of lost packet.
                                num_lost++;                                
                          }
                  }
                    
                    //stop timer                    
                    long x2 = System.currentTimeMillis();
                    
                    //calculate average round trip time
                    long total = (x2 -x1);
                    avg_rt = ((double)total/(double)counter);        
                             
              } catch (SocketException se){
                    System.err.println("Socket error: " + se);
                    
              } catch(IOException ie){
                    System.err.println("IOException : " + ie);
              }
              
      }
      

}
0
 
CEHJCommented:
>>I think I need to sequence number in order to detect dupplicated packets.

That's perfectly reasonable

>>Also, my packet size to be sent allows only one byte

How then can you send a sequence number if the packet itself is only one byte? There's no room for anything else
0
 
LanAnhAuthor Commented:
Is there any way I can split points of this question to CEHJ and objects ???
0
 
CEHJCommented:
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now