Loading...
实现udp服务器和客户端的通信
我是会员 发布于 07/24 17:13 浏览 86

首先是服务器的搭建: 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)主动关闭后可以立即重启。

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

*本文仅代表作者观点,不代表易百纳技术社区立场。系作者授权易百纳技术社区发表,未经许可不得转载。

点赞2
收藏1
分享

精彩评论

内容存在敏感词
确定要删除此文章、专栏、评论吗?
确定
取消

关注公众号