UDP server and client communication code development process

1. UDP communication

TCP:Transmission Control Protocol, connection-oriented, stable, reliable, and secure data set stream delivery

Stable and reliable: packet loss retransmission

Data order: sequence number and confirmation sequence number

Flow Control: Stability Window

UDP: User Datagram Protocol

For connectionless, unstable, unreliable, and unsafe datagram transmission =---more like sending and receiving text messages, UDP transmission does not require establishing a connection, the transmission efficiency is higher, and it is relatively reliable in a stable LAN internal environment

Introduction to UDP communication related functions

Receive information function

ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,

Structure sockaddr *src_addr, socklen_t *addrlen);

Function description: Receive message

Parameter Description:

sockfd socket

buf The buffer to accept

len the length of the buffer

Flags flag bit is usually filled with 0

src_addr original address outgoing parameters

addrlen sender address length

return value

Success: Returns the number of bytes read

Failure: Return -1 and set errno

Calling this function is equivalent to the recv+accept function of TCP communication.

Send data function

ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,

const struct sockaddr *dest_addr, socklen_t addrlen);

Function description: Send data

Parameter Description:

sockfd socket

dest_addr destination address

addrlen destination address length

return value

Success: Returns the number of bytes written

Failure: Return -1, set errno

2. UDP server and client development process

1. Server-side development process

(1) Create socket (man 7 udp)

udp_socket = socket(AF_INET, SOCK_DGRAM, 0);

SOCK_DGRAM: For connectionless, insecure, unreliable, datagram transmission

(2) Binding-Binding

(3) and (1)

{

Read data

n = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr *) &client,&len);

send data

sendto(cfd,buf,n,0,(struct sockaddr *)&client,len);

}

(4)//Close the file descriptor

Close position (CFD)

2. Client development process

(1) Create socket (man 7 udp)

udp_socket = socket(AF_INET, SOCK_DGRAM, 0);

(2) and (1)

     {

              //send data

              sendto(cfd,buf,n,0,(struct sockaddr *) &client,&len);

              //Read data

              n = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr *) &client,&len);

      }

(3)Close the socket

close(cfd)

3. Client and server code development cases

1.UDP server code development

code

//udp服务端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <ctype.h>

int main()
{
	int cfd = socket(AF_INET,SOCK_DGRAM,0);
	if(cfd < 0)
	{
		perror("sock error");
		return -1;
	}
	
	struct sockaddr_in serv;
	struct sockaddr_in client;
	bzero(&serv,sizeof(serv));
	serv.sin_family = AF_INET;
	serv.sin_port = htons(8888);
	serv.sin_addr.s_addr = htonl(INADDR_ANY);
	bind(cfd,(struct sockaddr *)&serv,sizeof(serv));
	
	int i;
	int n;
	socklen_t len;
	char buf[1024];
	
	while(1)
	{
		memset(buf,0x00,sizeof(buf));
		len = sizeof(client);
		n = recvfrom(cfd, buf,sizeof(buf),0,(struct sockaddr *)&client,&len);
		
		for(i = 0;i < n;i ++)
		{
			buf[i] = toupper(buf[i]);
		}
		
		printf("[%d]: n == [%d],buf == [%s]\n",ntohs(client.sin_port),n,buf);
		sendto(cfd, buf,n,0,(struct sockaddr *)&client,len);
	}
    close(cfd);
    return 0;
}

Show results

Run the server

 Usenc -u 127.1 8888 to connect to the server

 Command for all network connections and processes and filters the results to only show connections or processes related to port 8888

2.UDP client code development

code

//udp服务端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <ctype.h>
 
int main()
{
    //创建socket
    int cfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(cfd<0)
    {
        perror("socket error");
        return -1;
    }
 
    //绑定
    struct sockaddr_in serv;
    serv.sin_family = AF_INET;
    serv.sin_port = htons(8888);
    inet_pton(AF_INET, "127.0.0.1", &serv.sin_addr.s_addr);
 
    int n;

    char buf[1024];
    while(1)
    {
        //读取标准输入 
        memset(buf, 0x00, sizeof(buf));
        n  = read(STDIN_FILENO,buf,sizeof(buf));
        
        //发送数据
        sendto(cfd, buf, n, 0, (struct sockaddr *)&serv, sizeof(serv));
        
        
        //读取数据
        memset(buf, 0x00, sizeof(buf));
        n = recvfrom(cfd, buf, sizeof(buf), 0, NULL, NULL);
        printf("n==[%d], buf==[%s]\n", n, buf);
        
    }
 
    //关闭套接字
    close(cfd);
 
    return 0;
}

operation result

Guess you like

Origin blog.csdn.net/qq_64691289/article/details/134170748