UDP socket data transmission

About socket configuration:

        1. Create a sokcet socket

2. Bind the address and port for the created socket, specify the transmission protocol, generally use AF_INET, and the data transmission format is generally (SOCK_STREAM, SOCK_DGRAM)

        3. Open the file identifier and start sending data

udp client CODE:

<pre name="code" class="html">#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
	
void client(int sock){
	//2, set the client socket parameters
	struct sockaddr_in servaddr;
	memset(&servaddr, 0, sizeof(servaddr));
	//3, set the transmission protocol, port and destination address
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(5188);
	servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

	//4. Create send and receive buffered byte arrays
	int ret;
	char sendbuf[1024] = {0};
	char recvbuf[1024] = {0};
	//5. Start sending data (here is sending data from standard input)
	while(fgets(sendbuf, sizeof(sendbuf), stdin) != NULL){
		sendto(sock, sendbuf, strlen(sendbuf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
		//6, start to receive data, receive the data returned by the server
		ret = recvfrom(sock, recvbuf, sizeof(recvbuf), 0, NULL, NULL);
		if (ret == -1) {
			perror("recvfrom");
			exit(EXIT_FAILURE);
		}

		//fputs(recvbuf, stdout); //Write the data returned by the server to standard output
		
		//7, clear the send and receive cache
		memset(sendbuf, 0, sizeof(sendbuf));
		memset(recvbuf, 0, sizeof(recvbuf));
	}
	//8, close the socket stream
	close(sock);
}

int main(void){
	int sock;
	//1. Create the client's socket
	if((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0){
		perror("socket");
		exit(EXIT_FAILURE);
	}
	client(sock);
	return 0;
}


 
 

UDP server CODE:

 
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

void server(int sock){
	//1. Create a receive buffer byte array
	char recvbuf[1024] = {0};
	//2, define socket
	struct sockaddr_in peeraddr;
	socklen_t peerlen;
	int n;
	while(1){
		//
		peerlen = sizeof(peeraddr);
		//3, clear the receive buffer array
		memset(recvbuf, 0, sizeof(recvbuf));
		//4, start receiving data
		n = recvfrom(sock, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&peeraddr, &peerlen);
		//5. Determine whether to receive complete data
		if(n == -1){
				perror("recvfrom");
				exit(EXIT_FAILURE);
		}else if(n > 0){
			//6. Successfully receive data, write cached data to standard output, and print to console
			fputs(recvbuf, stdout);
			//7. I want the client to send back the received data (return the data input by the client)
			sendto(sock, recvbuf, n, 0, (struct sockaddr *)&peeraddr, peerlen);
		}
	}
	//close socket
	close(sock);
}

int main(void){
		int sock;
		//create server socket
		if((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0){
			perror("socket error");
			exit(EXIT_FAILURE);
		}
		//Set server socket parameters
		struct sockaddr_in servaddr;
		memset(&servaddr, 0, sizeof(servaddr));
		//Set the protocol, port and address of the socket
		servaddr.sin_family = AF_INET;
		servaddr.sin_port = htons(5188);
		servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	
		//Bind the socket to the address
		if(bind(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){
			perror("bind error");
			exit(EXIT_FAILURE);
		}
		server(sock);
		return 0;
}





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325827648&siteId=291194637