In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to analyze UDP protocol, the content of the article is of high quality, so Xiaobian shares it with you as a reference, I hope you have a certain understanding of relevant knowledge after reading this article.
1. Socket
Socket: IP address + port number. In TCP/IP, it uniquely identifies a process in network communication. Sockets are used to describe one-to-one relationships between network connections.
TCP/IP protocol stipulates that network data flow should adopt big endian byte order, that is,(memory) low address high byte (data).
UDP_SOCKET
UDP Protocol----User Datagram Protocol (connection-oriented)--- SOCK_DDRAM
h represents host,n represents network,l represents 32-bit long integer,s represents 16-bit short integer.
IPv4 address format is defined in netinet/in.h,IPv4 address: sockaddr_in structure, including 16-bit port number and 32-bit IP address
struct sockaddr_in { uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_zero[8];};
UDP socket instance:
//udp_server.c
#include #include#include#include#include#include#include#include#includevoid usage(const char *proc){ printf("%s:[ip][port]\n",proc);}int main(int argc,char *argv[]){ if(argc != 3) { usage(argv[0]); return 1; } char *_ip=argv[1]; int _port=atoi(argv[2]); int sock=socket(AF_INET,SOCK_DGRAM,0); if(sock
< 0) { perror("socket"); exit(1); } struct sockaddr_in local; local.sin_family=AF_INET; local.sin_port=htons(_port); local.sin_addr.s_addr=inet_addr(_ip); if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0) { perror("bind"); exit(2); } int done=0; char buf[1024]; struct sockaddr_in client; socklen_t len=sizeof(client); while(!done) { ssize_t _size=recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&client,&len); if(_size >0) { buf[_size]='\0'; printf("[%s : %d]: %s\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port),buf); } else if(_size == 0) { printf("client close...\ n"); } else {} } return 0;}
//udp_client.c
#include #include#include#include#include#include#include#include#includevoid usage(const char *proc){ printf("%s:[ip][port]\n",proc);}int main(int argc,char *argv[]){ if(argc != 3) { usage(argv[0]); return 1; } char *_ip=argv[1]; int _port=atoi(argv[2]); int sock=socket(AF_INET,SOCK_DGRAM,0); if(sock
< 0) { perror("socket"); exit(1); } struct sockaddr_in local; local.sin_family=AF_INET; local.sin_port=htons(_port); local.sin_addr.s_addr=inet_addr(_ip); if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0) { perror("bind"); exit(2); } int done=0; char buf[1024]; struct sockaddr_in client; socklen_t len=sizeof(client); while(!done) { ssize_t _size=recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&client,&len); if(_size >0) { buf[_size]='\0'; printf("[%s : %d]: %s\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port),buf); } else if(_size == 0) { printf("client close...\ n"); } else {} } return 0;}
Run Results:
About how to analyze UDP protocol to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
Https://github.com/sssssanr/sanr-paper
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.