欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UDP socket 编程示例

程序员文章站 2022-03-18 20:10:45
...

本代码根据老师提供的tcp示例改写

一、UDP socket 编程模型

    UDP socket 编程示例

二、使用UDP协议实现一个简单的通讯程序

1.服务器端

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



void print_usage(char * cmd)
{
	fprintf(stderr," %s usage:\n",cmd);
	fprintf(stderr,"%s  [port]\n",cmd);

}

int main(int argc,char** argv)
{
	struct sockaddr_in server;
	struct sockaddr_in client;
	int sockfd;
	int len;
	int port;
	int sendnum;
	int recvnum;
	int opt;
	char send_buf[2048];
	char recv_buf[2048];
	int addr_len = sizeof(struct sockaddr_in);
	if ((1>argc)|| (argc >2))
	{
		print_usage(argv[0]);
		exit(1);

	}
	if (2==argc) 
	{
		port = atoi(argv[1]);
	}
	memset(send_buf,0,2048);
	memset(recv_buf,0,2048);
	opt=SO_REUSEADDR;
	if (-1==(sockfd=socket(AF_INET,SOCK_DGRAM,0)))
	{
		perror("create socket error\n");
		exit(1);
	}
	memset(&server,0,sizeof(struct sockaddr_in));
	server.sin_family = AF_INET;
   	server.sin_addr.s_addr = htonl(INADDR_ANY);
	server.sin_port = htons(port);
	if (-1==bind(sockfd,(struct sockaddr *)&server,sizeof(struct sockaddr)))
	{
		perror("bind error\n");
		exit(1);
	}
	while (1)
	{

        	if (0>(recvnum = recvfrom(sockfd,recv_buf,sizeof(recv_buf),0,(struct sockaddr *)&client,&addr_len)))
		{
			perror("recv error\n");
			//close(sockfd);
			continue;
		}
		recv_buf[recvnum]='\0';

		printf ("the message from the client is: %s\n",recv_buf);
	
		if (0==strcmp(recv_buf,"quit"))
        	{
			perror("the client break the server process\n");
			close(sockfd);
			break;
		}

		sendnum = sprintf(send_buf,"The message from client is %s\n",recv_buf);
  		sendto(sockfd,send_buf,sizeof(send_buf),0,(struct sockaddr *)&client,sizeof(client));
		continue;

	}
	close(sockfd);
	return 0;
}

2.客户端

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

#define PORT 8900


void print_usage(char * cmd)
{
	fprintf(stderr," %s usage:\n",cmd);
	fprintf(stderr,"%s IP_Addr [port]\n",cmd);

}


int main(int argc,char** argv)
{
	struct sockaddr_in server;
	int ret;
	int len;
	int port;
	int sockfd;
	int sendnum;
	int recvnum;
	char send_buf[2048];
	char recv_buf[2048];
	int addr_len = sizeof(struct sockaddr_in);
	if ((2>argc)|| (argc >3))
	{
		print_usage(argv[0]);
		exit(1);

	}
	if (3==argc) 
	{
		port = atoi(argv[2]);
	}
	if (-1==(sockfd=socket(AF_INET,SOCK_DGRAM,0)))
	{
		perror("can not create socket\n");
		exit(1);
	}

	memset(&server,0,sizeof(struct sockaddr_in));
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = inet_addr(argv[1]);
	server.sin_port = htons(port);

	if (0>(ret=connect(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr))))
	{
		perror("connect error");
		close(sockfd);
		exit(1);

	}

	//memset(send_buf,0,2048);
	//memset(recv_buf,0,2048);
	while(1)
	{
		printf("what words do  you want to tell to server:\n");
		//gets(send_buf);
		fgets(send_buf,2048,stdin);
		//sprintf(send_buf,"i am lg,thank for your servering\n");
		if (0>(len=sendto(sockfd,send_buf,sizeof(send_buf),0,(struct sockaddr *)&server,sizeof(server))))
		{
			perror("send data error\n");
			close(sockfd);
			exit(1);
		}
		if (0>(len=recvfrom(sockfd,recv_buf,sizeof(recv_buf),0,(struct sockaddr *)&server,&addr_len)))
		{
			perror("recv data error\n");
			close(sockfd);
			exit(1);
		}
		recv_buf[len]='\0';
		printf("the message from the server is:%s\n",recv_buf);		
	}
	close(sockfd);
}

三、运行结果

    UDP socket 编程示例

相关标签: socket