Implementation of UDP half-duplex communication under C language in Linux

C language realizes half-duplex UDP communication under Linux

1. One-way communication: also known as simplex communication, that is, there can be only one direction of communication and no interaction in the opposite direction. Radio broadcasting or cable radio broadcasting and television broadcasting belong to this type.

Unidirectional communication requires only one channel, while bidirectional alternating communication or bidirectional simultaneous communication requires two channels (one for each direction). Obviously, the two-way simultaneous communication has the highest transmission efficiency. However, it should be pointed out that although the telecommunications bureau provides two-way simultaneous communication channels for users who make calls, effective telephone conversations are generally alternate communication between the two parties. When a quarrel occurs between the two parties, it is often a two-way simultaneous communication.

2. Half-duplex communication means that data can be transmitted in two directions. But one channel only allows one-way transmission at the same time, so the meaning is called two-way alternating communication, as shown in (b) in the figure. For example, a wireless intercom is a half-duplex device that allows only one party to speak at a time.

3. Full-duplex communication refers to a data transmission method that occurs in both directions at the same time, as shown in (c) in the figure. A telephone is a full-duplex device, and both parties can talk at the same time. High-speed data communication between computers is also this way.

Two-way alternating communication is also called half-duplex communication, that is, both sides of the communication can send information, but they cannot send at the same time (of course, they cannot receive simultaneously). In this communication method, one party sends the other party to receive it, and then the other way round.

————————————————
Copyright Statement: The above content is the original link of the original article of the blogger "  Broken Ice "
: https://www.cnblogs.com/lightice/p/ 12726717.html

udp_recv.c

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>

#define IP "127.0.0.1"
#define POST 8088

int main()
{
	int cid=socket(AF_INET,SOCK_DGRAM,0);//创建socket
	if(cid<0)
	{
		perror("socket error");
		return -1;
	}
	//配置socket相关信息
	struct sockaddr_in addr = {0};
	addr.sin_family=AF_INET;
	addr.sin_addr.s_addr=inet_addr(IP);
	addr.sin_port=htons(POST);//端口
	
	char buf[128]={0};
	char str[128]={0};
	int size =sizeof(struct sockaddr_in),len;
	//绑定socket,使用bind()
	int res=bind(cid,(struct sockaddr*)&addr,sizeof(struct sockaddr));
	
	while(1)
	{
		len=recvfrom(cid,buf,sizeof(buf)-1,0,(struct sockaddr*)&addr,&size);//接收数据
		if(strncmp(buf,"quit",4)==0)
		{
			printf("Quit,I got it!\n");			
			break;
		}
			
		if(len<=0)
		{
			printf("receive error");
			return -1;
		}
		else
			printf("receive len=%d,buf=%s\n",len,buf);
		
		printf("return mas:");		
		scanf("%s",str);
		sendto(cid, str, sizeof(str)-1, 0,(struct sockaddr *)&addr,size);//返回数据
		printf("mas:%s\n",str);
	}
	close(cid);
	return 0;	
}

udp_send.c

 

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>

#define IP "127.0.0.1"
#define POST 8088

int main()
{
	int cid=socket(AF_INET,SOCK_DGRAM,0);//创建socket
	if(cid<0)
	{
		perror("socket error");
		return -1;
	}
	//配置socket相关信息
	struct sockaddr_in addr = {0};
	addr.sin_family=AF_INET;//地址族
	addr.sin_addr.s_addr=inet_addr(IP);
	addr.sin_port=htons(POST);//端口
	
	char buf[128]={0};
	char str[128]={0};	
	int size =sizeof(struct sockaddr_in),len,rec;
		printf ("Data sent:");
	while (1)
	{	
		scanf ("% s", buf); 
		len = sendto (cid, buf, sizeof (buf) -1, 0, (struct sockaddr *) & addr, size); // Send data 
		if (strncmp (buf, "quit" , 4) == 0) 
			break; 
		printf ("send mas len =% d \ n", len); 
		rec = recvfrom (cid, str, sizeof (str) -1,0, (struct sockaddr *) & addr, & size ); // Receive data 
		if (rec <= 0) 
		{ 
			printf ("Receive failed!");	 
		} 
		Else 
			printf ("The received information is:% s \ n", str); 
		memset (buf, 0, sizeof ( buf)); // Empty array contents	 
		memset (str, 0, sizeof (str)); // Empty array contents 
	} 
	close (cid); 
	return 0; 

}

 

  operation result:

 

Guess you like

Origin www.cnblogs.com/OneSongsTime/p/12751603.html