In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.