简体   繁体   中英

Receiving packets in UDP- Java

Hi i am trying to send packets of 1 kb from client to server using UDP. I can receive all the packets but the problem is while loop is not exiting so i used socket timeout to achieve this.Since this does not suit dynamic environment i want to replace something sensible. Below are my server and client side code, client side follows server side from while loop.I just want to implement RDT 1.0 where the packets are divided into 1kb and send it over the UDP socket to receive in the other side. But while receiving all 1kb packet i have tried quite a few things to exit the while loop and finally end up setting a socket timeout at the receiving side. Also i declared 'i' (i<9 knowing my file size) to get out of the loop in order to execute the rest of the codings before the timeout.Is there a way i can change my while loop so that it suits all environment (sending N number of packets).

//Client side
    class Client
{
    public static DatagramSocket sock =null;
    public static FileInputStream in=null;
    public static DatagramPacket[] sendpacket=new DatagramPacket[1024];
public static void main(String[] args) throws Exception
{
    byte[] array = new byte[1024];
    DatagramSocket sock = new DatagramSocket();
    InetAddress ip= InetAddress.getByName("localhost");
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter a file name: ");
    System.out.flush(); 
    String filename = scanner.nextLine();
    File file = new File(filename);
    in=new FileInputStream(file);
    int length =0,checksum=0,h=1;
    while((length = in.read(array))!=-1)
    {
        sendpacket[h]=new DatagramPacket(array,array.length,ip,1234);
        sock.send(sendpacket[h]);
        checksum+=length;
        System.out.println("Sent packet "+h);
        System.out.println(length);
        System.out.println(checksum);
        h++;
         }
        in.close();

}
}





 //Server side
public class server
{
    public static DatagramPacket[] recvpacket=new DatagramPacket[100];
    public static DatagramSocket sock=null;
    public static FileOutputStream fos=null;
    public static BufferedOutputStream bos=null;
    public static void main(String args[]) 
    {

    try
        {
        try
        {
        sock=new DatagramSocket(1234);//opening a socket
    }
    catch(IOException ex)
    {
    System.out.println(ex);
    }
    sock.setSoTimeout(30000);
    boolean socketalive=true;
    byte[] array=new byte[1024];
    int i=1,checksum=0;
    System.out.println("server is ready to receive packets");
    while(recvpacket!=null)//I need change in this loop 
    {
    recvpacket[i]=new DatagramPacket(array,array.length);
    sock.receive(recvpacket[i]);
    System.out.println("server received packet "+i);
    int length=array.length;
    checksum+=length;
    System.out.println(length);//here i get the size of the buffer so  //checksum is wrong
    System.out.println(checksum);
    fos=new FileOutputStream(new File("profile1.docx"),true);
    fos.write(array);
    for(int j=0; j<1024; j++)
    {
      array[j]=0;
   }
   i++;
    }//while
    System.out.println("server received packet "+i);
    }//try
    catch(IOException e)
    {
    System.out.println(e);
    }
    }//main
    }//class

There are a few issues you may run into with this. UDP is not reliable like TCP so packets may be dropped or worse may come in out of sequence. You will need to store the data in a local buffer before writing it to a file, or write it to the appropriate spot in the file (possibly leaving room for one missing).

Ideally the UDP listening loop should be in its own thread so other code can be run. If you want to implement a sort of FTP over UDP then you need to at the start send a packet with information about the transfer such as the size, number of packets, checksum. Then for each packet you also need to include a sequence number and a checksum for the individual packet. At the end (or along the way), the server needs to reply to the client requesting missing/corrupt packets. I recommend sending a "transfer complete" packet at the end.

If all the packets have been received and are not corrupt, then you can terminate the loop!

You should be able to find something existing that will handle all of this for you rather than writing it all from scratch, but if you must write it all yourself then you have to replicate the overhead of TCP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM