Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of client programming flow of UDP Server

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shares with you the content of a sample analysis of the UDP server client programming process. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

UDP programming flow

UDP provides connectionless, unreliable, Datagram services

UDP transmits as much as it can, but it does not guarantee reliability. The reliability of TCP is due to a series of mechanisms to ensure reliability. UDP packet loss will not be retransmitted. There is no distinction between the two protocols. It is necessary to distinguish between different scenarios. For example, if there is no data loss in file transfer, TCP protocol is more suitable.

For real-time video calls, UDP will send them at a constant rate, which allows some data to be lost in pursuit of better real-time performance, so UDP is more suitable.

Process: first of all, both the server and the client need to create a socket socket () (UDP does not have a strict sense of the server and client), and then the server needs to determine the ip and port bind (), waiting to receive data recvfrom () (which records each other's ip and port). Here we do not connect with a client, but just receive the sent data. The client sends data sendto () (you need to specify ip and port), because UDP does not establish a connection like TCP and identifies the client through file descriptors. It can only distinguish different data by identifying ip and port when sending and receiving, and close the socket close () at the end of sending and receiving.

UDP server code implementation # include#include#include#include#include#include#include#includeint main () {/ / create socket / / Parameter: / / AF_INET ipv4 / / Datagram service type used by SOCK_DGRAM UDP (the service type / / flag bit used by SOCK_STREAM streaming socket TCP is generally given 0 int sockfd = socket (AF_INET,SOCK_DGRAM,0) Assert (sockfd! =-1); / / create socket address structure struct sockaddr_in saddr,caddr; memset (& saddr,0,sizeof (saddr)); saddr.sin_family = AF_INET; saddr.sin_port = htons (6000); saddr.sin_addr.s_addr = inet_addr ("127.0.0.1") / / named socket int res = bind (sockfd, (struct sockaddr*) & saddr,sizeof (saddr)); assert (res! =-1); while (1) {int len = sizeof (caddr); / / it is specially stored in len because it receives a pointer char buff [128] = {0} when recvfrom / / accept data / / parameters: / / server socket / / store data / / store size / / flag bits generally give 0 / / store client address information (ip and port) / / the size recvfrom of caddr (sockfd Buff,127,0, (struct sockaddr*) & caddr,&len) Printf ("buff=%s\ n", buff) / / send data / / parameters: / / server socket / / sent data / / send data size / / flag bits generally give 0 / / address information of the sending destination / / size of address information sendto (sockfd "ok", 2 struct sockaddr* 0, (struct sockaddr*) & caddr,sizeof (caddr)) } / / close socket close (sockfd);} UDP client code implementation # include#include#include#include#include#include#include#includeint main () {int sockfd = socket (AF_INET,SOCK_DGRAM,0); assert (sockfd! =-1) / only need to specify the ip and port of the server, and the client's own ip and port are automatically specified by the system struct sockaddr_in saddr; memset (& saddr,0,sizeof (saddr)); saddr.sin_family = AF_INET; saddr.sin_port = htons (6000); saddr.sin_addr.s_addr = inet_addr ("127.0.0.1") While (1) {char buff [128] = {0}; printf ("input:\ n"); fgets (buff,128,stdin); if (strncmp (buff, "end", 3) = = 0) {break } sendto (sockfd,buff,strlen (buff), 0, (struct sockaddr*) & saddr,sizeof (saddr)); memset (buff,0128); int len = sizeof (saddr); recvfrom (sockfd,buff,127,0, (struct sockaddr*) & saddr,&len) / / it takes up the ip and port of the saddr to store and acquire each other, but it doesn't actually change the same printf ("buff=%s\ n", buff);} close (sockfd);} UDP server client code details

Execute the code of the server and the client

When we open multiple windows, start multiple clients to send data to the server

The reception of the UDP server is only based on whether there is data sent, as long as there is data sent, and there is no connection. Even if the server is closed and reopened, the original client can still send data, because there is no mutual connection between them.

If the server shuts down and does not restart, the client still sends out sendto () without blocking, but it will block at the step of recvfrom (). To put it simply, the server only receives data or sends data to the sender, no matter who can send data to it without any connection.

If we modify the server code

We then send data to the server through the client.

When using the UDP protocol, when the data is transmitted, we take the packet apart and read only the data of the set size, and the rest will be thrown away and then lost.

Each transmission is a separate packet, because each transmission may have a different destination address, and multiple data can be combined for TCP, because only the other side of the connection is sent within the same descriptor and connection.

Thank you for reading! This is the end of the article on "sample Analysis of UDP Server client programming process". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report