-1

Goodevening everyone. I am trying to create an application using Eclipse(Kepler) where I send an array of DatagramPackets. My server side application is receiving all packets sent by the client. But when the server tries to respond back by sending the response packets back to client, my client is unable to receive all the packets. I would be really obliged if someone helped me out.

here is my client side application code:

public static void main(String [] args) throws IOException
{
    timing t=new timing(); // timing is the name of my class

    InetAddress inet;
    DatagramSocket ds;

    int k=1024*60;

    DatagramPacket[] p=new DatagramPacket[64];
    DatagramPacket []recd=new DatagramPacket[64];
    byte[] buf=new byte[1024*60]; 

            if(args[0]==null)
    {
        args[0]="localhost";
    }
    inet=InetAddress.getByName(args[0]);
    ds=new DatagramSocket(6443);

    for(int i=0;i>64;i++)
    {
        p[i]=new DatagramPacket(sent.getBytes(),sent.length(),inet,7443);
        recd[i]=new DatagramPacket(buf,buf.length);
    }

    ds.setSoTimeout(120000);
    int buffer=ds.getReceiveBufferSize();
    int j=ds.getSendBufferSize();
    while(h<64)
        {
        p[h]=new DatagramPacket(sent.getBytes(),sent.length(),inet,7443);
        ds.send(p[h]);
        System.out.println("Client has sent packet:"+h);
        h++;
        }
    System.out.println("Receiving.");
    h=0;
    while(h<64)   // UNABLE TO RECEIVE ALL SERVER SIDE PACKETS . PROBLEM CODE
    {
    recd[h]=new DatagramPacket(buf,buf.length);
    ds.receive(recd[h]);
    System.out.println("Client has recd packet:"+h);
    h++;
    }

SERVER SIDE APPLICATION:

try{

    byte[] buf=new byte[60*1024];

    InetAddress add=InetAddress.getByName("localhost");
    for(int i=0;i<64;i++)
        dp[i]=new DatagramPacket(buf,buf.length,add,6443);
    ds.setSoTimeout(120000);
    System.out.println("SERVER READY AND LISTENING PORT 6443");
    int h=0;
    while(h<64)
    {
        dp[h]=new DatagramPacket(buf,buf.length,add,6443);
        ds.receive(dp[h]);
        System.out.println("Packet "+h+"recd.");
        h++;
    }


    String x1=new String(dp[63].getData());
    System.out.println("Server recd:"+x1);// correct. no problem here

    InetAddress add2=dp[63].getAddress();
    int port=dp[63].getPort();// this is fine. same data sent in all packets

    h=0;
    while(h<64)
    {
        dp[h]=new DatagramPacket(buf,buf.length,add2,port);
        ds.send(dp[h]);
        System.out.println("Server has sent packet:"+h);
        h++;
    }

kindly help me as when i send a single datagrampacket its being recd. but this array of packets isnt.

prmottajr
  • 1,816
  • 1
  • 13
  • 22
Utsav Jha
  • 366
  • 4
  • 16
  • could you add your output? maybe it could help a litte bit. – prmottajr May 25 '14 at 15:30
  • @prmottajr: here is the output. i have highlighted the important details in the cmd window.http://imgur.com/wbnDjnF – Utsav Jha May 25 '14 at 17:12
  • Although it is strange that a timeout occurs in the same machine it could happen. If you wrap your receive code in a try catch block maybe you could receive the packets after 60. Maybe only one packet died on the network layer. One problem with UDP is that it has not guarantee that it will deliver your packets so this is a correct behaviour, the code seems ok. If you could try that and post the new result. – prmottajr May 25 '14 at 19:56
  • I thought as much. UDP being a connectionless protocol is less reliable. – Utsav Jha May 26 '14 at 05:06
  • What is `sent` in the client? – user207421 Sep 29 '15 at 03:56

2 Answers2

1

I do not know much about your code, but if I really want this 'for' loop:

for (int i=0;i>64;i++)

to be executed at least once I would change it to i<64.

user207421
  • 305,947
  • 44
  • 307
  • 483
Vishal S
  • 73
  • 11
0

You don't show what sent is in the client, or therefore how long it is, but I suspect it is some reasonable size that can actually be sent.

By contrast, in the server you are attempting to send 60k datagrams, which won't work unless the sender's socket send buffer and the receiver's socket receive buffer are at least that size.

user207421
  • 305,947
  • 44
  • 307
  • 483