2.2 Linux下實現簡單UDP通訊

2020-08-13 20:36:46

基於上一文件簡單通訊,完成迴環設計。

伺服器端實現發送數據程式碼如下:

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

#define LOCAL_IP "192.168.186.142"
#define TARGET_IP "192.168.186.142"
#define LOCAL_PORT 5000
#define TARGET_PORT 6000

//****************main function*****************************************
int main()
{
	struct sockaddr_in local_addr;
	struct sockaddr_in target_addr;
	socklen_t local_addr_size=0;
	socklen_t target_addr_size=0;
    
	int sockfd=0;
	int bind_status=0;
	int connect_status=0;

	char buff[99]={};
	int  buff_len=0;
	int  i=0;

	//create udp socket
	sockfd=socket(AF_INET,SOCK_DGRAM,0);	
	if(sockfd==-1)
	{
		printf("socket status: socket failed!\n");
		exit(1);
	}

	//bind local port/IP
	local_addr.sin_family=AF_INET;  //protocol
	local_addr.sin_port=htons(LOCAL_PORT);//port
	local_addr.sin_addr.s_addr=inet_addr(LOCAL_IP);//ip
    local_addr_size=sizeof(local_addr);
	bind_status=bind(sockfd,(struct sockaddr*)&local_addr,local_addr_size);
	if(bind_status==-1)
	{
		printf("bind status: failed!\n");
	}

	//create target port/ip
	target_addr.sin_family=AF_INET;
	target_addr.sin_port=htons(TARGET_PORT);
	target_addr.sin_addr.s_addr=inet_addr(TARGET_IP);
	target_addr_size=sizeof(target_addr);
	connect_status=connect(sockfd,(struct sockaddr*) &target_addr,target_addr_size);	
	if(connect_status==-1)
	{
		printf("connect target deveice failed!!\n");
	}
	
	//tx data --> rx data
	while(1)
	{
		memset(buff,0,99);
		printf("send data: ");
		fgets(buff,99,stdin);
		buff_len=strlen(buff);
		send(sockfd,buff,buff_len,0);

		recvfrom(sockfd,buff,99,0,(struct sockaddr*)&target_addr,&target_addr_size);
		printf("recv data: ",inet_ntoa(target_addr.sin_addr));//output Ip
		for(i=0;i<=99;i++)
		{
			if(buff[i]=='\0' || buff[i]=='\n')
			{ 
				printf("%c",buff[i]);
				break;
			}
			else
			{
				printf("%c",buff[i]);
			}
		}
	}
}

//-------------------------------------------------------------------------

用戶端將接收數據迴環,如下:

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

#define LOCAL_IP "192.168.186.142"
#define TARGET_IP "192.168.186.142"
#define LOCAL_PORT 6000
#define TARGET_PORT 5000

//****************main function*****************************************
int main()
{
	struct sockaddr_in local_addr;
	struct sockaddr_in target_addr;
	socklen_t local_addr_size=0;
	socklen_t target_addr_size=0;
    
	int sockfd=0;
	int bind_status=0;
	int connect_status=0;

	char rx_buff[99]={};
	char tx_buff[99]={};
	int  buff_len=0;
	int  i=0;

	//create udp socket
	sockfd=socket(AF_INET,SOCK_DGRAM,0);	
	if(sockfd==-1)
	{
		printf("socket status: socket failed!\n");
		exit(1);
	}

	//bind local port/IP
	local_addr.sin_family=AF_INET;  //protocol
	local_addr.sin_port=htons(LOCAL_PORT);//port
	local_addr.sin_addr.s_addr=inet_addr(LOCAL_IP);//ip
    local_addr_size=sizeof(local_addr);
	bind_status=bind(sockfd,(struct sockaddr*)&local_addr,local_addr_size);
	if(bind_status==-1)
	{
		printf("bind status: failed!\n");
	}

	//create target port/ip
	target_addr.sin_family=AF_INET;
	target_addr.sin_port=htons(TARGET_PORT);
	target_addr.sin_addr.s_addr=inet_addr(TARGET_IP);
	target_addr_size=sizeof(target_addr);
	connect_status=connect(sockfd,(struct sockaddr*) &target_addr,target_addr_size);	
	if(connect_status==-1)
	{
		printf("connect target deveice failed!!\n");
	}
	
	//rxdata --> tx
	while(1)
	{
		recv(sockfd,rx_buff,99,0);
		printf("IP%srecv data: ",inet_ntoa(target_addr.sin_addr));
		for(i=0;i<=99;i++)
		{
			if(rx_buff[i]=='\0' || rx_buff[i]=='\n')
			{
				printf("%c",rx_buff[i]);
				tx_buff[i]='\0';
				break;
			}
			else
			{
				printf("%c",rx_buff[i]);
				tx_buff[i]=rx_buff[i];
			}
		}
		
		buff_len=strlen(tx_buff);
		sendto(sockfd,tx_buff,buff_len,0,(struct sockaddr*)&target_addr,target_addr_size);

	}
}

//-------------------------------------------------------------------------