8 Replies - 1601 Views - Last Post: 14 February 2013 - 01:13 AM Rate Topic: -----

#1 codecsmash49  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 09-December 08

UDP packet drop simulation - Java

Posted 12 February 2013 - 05:58 PM

I have a peer to peer Chat program and I'm trying to simulate a dropped package and then having UDP resend it. Pretty much trying to have UDP act like TCP.


currently when I random 0 it isn't stopping the message, and anything after sent when a 0 hits breaks the next message.

-----Server "Run" class------
    if(randomDrop == 0)
                {
                    //droped
                  String message = "Failed";
                  byte [] returnData = message.getBytes();
                  
                  DatagramPacket sendPacket = new    
                  DatagramPacket(returnData,returnData.length, returnIPAddress, checkArr);                                           
                    try {
                        serverSocket.send(sendPacket);
                    } catch (IOException ex) {
                        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                else{
                    message = (new String(receivePacket.getData())).trim();
                    message = decrypt(message);                
                    
                    message = checkString(message);
                    System.out.println(checkArr);
                    String checkStr = message;
                    checkStr = checkStr.substring(0, 4);
    }

    public void run() {

        String input = this.userName + ": " + ChatGui.getText(); // get text from Send
 



----Client side Run class ---
        
        ChatGui.txtChat.append(input + '\n');
        ChatGui.clear();

        input = input.length() + "-" + input;
        input = crypt(input);
        for (int i = 0; i < this.ArrayPort.size(); i++) {
            if (ArrayPort.get(i) != myPort) {
                convertToPacket(input, ArrayPort.get(i));
                try {
                    clientSocket.send(sendPacket); // send message
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        
        
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        try {
            clientSocket.receive(receivePacket);
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
        String AckResponse = new String(receivePacket.getData());
        System.out.println(AckResponse);
        if(AckResponse.equals("Failed"))
        {
            for (int i = 0; i < this.ArrayPort.size(); i++) {
            if (ArrayPort.get(i) != myPort) {
                convertToPacket(input, ArrayPort.get(i));
                try {
                    clientSocket.send(sendPacket); // send message
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        }
        
        
    }


This post has been edited by codecsmash49: 12 February 2013 - 07:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: UDP packet drop simulation - Java

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2813
  • View blog
  • Posts: 11,909
  • Joined: 20-September 08

Re: UDP packet drop simulation - Java

Posted 13 February 2013 - 03:23 AM

Please show declaration and generation of 'randomDrop'
Was This Post Helpful? 0
  • +
  • -

#3 codecsmash49  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 09-December 08

Re: UDP packet drop simulation - Java

Posted 13 February 2013 - 01:59 PM

View Postg00se, on 13 February 2013 - 03:23 AM, said:

Please show declaration and generation of 'randomDrop'


Random rm = new Random();
        int randomDrop = rm.nextInt(9); // roll random
        System.out.println(roll);



Edit: problems pressing "9"

This post has been edited by codecsmash49: 13 February 2013 - 02:03 PM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2813
  • View blog
  • Posts: 11,909
  • Joined: 20-September 08

Re: UDP packet drop simulation - Java

Posted 13 February 2013 - 02:12 PM

I don't really understand the server class. Currently 1 in 9 runs of the app will send a packet with the content 'Failed'. The other 8 out of 9 runs will attempt to process a RECEIVED packet - why?

This post has been edited by g00se: 13 February 2013 - 02:14 PM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#5 codecsmash49  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 09-December 08

Re: UDP packet drop simulation - Java

Posted 13 February 2013 - 02:40 PM

View Postg00se, on 13 February 2013 - 02:12 PM, said:

I don't really understand the server class. Currently 1 in 9 runs of the app will send a packet with the content 'Failed'. The other 8 out of 9 runs will attempt to process a RECEIVED packet - why?


We are told to simulate a Dropped Package and a Resend, turning UDP into TCP pretty much. So I forcefully cause my server to "drop" a packet by ignoring it. Then it is supposed to send a message back to Client side, telling it to resend the last message.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2813
  • View blog
  • Posts: 11,909
  • Joined: 20-September 08

Re: UDP packet drop simulation - Java

Posted 13 February 2013 - 02:48 PM

You can't simulate that functionality by 'refusing' to receive a packet, because that packet might well have already been received. What you need to do is to receive ALL packets and then simply REPORT that 1 in 9 of those packets has been lost (whether it has or not)
Was This Post Helpful? 0
  • +
  • -

#7 codecsmash49  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 09-December 08

Re: UDP packet drop simulation - Java

Posted 13 February 2013 - 03:06 PM

View Postg00se, on 13 February 2013 - 02:48 PM, said:

You can't simulate that functionality by 'refusing' to receive a packet, because that packet might well have already been received. What you need to do is to receive ALL packets and then simply REPORT that 1 in 9 of those packets has been lost (whether it has or not)


So add a numerical tag at each message sent. When they get to the other side I can pick 1 to delete. Making the program thinking it like message 5 was lost. Then request the client to resend ALL packets?
Was This Post Helpful? 0
  • +
  • -

#8 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: UDP packet drop simulation - Java

Posted 13 February 2013 - 06:45 PM

Why wouldn't you just use TCP? TCP seems better then UDP when it comes to chat applications..
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2813
  • View blog
  • Posts: 11,909
  • Joined: 20-September 08

Re: UDP packet drop simulation - Java

Posted 14 February 2013 - 01:13 AM

Quote

So add a numerical tag at each message sent. When they get to the other side I can pick 1 to delete. Making the program thinking it like message 5 was lost.
Yes, that goes without saying - if you want to request that a packet be resent, it has to have an id in order to make that request.

Quote

Then request the client to resend ALL packets?
No, just the one that was 'deleted'
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1