• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

网络编程——UDP的双向通信

武飞扬头像
在傍晚云里
帮助1

一、特点

特点:UDP之间的通信不需要实现连接,直接发送数据,不稳定,容易丢包。

二、操作步骤

①创建socket套接字;

②使用bind函数绑定自己的IP地址和端口号;

③写入对方的IP地址和端口号;

④创建线程,实行发送或接收操作;

三、相关API

  1.  
    #include <sys/types.h>
  2.  
    #include <sys/socket.h>
  3.  
     
  4.  
    ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
  5.  
    const struct sockaddr *dest_addr, socklen_t addrlen); //UDP的发送数据
  6.  
    //参数说明
  7.  
    //sockfd:文件描述符;
  8.  
    //buf:需要发送的数据;
  9.  
    //len:长度;
  10.  
    //flags:一般写0,表示可阻塞;
  11.  
    //dest_addr:发送给谁;
  12.  
    //addrlen:addr的长度;
  13.  
     
  14.  
     
  15.  
     
  16.  
    #include <sys/types.h>
  17.  
    #include <sys/socket.h>
  18.  
     
  19.  
    ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
  20.  
    struct sockaddr *src_addr, socklen_t *addrlen); //UDP的接收数据
  21.  
    //参数说明
  22.  
    //sockfd:文件描述符;
  23.  
    //buf:需要发送的数据;
  24.  
    //len:长度;
  25.  
    //flags:一般写0,表示可阻塞;
  26.  
    //src_addr:数据的来源;
  27.  
    //addrlen:addr的长度;
学新通

四、测试代码

客户端1

  1.  
    #include <stdio.h>
  2.  
    #include <sys/types.h>
  3.  
    #include <sys/socket.h>
  4.  
    #include <arpa/inet.h>
  5.  
    #include <netinet/in.h>
  6.  
    #include <stdlib.h>
  7.  
    #include <strings.h>
  8.  
    #include <string.h>
  9.  
    #include <pthread.h>
  10.  
     
  11.  
    struct sockaddr_in other;
  12.  
    void *readMsg(void *arg);
  13.  
     
  14.  
    int main(int argc,char *argv[])
  15.  
    {
  16.  
    if(argc!=3)
  17.  
    {
  18.  
    printf("User:%s<name>\n",argv[0]);
  19.  
    exit(-1);
  20.  
    }
  21.  
     
  22.  
    //创建socket套接字
  23.  
    int sockfd=socket(AF_INET,SOCK_DGRAM,0);
  24.  
    if(sockfd<0)
  25.  
    {
  26.  
    perror("socket error");
  27.  
    exit(-1);
  28.  
    }
  29.  
     
  30.  
    //bind绑定自己的IP地址和端口号
  31.  
    struct sockaddr_in myself;
  32.  
    myself.sin_family=AF_INET;
  33.  
    myself.sin_port=htons(12345);
  34.  
    myself.sin_addr.s_addr=INADDR_ANY;
  35.  
    if(bind(sockfd,(struct sockaddr*)&myself,sizeof(myself)))
  36.  
    {
  37.  
    perror("bind error");
  38.  
    exit(-1);
  39.  
    }
  40.  
     
  41.  
    //对方的IP地址和端口号
  42.  
    other.sin_family=AF_INET;
  43.  
    other.sin_port=htons(atoi(argv[2]));
  44.  
    other.sin_addr.s_addr=inet_addr(argv[1]);
  45.  
     
  46.  
    //创建线程
  47.  
    pthread_t pthread;
  48.  
    if(pthread_create(&pthread,NULL,readMsg,&sockfd))
  49.  
    {
  50.  
    perror("pthread_create error");
  51.  
    exit(-1);
  52.  
    }
  53.  
     
  54.  
    char buf[50];
  55.  
    int ret;
  56.  
    while(1)
  57.  
    {
  58.  
    bzero(buf,sizeof(buf));
  59.  
    scanf("%s",buf);
  60.  
    ret=sendto(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&other,sizeof(other)); //发送数据
  61.  
    if(ret<0)
  62.  
    {
  63.  
    perror("sendto error");
  64.  
    exit(-1);
  65.  
    }
  66.  
    }
  67.  
    return 0;
  68.  
    }
  69.  
     
  70.  
     
  71.  
    //线程的操作
  72.  
    void *readMsg(void *arg)
  73.  
    {
  74.  
    int sockfd=*(int *)arg; //将传进来的参数转换
  75.  
    char buf[50];
  76.  
    int len=sizeof(other);
  77.  
    while(1)
  78.  
    {
  79.  
    bzero(buf,sizeof(buf));
  80.  
    int ret=recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&other,&len); //接收数据
  81.  
    if(ret<0)
  82.  
    {
  83.  
    perror("recvfrom error");
  84.  
    exit(-1);
  85.  
    }
  86.  
    printf("%s\n",buf);
  87.  
    }
  88.  
    }
