实现udp服务器和客户端的通信

首先是服务器的搭建:

1.创建套接字文件 socket(AF_INET,SOCK_DGRAM,0);

2.用truct sockaddr_in此数据结构用做bind、connect、recvfrom、sendto等函数的参数,指明服务器地址信息。(truct sockaddr_in service_addr)详细配置 (truct sockaddr_in client_addr)由于不知道客户端地址信息,所以创建,用来存储等待客户端发消息从而获得的地址信息

3.将服务器地址信息绑定到套接字文件bind(socfd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr))

4.等待客户端主动连接recvfrom()

5.发送信息给客户端sendto()

第四步放在主线程中,第五步放在子线程中完成。

程序:

int main(int argc,char *argv[]) { pthread_t tid; int ret=53; int on=1; char rbuff[1024]={0}; ssize_t r_len; socklen_t len; struct sockaddr_in serv_addr; len = sizeof(cli_addr) ; bzero((char*)&cli_addr,sizeof(cli_addr)); bzero((char*)&serv_addr,sizeof(serv_addr)); socfd = socket(AF_INET,SOCK_DGRAM,0); if(socfd == -1) { printf(“create socket fail\n”); } else{printf(“create socket success\n”); } serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(SERV_PORT); serv_addr.sin_addr.s_addr = inet_addr(SERVICE_IP); setsockopt(socfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); ret = bind(socfd,(SA*)&serv_addr,sizeof(SA)); if(ret == -1) { printf(“bind fail\n”); } else if(ret==0){printf(“bind succeed\n”);} pthread_create(&tid,NULL,pthread1,NULL); while(1) { r_len = recvfrom(socfd,rbuff,sizeof(rbuff),0,(SA*)&cli_addr,&len);//can get client address infomation if(strncmp(rbuff,”quit”,4)==0) { pthread_exit(NULL); break; } if(r_len == -1) {printf(“rec error”);} else{printf(“from client:%s\n”,rbuff);} bzero(rbuff,sizeof(rbuff)); } close(socfd); return 0; } void *pthread1(void *arg) { char sbuff[1024]={0}; ssize_t s_len; while(1) { printf(“service:”); scanf(“%s”,sbuff); s_len = sendto(socfd,sbuff,sizeof(sbuff),0,(SA*)&cli_addr,sizeof(cli_addr)); if(s_len == -1) {printf(“send error”);} //else{printf(“send success\n”);} bzero(sbuff,sizeof(sbuff)); } }
<

客户端:

1.创建套接字文件 socket(AF_INET,SOCK_DGRAM,0);

2.用truct sockaddr_in此数据结构用做bind、connect、recvfrom、sendto等函数的参数,指明服务器地址信息。(truct sockaddr_in 变量名)

3.发送信息给客户端

4.等待服务器发送信息

第三步放在主线程中,第四步放在子线程中完成。

程序:

int main(void) { pthread_t tid; char sbuff[1024]={0}; ssize_t s_len; socfd = socket(AF_INET,SOCK_DGRAM,0); if(socfd == -1) { printf(“create socket fail\n”); } else{printf(“create success\n”);} bzero((char*)&serv_addr,sizeof(serv_addr)); //configure serv_addr serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(SERV_PORT); //serv_addr.sin_addr.s_addr=INADDR_ANY; serv_addr.sin_addr.s_addr = inet_addr(SERVICE_IP); pthread_create(&tid,NULL,pthread1,NULL); while(1) { printf(“client1:”); scanf(“%s”,sbuff); s_len=sendto(socfd,sbuff,sizeof(sbuff),0,(struct sockaddr*)&serv_addr,sizeof(serv_addr));/// if(strncmp(sbuff,”quit”,4)==0) { pthread_exit(NULL); break; } bzero(sbuff,sizeof(sbuff)); if(s_len == -1) {printf(“send error\n”);} //else{printf(“send succeed\n”);} } close(socfd); return(0); } void *pthread1(void *arg) { ssize_t r_len; socklen_t len; char rbuff[1024]={0}; len = sizeof(serv_addr); while(1) { r_len=recvfrom(socfd,rbuff,sizeof(rbuff),0,(struct sockaddr*)&serv_addr,&len); if(r_len == -1) {printf(“rec error”);} else{printf(“from server:%s\n”,rbuff);} bzero(rbuff,sizeof(rbuff)); }
<

}

需要注意的是:

1.端口号选取尽量大一点,以防被其他udp进程占用,0~65535,我刚开始选取666,无法实现正常功能,改成8888后能正常实现功能

2.服务器bind经常失败.解决方法:在bind设置SO_REUSEADDR套接字选项。

const int on=1;

setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));

这个使服务端(server1)主动关闭后可以立即重启。

我是菜鸟,程序还有很多不足,见谅哈!

免责声明:文章内容来自互联网,本站不对其真实性负责,也不承担任何法律责任,如有侵权等情况,请与本站联系删除。
转载请注明出处:实现udp服务器和客户端的通信 https://www.yhzz.com.cn/a/15468.html

上一篇 2023-05-12 20:38:11
下一篇 2023-05-12 20:42:10

相关推荐

联系云恒

在线留言: 我要留言
客服热线:400-600-0310
工作时间:周一至周六,08:30-17:30,节假日休息。