实现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)主动关闭后可以立即重启。
我是菜鸟,程序还有很多不足,见谅哈!
- 分享
- 举报
-
浏览量:2313次2020-07-28 19:16:36
-
浏览量:4149次2020-08-20 11:05:39
-
浏览量:1764次2020-07-17 16:43:51
-
浏览量:2303次2019-12-31 09:01:27
-
浏览量:2434次2020-07-31 18:12:31
-
浏览量:2172次2019-11-22 14:11:11
-
浏览量:848次2024-07-15 11:57:20
-
浏览量:732次2024-03-01 16:04:36
-
浏览量:699次2023-08-21 11:47:40
-
浏览量:3265次2020-08-04 17:37:01
-
浏览量:3181次2022-08-26 09:50:14
-
浏览量:2270次2020-04-24 18:04:21
-
浏览量:1179次2023-06-07 14:32:02
-
浏览量:4224次2020-08-18 19:31:22
-
浏览量:1523次2023-11-21 16:33:30
-
浏览量:1452次2024-01-24 17:07:21
-
浏览量:4529次2021-04-07 20:01:21
-
浏览量:4260次2017-10-30 16:46:25
-
浏览量:1783次2019-09-06 09:28:53
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
卟留遗憾灬
感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明