学新通

客户端2

  1.  
  2.  
    #include <stdio.h>
  3.  
    #include <sys/types.h>
  4.  
    #include <sys/socket.h>
  5.  
    #include <arpa/inet.h>
  6.  
    #include <netinet/in.h>
  7.  
    #include <stdlib.h>
  8.  
    #include <strings.h>
  9.  
    #include <string.h>
  10.  
    #include <pthread.h>
  11.  
     
  12.  
    struct sockaddr_in other;
  13.  
    void *writeMsg(void *arg)
  14.  
     
  15.  
    int main(int argc,char *argv[])
  16.  
    {
  17.  
    if(argc!=3)
  18.  
    {
  19.  
    printf("Useer:%s<name>\n",argv[0]);
  20.  
    exit(-1);
  21.  
    }
  22.  
     
  23.  
    //创建socket套接字
  24.  
    int sockfd=socket(AF_INET,SOCK_DGRAM,0);
  25.  
    if(sockfd<0)
  26.  
    {
  27.  
    perror("socket error");
  28.  
    exit(-1);
  29.  
    }
  30.  
     
  31.  
    //用bind函数绑定自己的IP地址和端口号
  32.  
    struct sockaddr_in myself;
  33.  
    myself.sin_family=AF_INET;
  34.  
    myself.sin_port=htons(54321);
  35.  
    myself.sin_addr.s_addr=INADDR_ANY;
  36.  
    if(bind(sockfd,(struct sockaddr*)&myself,sizeof(myself)))
  37.  
    {
  38.  
    perror("bind error");
  39.  
    exit(-1);
  40.  
    }
  41.  
     
  42.  
    //对方的IP地址和端口号
  43.  
    other.sin_family=AF_INET;
  44.  
    other.sin_port=htons(atoi(argv[2]));
  45.  
    other.sin_addr.s_addr=inet_addr(argv[1]);
  46.  
     
  47.  
    //创建线程
  48.  
    pthread_t pthread;
  49.  
    if(pthread_create(&pthread,NULL,writeMsg,&sockfd))
  50.  
    {
  51.  
    perror("pthread_create error");
  52.  
    exit(-1);
  53.  
    }
  54.  
     
  55.  
    char buf[50];
  56.  
    int len=sizeof(other);
  57.  
     
  58.  
    while(1)
  59.  
    {
  60.  
    bzero(buf,sizeof(buf));
  61.  
    int ret=recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&other,&len); //接收数据
  62.  
    if(ret<0)
  63.  
    {
  64.  
    perror("recvfrom error");
  65.  
    exit(-1);
  66.  
    }
  67.  
    printf("%s\n",buf);
  68.  
    }
  69.  
     
  70.  
    return 0;
  71.  
    }
  72.  
     
  73.  
    //线程的操作
  74.  
    void *writeMsg(void *arg)
  75.  
    {
  76.  
    int sockfd=*(int *)arg;
  77.  
    char buf[50];
  78.  
    while(1)
  79.  
    {
  80.  
    bzero(buf,sizeof(buf));
  81.  
    scanf("%s",buf);
  82.  
    int ret=sendto(sockfd,buf,sizeof(buf),0,(struct sockaddr *)&other,sizeof(other)); //发送数据
  83.  
    if(ret<0)
  84.  
    {
  85.  
    perror("sendto error");
  86.  
    exit(-1);
  87.  
    }
  88.  
    }
  89.  
    }
  90.  
     
  91.  
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfibeab
系列文章
更多 icon
同类精品
更多 icon
继续加载