简体   繁体   English

java中的可靠UDP

[英]Reliable UDP in java

I am working on my assignment to make UDP reliable using java.我正在完成我的任务,使用 java 使 UDP 可靠。 How can i add Timeout and re-transmission to handle data-grams that are discarded and add Sequence numbers so the client can verify that a reply is for the appropriate request ??我如何添加超时和重新传输来处理被丢弃的数据报并添加序列号,以便客户端可以验证回复是否针对适当的请求??

this is client code这是客户端代码

import java.net.*;
import java.io.*;
public class EchoClient {
// UDP port to which service is 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];
// Get an InetAddress for the specified hostname
    InetAddress addr = null;
        try
        {
            // Resolve the hostname to an InetAddr
            addr = InetAddress.getByName(hostname);
        }
        catch (UnknownHostException uhe)
        {
            System.err.println ("Unable to resolve host");
            return;
        }

        try
        {
            // Bind to any free port
            DatagramSocket socket = new DatagramSocket();

            // Set a timeout value of two seconds
            socket.setSoTimeout (2 * 1000);

            for (int i = 1 ; i <= 10; i++)
            {
                // Copy some data to our packet
                String message = "Packet number " + i ;
                char[] cArray = message.toCharArray();                  
                byte[] sendbuf = new byte[cArray.length];
                for (int offset = 0; offset < cArray.length ; offset++)
                {
                    sendbuf[offset] = (byte) cArray[offset];
                }

                // Create a packet to send to the UDP server
                DatagramPacket sendPacket = new DatagramPacket(sendbuf, cArray.length, addr, SERVICE_PORT);

                System.out.println ("Sending packet to " + hostname);

                // Send the packet
                socket.send (sendPacket);

                System.out.print ("Waiting for packet.... ");

                // Create a small packet for receiving UDP packets
                byte[] recbuf = new byte[BUFSIZE];
                DatagramPacket receivePacket = new DatagramPacket(recbuf, BUFSIZE);

                // Declare a timeout flag
                boolean timeout = false;

                // Catch any InterruptedIOException that is thrown
                // while waiting to receive a 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 a byte input stream to read the UDP packet
                    ByteArrayInputStream bin = new ByteArrayInputStream (
                      receivePacket.getData(), 0, receivePacket.getLength() );

                    // Connect a reader for easier access
                    BufferedReader reader = new BufferedReader ( 
                      new InputStreamReader ( bin ) );

                    // Loop indefinitely
                    for (;;)
                    {
                        String line = reader.readLine();

                        // Check for end of data
                        if (line == null)
                            break;
                        else
                            System.out.println (line);
                    }               
                }
                else
                {
                    System.out.println ("packet lost!");
                }

                // Sleep for a second, to allow user to see packet
                try
                {
                    Thread.sleep(1000);
                }catch (InterruptedException ie) {}
            }
        }
        catch (IOException ioe)
        {
            System.err.println ("Socket error " + ioe);
        }
    }
}

What you can do is adding import TCP headers like sequence number, windows into the UDP message body to make it more like TCP.您可以做的是将导入 TCP 标头(如序列号、窗口)添加到 UDP 消息正文中,使其更像 TCP。 Here is the a solution that might help you.这是一个可能对您有所帮助的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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