基于UDP的回声服务器/客户端

//服务器端代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAXN 1024
void error_handling(const char *str)
{
	fputs(str,stderr);
	fputc('\n',stderr);
	exit(1);
}

int main(int argc,char *argv[])
{
	int serv_sock;
	int n=5,str_len;
	struct sockaddr_in serv_addr;
	struct sockaddr_in clnt_addr;
	socklen_t clnt_addr_size;
	char msg[MAXN];	
	if(argc!=2)
	{
		printf("Usage:%s <port>\n",argv[0]);	
		exit(1);
	}
	serv_sock=socket(PF_INET,SOCK_DGRAM,0);		//第一步:注意是SOCK_DGRAM
	if(serv_sock==-1)
		error_handling("socket() error");
	
	memset(&serv_addr,0,sizeof(serv_addr));
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
	serv_addr.sin_port=htons(atoi(argv[1]));
	
	if(bind(serv_sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr))==-1)		//第二步
		error_handling("bind() error");
	
	while(n--)
	{
		clnt_addr_size=sizeof(clnt_addr);
		str_len=recvfrom(serv_sock,msg,MAXN,0,(struct sockaddr*)&clnt_addr,&clnt_addr_size);	//接收数据
		sendto(serv_sock,msg,str_len,0,(struct sockaddr*)&clnt_addr,clnt_addr_size);	//发送数据	
	}
	close(serv_sock);
	return 0;
}


//客户端代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAXN 1024
void error_handling(const char *str)
{
	fputs(str,stderr);
	fputc('\n',stderr);
	exit(1);
}

int main(int argc,char *argv[])
{
	int clnt_sock,str_len;
	struct sockaddr_in serv_adr,from_adr;
	socklen_t from_adr_sz;
	char msg[]="HelloWorld";
	char store[1024];
	if(argc!=3)
	{
		printf("ERROR\n");
		exit(1);
	}
	clnt_sock=socket(PF_INET,SOCK_DGRAM,0);		//第一步
	if(clnt_sock==-1)
		error_handling("socket() error");
	
	memset(&serv_adr,0,sizeof(serv_adr));
	serv_adr.sin_family=AF_INET;
	serv_adr.sin_addr.s_addr=inet_addr(argv[1]);
	serv_adr.sin_port=htons(atoi(argv[2]));
	
	str_len=sendto(clnt_sock,msg,strlen(msg),0,(struct sockaddr*)&serv_adr,sizeof(serv_adr));	//发送数据
	from_adr_sz=sizeof(from_adr);
	recvfrom(clnt_sock,store,str_len,0,(struct sockaddr*)&from_adr,&from_adr_sz);	//接收数据
	store[str_len]='\0';
	printf("Message from server: %s\n",store);	
	close(clnt_sock);
	return 0;
}


可以使用IPv6的服务器客户端套接字来实现基于IPv6的UDP回声程序。以下是示例代码: 服务器端代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define BUF_SIZE 1024 int main(int argc, char **argv) { struct sockaddr_in6 server_addr, client_addr; int server_sockfd, client_sockfd; socklen_t sin_size; char buf[BUF_SIZE]; int n; memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin6_family = AF_INET6; server_addr.sin6_port = htons(12345); server_addr.sin6_addr = in6addr_any; server_sockfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); bind(server_sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)); while (1) { sin_size = sizeof(client_addr); n = recvfrom(server_sockfd, buf, BUF_SIZE, 0, (struct sockaddr *) &client_addr, &sin_size); sendto(server_sockfd, buf, n, 0, (struct sockaddr *) &client_addr, sin_size); } close(server_sockfd); return 0; } ``` 客户端代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define BUF_SIZE 1024 int main(int argc, char **argv) { struct sockaddr_in6 server_addr; int client_sockfd; socklen_t sin_size; char buf[BUF_SIZE]; int n; memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin6_family = AF_INET6; server_addr.sin6_port = htons(12345); inet_pton(AF_INET6, "::1", &(server_addr.sin6_addr)); client_sockfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); while (1) { printf("Enter a string: "); fgets(buf, BUF_SIZE, stdin); sendto(client_sockfd, buf, strlen(buf), 0, (struct sockaddr *) &server_addr, sizeof(server_addr)); sin_size = sizeof(server_addr); n = recvfrom(client_sockfd, buf, BUF_SIZE, 0, (struct sockaddr *) &server_addr, &sin_size); buf[n] = '\0'; printf("Echo: %s", buf); } close(client_sockfd); return 0; } ``` 在上面的代码中,服务器绑定到IPv6 any address,也就是将接收所有来自IPv6客户端的请求。客户端连接到本地主机的IPv6环回地址“::1”(也可以使用本地链路地址来指定服务器地址)。客户端发送字符串到服务器服务器然后将其传回客户端,类似于一个回声室。注意这是一个基于UDP协议的实现,不提供重传和丢包处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值