使用UDP建立全双工通信(使用2个线程,完成UDP双向聊天)

 一、建立头文件

#ifndef __HEAD_H__    //防止头文件被重复定义
#define __HEAD_H__   防止头文件被重复定义

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <poll.h>
#include <semaphore.h>
#include <pthread.h>

#endif    防止头文件被重复定义

(头文件所使用的函数不一定都用到,方便下次写其他代码,所以大部分都写在上面)

二、客户端

思路:    socket------>sendto----->pthread(建立两个线程,一个负责发送,一个负责接收)------>close

使用自己的IP号

#include  "head.h"

struct sockaddr_in ser;

int init_udp_cli(char *ip ,unsigned short port)
{
    int sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(-1 == sockfd)
    {
    perror("fail socket");
    return -1;
    }
    ser.sin_family = AF_INET;
    ser.sin_port = htons(port);
    ser.sin_addr.s_addr = inet_addr(ip);
    return sockfd;
    }

void *thread1(void *arg)
{   
    int sockfd = *(int *)arg;
    char p[1024]={0};
    while(1)
    {
    fgets(p,sizeof(p),stdin);
    sendto(sockfd, p, strlen(p), 0, (struct sockaddr *)&ser, sizeof(ser));
    }
}

void *thread2(void *arg)
{   
    int sockfd = *(int *)arg;
    while(1)
    {
    char buff[1024] = {0};
    recvfrom(sockfd, buff, sizeof(buff), 0, NULL, NULL);
    printf("ser------cli: %s\n", buff);
    }
}
int main(int argc, char const *argv[])
{

    int sockfd = init_udp_cli("192.168.22.25",50000);

    sendto(sockfd,"hello",5,0,(struct sockaddr *)&ser,sizeof(ser));

    pthread_t tidsend;
    pthread_t tidresv;

    pthread_create(&tidsend,NULL,thread1,&sockfd);
    pthread_create(&tidresv,NULL,thread2,&sockfd);

    pthread_join(tidsend,NULL);
    pthread_join(tidresv,NULL);
    close(sockfd);
    return 0;
}

三、服务端

思路:    socket------>recvfrom----->pthread(建立两个线程,一个负责发送,一个负责接收)------>close

使用自己的IP号

#include  "head.h"

struct sockaddr_in cli;
socklen_t clilen = sizeof(cli);

int init_udp_ser(char *ip ,unsigned short port)
{
    int sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(-1 == sockfd)
    {
    perror("fail socket");
    return -1;
    }

    struct sockaddr_in ser;

    ser.sin_family = AF_INET;
    ser.sin_port = htons(port);
    ser.sin_addr.s_addr = inet_addr(ip);

    int ret = bind(sockfd,(struct sockaddr *)&ser,sizeof(ser));
    if(-1 == ret)
    {
        perror("fail bind");
        return -1;
    }
    return sockfd;
    }

void *thread1(void *arg)
{   
    int sockfd = *(int *)arg;
    while(1)
    {
    char p[1024]={0};
    fgets(p,sizeof(p),stdin);
    sendto(sockfd,p,strlen(p),0,(struct sockaddr *)&cli,sizeof(cli));
    }

}

void *thread2(void *arg)
{         
    int sockfd = *(int *)arg;
    while(1)
    {
    char buff[1024] = {0};
    recvfrom(sockfd,buff,sizeof(buff),0,NULL,NULL);
    printf("cli-----ser: %s\n", buff);
    }
}

int main(int argc, char const *argv[])
{

    int sockfd = init_udp_ser("192.168.22.25",50000);

    char buff[1024] = {0};
    recvfrom(sockfd,buff,sizeof(buff),0,(struct sockaddr *)&cli,&clilen);

    pthread_t tidsend;
    pthread_t tidresv;
    
    pthread_create(&tidsend,NULL,thread1,&sockfd);
    pthread_create(&tidresv,NULL,thread2,&sockfd);

    pthread_join(tidsend,NULL);
    pthread_join(tidresv,NULL);
    close(sockfd);
    return 0;
}

四、运行结果

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值