How to implement UDP protocal

2023-04-28,,

Server implementation

    Open a socket on the server that listens to the UDP requests. (I’ve chosen 8888)

    Make a loop that handles the UDP requests and responses

    Inside the loop, check the received UPD packet to see if it’s valid

    Still inside the loop, send a response to the IP and Port of the received packet

Client implementation

    Open a socket on a random port.

    Try to broadcast to the default broadcast address (255.255.255.255)

    Loop over all the computer’s network interfaces and get their broadcast addresses

    Send the UDP packet inside the loop to the interface’s broadcast address

    Wait for a reply

    When we have a reply, check to see if the package is valid

    When it’s valid, get the package’s sender IP address; this is the server’s IP address

    CLOSE the socket! We don’t want to leave open random ports on someone else’s computer

DatagramSocket

The Java DatagramSocket class represents a socket for sending and receiving datagram packets. It is a sending and receiving point for a packet delivery service where each packet is individually addressed and routed

Commonly used Constructors of DatagramSocket class

DatagramSocket() throws SocketEeption: it creates a datagram socket and binds it with the available Port Number on the localhost machine.

DatagramSocket(int port) throws SocketEeption: it creates a datagram socket and binds it with the given Port Number.

DatagramSocket(int port, InetAddress address) throws SocketEeption: it creates a datagram socket and binds it with the specified port number and host address.

DatagramPacket

This class provides facility for connection less transfer of messages from one system to another. Each message is routed only on the basis of information contained within the packet and it may be possible for different packets to route differently. There is also no guarantee as to whether the message will be delivered or not, and they may also arrive out of order. This class provides mechanisms for creation of datagram packets for connectionless delivery using datagram socket class.

 Syntax :public DatagramPacket(byte[] buf,
               int offset,
               int length,
               InetAddress address,
               int port)
 Parameters :
 buf : byte array
 offset : offset into the array
 length : length of message to deliver
 address : address of destination
 port : port number of destination

Enumeration

The Enum in Java is a data type which contains a fixed set of constants.

Java Enums can be thought of as classes which have a fixed set of constatns. The Java enum constants are static and final implicitly

Enums are used to create our own data type like classes. The enum data type (also known as Enumerated Data Type) is used to define an enum in Java

NetworkInterface

A network interface is the point of interconnection between a device and any of its network connects

 NetworkInterface nif = NetworkInterface.getByName("lo");
 Enumeration<InetAddress> nifAddresses = nif.getInetAddresses();
 ​
 Socket socket = new Socket();
 socket.bind(new InetSocketAddress(nifAddresses.nextElement(), 0));
 socket.connect(new InetSocketAddress(address, port));

We retrieve the network interface attached to lo first, retrieve the addresses attached to it, create a socket, bind it to any of the enumerated addresses which we don't even know at compile time and then connect.

A NetworkInterface object contains a name and a set of IP addresses assigned to it. So binding to any of these addresses will gurantee communication through this interface

InetAddress

Java InetAddress class represnets an IP address. InetAddress class provides methods to get the IP of any host name

https://jackiexie.com/2015/07/15/network-discovery-using-udp-broadcast-java/

How to implement UDP protocal的相关教程结束。

《How to implement UDP protocal.doc》

下载本文的Word格式文档,以方便收藏与打印。