How to implement UDP protocal
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的更多相关文章
- RFC3261--sip
本文转载自 http://www.ietf.org/rfc/rfc3261.txt 中文翻译可参考 http://wenku.baidu.com/view/3e59517b1711cc7931b716 ...
- socket编程函数
连接 TCP/IP协议规定网络数据传输应采用大端字节序 socket地址 struct sockaddr{ unsigned short sa_family; char sa_data[14]; }; ...
- 406 UDP协议是面向非连接的协议
HTTP The Definitive Guide Table 3-1. Common HTTP methods Method Description Message body? GET ...
- 406 UDP协议是面向非连接的协议 Keep-Alive
HTTP The Definitive Guide Table 3-1. Common HTTP methods Method Description Message body? GET ...
- [转]Objective-c中@interface、@implementation、@protocal
原处:http://blog.csdn.net/l271640625/article/details/8393531 以下Objective-c简称OC 从事java开发的程序员们都知道,在java中 ...
- twisted udp编程
概述 Unlike TCP, UDP has no notion of connections. A UDP socket can receive datagrams from any server ...
- Linux网络编程8——对TCP与UDP的简易封装
引言 每次使用socket通信,都会有很对相似的操作.本文,会对TCP与UDP通信做一简单封装,并生成动态库. 代码 my_socket.h #ifndef __MY_SOCKET_H__ #defi ...
- 详解TCP和UDP数据段的首部格式
TCP数据段的首部格式: 源端口号(16) 目的端口号(16) 序列号(32) 确认应答号(32) 数据偏移(4) 保留(6) 代码位(6) 窗口(16) 校验和(16) 紧急指针 选项(长度可变) ...
- ip头、tcp头、udp头详解及定义,结合Wireshark抓包看实际情况
公司的同事们在分析网页加载慢的问题,忽然使用到了Wireshark工具,我就像发现新大陆一样好奇,赶紧看了看,顺便复习了一下相关协议.上学时学的忘的差不多了,汗颜啊! 报文封装整体结构 mac帧头定义 ...
- 程序员必备基础知识:通信协议——Http、TCP、UDP
CP HTTP UDP: 都是通信协议,也就是通信时所遵守的规则,只有双方按照这个规则“说话”,对方才能理解或为之服务. TCP HTTP UDP三者的关系: TCP/IP是个协议组,可分为四个层次: ...
随机推荐
- VUE安装环境及项目创建
Vue环境安装配置 安装git工具,便于在wind电脑上操作命令行,自行在网上下载安装,(可以不安装)使用cmd. 安装node,检查node安装是否成功,在git工具中输入node -v(如果成功的 ...
- Docker-compose安装Wordpress
Docker-compose实战Wordpress 96 iXiAo9 2019.01.10 18:54 字数 82 阅读 105评论 0喜欢 0 Docker-compose实战Wordpress ...
- miou
1. 前言 本文学习记录了机器学习中的分类常见评价指标以及分割中的MIoU. 主要有以下概念:Accuracy, Precision, Recall, Fscore,混淆矩阵,IoU及MIoU. 2. ...
- aop的一些方注释介绍
//定义切入点 @Pointcut("execution(void com.itheima.dao.BookDao.update())") private void pt2(){} ...
- 获取n位数m进制的随机数 js
js 获取n位数m进制的随机数 n 的取值范围为 0 < n > 1.7976931348623157e+308 (Number.MAX_VALUE) m的取值范围为 2 <= m ...
- 第二次python作业
#3.1 print("今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问几何?\n") number = int(input("请输入你认为符合条件的数: & ...
- 11、jmeter配置元件--计数器
前面学过csv和变量 csv里面的数据是固定的 如果里面的数据不够 线程要么就停止要么就需要重头再来 不太灵活 用到固定化的数据,比如说多少个用户等等 如果有一些是变动的 随着线程数增加 数据 ...
- MSVC设置版本
MSVC设置版本 在开发QT时,由于QT 5.12与MSVC 2017兼容,因此需要用MSVC 2017来编译使用QT 5.12的程序. 1 安装MSVC 2017 由于笔者电脑上安装的Visual ...
- FIRE2023:殁亡漫谈
FIRE2023:殁亡漫谈 读书的时候,想到殁亡,脑海涌出一则喜欢的遗言: 钱花完了,我走了.签名 如果可能牵涉到旁人(比如殁在旅馆里),就再立一则: 我的殁与店家无关. 签名 然后放下Kindle, ...
- PHP 计算一个月第一天和最后一天
$fistday= date('Y-m-1 23:59:59'); $lastday= strtotime("$fistday+1 month -1 day");