Server implementation

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

  2. Make a loop that handles the UDP requests and responses

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

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

Client implementation

  1. Open a socket on a random port.

  2. Try to broadcast to the default broadcast address (255.255.255.255)

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

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

  5. Wait for a reply

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

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

  8. 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的更多相关文章

  1. RFC3261--sip

    本文转载自 http://www.ietf.org/rfc/rfc3261.txt 中文翻译可参考 http://wenku.baidu.com/view/3e59517b1711cc7931b716 ...

  2. socket编程函数

    连接 TCP/IP协议规定网络数据传输应采用大端字节序 socket地址 struct sockaddr{ unsigned short sa_family; char sa_data[14]; }; ...

  3. 406 UDP协议是面向非连接的协议

    HTTP The Definitive Guide   Table 3-1. Common HTTP methods   Method Description Message body?   GET ...

  4. 406 UDP协议是面向非连接的协议 Keep-Alive

    HTTP The Definitive Guide   Table 3-1. Common HTTP methods   Method Description Message body?   GET ...

  5. [转]Objective-c中@interface、@implementation、@protocal

    原处:http://blog.csdn.net/l271640625/article/details/8393531 以下Objective-c简称OC 从事java开发的程序员们都知道,在java中 ...

  6. twisted udp编程

    概述 Unlike TCP, UDP has no notion of connections. A UDP socket can receive datagrams from any server ...

  7. Linux网络编程8——对TCP与UDP的简易封装

    引言 每次使用socket通信,都会有很对相似的操作.本文,会对TCP与UDP通信做一简单封装,并生成动态库. 代码 my_socket.h #ifndef __MY_SOCKET_H__ #defi ...

  8. 详解TCP和UDP数据段的首部格式

    TCP数据段的首部格式: 源端口号(16) 目的端口号(16) 序列号(32) 确认应答号(32) 数据偏移(4) 保留(6) 代码位(6) 窗口(16) 校验和(16) 紧急指针 选项(长度可变) ...

  9. ip头、tcp头、udp头详解及定义,结合Wireshark抓包看实际情况

    公司的同事们在分析网页加载慢的问题,忽然使用到了Wireshark工具,我就像发现新大陆一样好奇,赶紧看了看,顺便复习了一下相关协议.上学时学的忘的差不多了,汗颜啊! 报文封装整体结构 mac帧头定义 ...

  10. 程序员必备基础知识:通信协议——Http、TCP、UDP

    CP HTTP UDP: 都是通信协议,也就是通信时所遵守的规则,只有双方按照这个规则“说话”,对方才能理解或为之服务. TCP HTTP UDP三者的关系: TCP/IP是个协议组,可分为四个层次: ...

随机推荐

  1. Python+Django(4)——创建其他网页(模板继承)

    模板继承: 1,修改主页 父模板:抽取通用元素,在index.html同级目录下新建base.html <p> <a href="{% url 'learning_logs ...

  2. postgresql Extract 函数的使用

    Extract 属于 SQL 的 DML(即数据库管理语言)函数,同样,InterBase 也支持 Extract,它主要用于从一个日期或时间型的字段内抽取年.月.日.时.分.秒数据,因此,它支持其关 ...

  3. doy 19 进程管理

    1.进程管理 1.什么是进程,什么是线程​ 1.什么是程序 一般情况下,代码,安装包等全部都是应用程序. 2.什么是进程 应用程序运行起来的能够提供某种服务的实例. 3.什么是线程 进程中处理具体事务 ...

  4. Ubuntu实战

    Ubuntu是什么 Ubuntu早期是一个由Debian Linux发展起来的以桌面应用为主的操作系统.作为Linux发行版中的后起 之秀,Ubuntu Linux在短短几年时间里便迅速成长为从Lin ...

  5. web自动化测试—Firefox安装与配置

    web自动化测试-Firefox安装与配置 下一步 下一步 下一步 下一步 下一步 下一步 下一步 下一步 下一步设置不更新 下一步添加插件 下一步 下一步 下一步 下一步立即重启 下一步查看插件 在 ...

  6. 网络游戏同步法则 -- skywind

    转载出处:http://www.skywind.me/blog/archives/112 网路的硬件也有限,而人的创造也无限,在公网平均130ms的Latency下,是不存在"完全的&quo ...

  7. mybatis的xml中#{}和${}区别

    #{}表示一个占位符号,通过#{}可以实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换,#{}可以有效防止sql注入.初步编译后的sql语句是" ...

  8. Delaunay triangulation 的实现

    在GitHub 找到的别人的代码:https://github.com/earthwjl/DelaunayTriangulate 解压后是这样的:(没有x64) 直接就有了.sln工程文件,于是用Vi ...

  9. 【Flutter】环境搭建(Windows+Android Studio 3.6.1)

    最近参加的项目需要用到Flutter框架进行iOS/Android双端开发,然而第一步环境搭建的过程就忙活了一整个晚上,直到现在终于有时间静下心来整理一下搭建过程中遇到的困难. 0x00 Flutte ...

  10. .bat 脚本替换文件内容

    rem 定义变量延迟环境,关闭回显 @echo off&setlocal enabledelayedexpansion rem 读取a.txt所有内容 for /f "eol=* t ...