In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
The following figure shows the general flow of a client / server program based on the UDP protocol:
Figure 1.1 UDP protocol communication flow
UDP socket is a connectionless, unreliable Datagram protocol
Why use it if he's unreliable? One: when an application uses broadcast or multicast, it can only use the UDP protocol; second, it is fast because it is connectionless. Because the UDP socket is connectionless, if one side's Datagram is lost, the other side will wait indefinitely, and the solution is to set a timeout.
When establishing a UDP socket, the second parameter of the socket function should be SOCK_DGRAM, indicating that a UDP socket is established. Because UDP is connectionless, the server side does not need the listen or accept function.
Using UDP socket programming can realize connectionless communication based on TCP/IP protocol, which is divided into two parts: server side and client side. The main implementation process is shown in figure 1.1.
The following describes the detailed functions and procedures of UDP network programming:
1. Socket function: in order to perform network input and output, the first thing a process must do is call the socket function to get a file descriptor.
-
# include
Int socket (int family,int type,int protocol);
Return: non-negative descriptor-success-1Murray-failure
-
The first parameter specifies the protocol suite, which currently supports five protocol suites, the most commonly used of which are AF_INET (IPv4 protocol) and AF_INET6 (IPv6 protocol). The second parameter specifies the socket type, and there are three types of sockets: SOCK_STREAM (byte stream socket), SOCK_DGRAM (Datagram socket) and SOCK_RAW (raw socket). If the socket type is not the original socket, the third parameter is 0.
2. Bind function: assign a local IP and protocol port to the socket. For Internet protocol, the protocol address is the combination of 32-bit IPv4 address or 128bit IPv6 address and 16-bit TCP or UDP port number; if the specified port is 0, the kernel will select a temporary port when calling bind. If you specify a wildcard IP address, the kernel will not select a local IP address until the connection is established.
-
# include
Int bind (int sockfd, const struct sockaddr * server, socklen_t addrlen)
Return: 0Mutual-success-1Mutual-failed
-
The first parameter is the socket description returned by the socket function; the second and third arguments are a pointer to a protocol-specific address structure and the length of that address structure, respectively.
3. Recvfrom function: UDP uses the recvfrom () function to receive data, which is similar to the standard read (), but specifies the destination address in the recvfrom () function.
-
# include
# include
Ssize_t recvfrom (int sockfd, void * buf, size_t len, int flags, struct sockaddr * from, size_t * addrlen)
Return the length of the data received-success-- 1-Murray-failure
-
The first three parameters are equivalent to the first three parameters of the function read (), and the flags parameter is the transmission control flag. The last two parameters are similar to the last two parameters of accept.
4. Sendto function: UDP uses the sendto () function to send data, which is similar to the standard write (), but specifies the destination address in the sendto () function.
-
# include
# include
Ssize_t sendto (int sockfd, const void * buf, size_t len, int flags, const struct sockaddr * to, int addrlen)
Return the length of the data sent-success-1Murray-failed
-
The first three parameters are equivalent to the first three parameters of the function read (), and the flags parameter is the transmission control flag. The parameter to indicates the protocol address to which the data will be sent, and its size is specified by the addrlen parameter.
Server.c program source code:
# include # define _ MAXLINE_ 80 # define _ SERV_PORT_ 8000 int main () {struct sockaddr_in server_addr,client_addr; socklen_t cliaddr_len Int sockfd; char buf [_ MAXLINE_]; char stre [INET _ ADDRSTRLEN]; int I, n; sockfd = socket (AF_INET, SOCK_DGRAM, 0); if (sockfd
< 0){ printf("create socket error!...\n"); return -1; } bzero(&server_addr,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_port = htons(_SERV_PORT_); if(bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr)) < 0){ printf("bind error!..."); close(sockfd); return 1; } printf("Accepting connections ...\n"); while(1){ cliaddr_len = sizeof(client_addr); //n = recvfrom(sockfd, buf, _MAXLINE_, 0,(struct sockaddr *)&client_addr, &cliaddr_len); n = recvfrom(sockfd, buf, _MAXLINE_, 0,(struct sockaddr *)&client_addr, &cliaddr_len); if(n == -1){ printf("recvfrom error...\n"); exit(1); } printf("received from %s at PORT %d\n",inet_ntop(AF_INET, &client_addr.sin_addr, str,sizeof(str)),ntohs(client_addr.sin_port)); for(i = 0; i < n; i++){ buf[i] = toupper(buf[i]); } n = sendto(sockfd, buf, n, 0, (struct sockaddr *)&client_addr, sizeof(client_addr)); if(n == -1){ printf("sendto error...\n"); } } return 0; } UDP客户端程序设计 client.c程序源码: #include #include #include #include #define _MAXLINE_ 80 #define _SERV_PORT_ 8000 int main(int argc, int* argv[]) { struct sockaddr_in servaddr; int sockfd, n; char buf[_MAXLINE_]; char str[INET_ADDRSTRLEN]; socklen_t servaddr_len; sockfd = socket(AF_INET, SOCK_DGRAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr); servaddr.sin_port = htons(_SERV_PORT_); while(fgets(buf, _MAXLINE_, stdin)!= NULL){ n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)\ &servaddr, sizeof(servaddr)); if(n == -1){ printf("sendto error\n"); } n = recvfrom(sockfd, buf, _MAXLINE_, 0, NULL, 0); if(n == -1){ printf("recvfrom error\n"); } write(STDOUT_FILENO, buf, n); } close(sockfd); return 0; } 运行展示,client发送小写字母,server收到之后进行大小写转换,发回到client,结果如下: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.