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

Tcp、UDP模型分别搭建服务器和客户端并且实现它们:间的数据接受和发送

武飞扬头像
22091shenhaonan
帮助1

 Tcp模型服务器搭建代码:

  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 <unistd.h>
  7.  
    #include <string.h>
  8.  
    #define ERR_MSG(Msg) do{\
  9.  
    fprintf(stderr,"__%d__:",__LINE__);\
  10.  
    perror(Msg);\
  11.  
    }while(0)
  12.  
    #define IP "192.168.8.10"
  13.  
    #define PORT 8888
  14.  
    int main(int argc, const char *argv[])
  15.  
    {
  16.  
    //创建字节流式套接字
  17.  
    int sdp=socket(AF_INET,SOCK_STREAM,0);
  18.  
    if(sdp<0)
  19.  
    {
  20.  
    ERR_MSG("sdp");
  21.  
    return -1;
  22.  
    }
  23.  
    //允许端口被重复使用
  24.  
    int reuse = 1;
  25.  
    if(setsockopt(sdp, SOL_SOCKET, SO_REUSEADDR, &reuse ,sizeof(reuse)) < 0)
  26.  
    {
  27.  
    ERR_MSG("setsockopt");
  28.  
    return -1;
  29.  
    }
  30.  
    printf("允许端口快速重用\n");
  31.  
    sin.sin_family=AF_INET;
  32.  
     
  33.  
    //将IP号和端口号绑定在套接字文件描述符
  34.  
    struct sockaddr_in sin;
  35.  
    sin.sin_family=AF_INET;
  36.  
    sin.sin_port=htons(PORT);
  37.  
    sin.sin_addr.s_addr=inet_addr(IP);
  38.  
    socklen_t addrlen=sizeof(sin);
  39.  
    if(bind(sdp,(struct sockaddr *)&sin,addrlen)<0)
  40.  
    {
  41.  
    ERR_MSG("bind");
  42.  
    return -1;
  43.  
    }
  44.  
    printf("绑定成功\n");
  45.  
    //将套接字设置为监听状态
  46.  
    if(listen(sdp,128)<0)
  47.  
    {
  48.  
    ERR_MSG("listen");
  49.  
    }
  50.  
    printf("监听成功\n");
  51.  
    struct sockaddr_in bin;
  52.  
    socklen_t addrlen1=sizeof(bin);
  53.  
    //生成新的文件描述符,存储链接成功的客户信息
  54.  
    int newsdp;
  55.  
    newsdp=accept(sdp,(struct sockaddr *)&bin,&addrlen1);
  56.  
    if(newsdp<0)
  57.  
    {
  58.  
    ERR_MSG("accept");
  59.  
    return -1;
  60.  
    }
  61.  
    while(1)
  62.  
    {
  63.  
    //接受数据
  64.  
    char buf[128];
  65.  
    bzero(buf,sizeof(buf));
  66.  
    ssize_t rcv;
  67.  
    rcv=recv(newsdp,buf,sizeof(buf),0);
  68.  
    if(rcv<0)
  69.  
    {
  70.  
    ERR_MSG("recv");
  71.  
    return -1;
  72.  
    }
  73.  
    if(rcv==0)
  74.  
    {
  75.  
    printf("对方客户端关闭\n");
  76.  
    break;
  77.  
    }
  78.  
    printf("对方客户端ip=[%s] port=%d newsdp=%d读取到的数据为%s\n",inet_ntoa(bin.sin_addr),ntohs(bin.sin_port),newsdp,buf);
  79.  
    //发送数据
  80.  
    strcat(buf,"*-*");
  81.  
    if(send(newsdp,buf,sizeof(buf),0)<0)
  82.  
    {
  83.  
    ERR_MSG("send");
  84.  
    return -1;
  85.  
    }
  86.  
    printf("发送成功\n");
  87.  
    }
  88.  
    close(sdp);
  89.  
    close(newsdp);
  90.  
    return 0;
  91.  
    }
学新通

Tcp模型客户端代码 :

  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 <unistd.h>
  7.  
    #include <string.h>
  8.  
    #define ERR_MSG(Msg) do{\
  9.  
    fprintf(stderr,"__%d__:",__LINE__);\
  10.  
    perror(Msg);\
  11.  
    }while(0)
  12.  
    #define SER_IP "192.168.8.10"
  13.  
    #define SER_PORT 8888
  14.  
    int main(int argc, const char *argv[])
  15.  
    {
  16.  
     
  17.  
    //创建套接字
  18.  
    int cdp=socket(AF_INET,SOCK_STREAM,0);
  19.  
    if(cdp<0)
  20.  
    {
  21.  
    ERR_MSG("cdp");
  22.  
    return -1;
  23.  
    }
  24.  
    /*
  25.  
    //允许端口被重复使用
  26.  
    int reuse = 1;
  27.  
    if(setsockopt(sdp, SOL_SOCKET, SO_REUSEADDR, &reuse ,sizeof(reuse)) < 0)
  28.  
    {
  29.  
    ERR_MSG("setsockopt");
  30.  
    return -1;
  31.  
    }
  32.  
    printf("允许端口快速重用\n");
  33.  
    struct sockaddr_in sin;
  34.  
    sin.sin_family=AF_INET;
  35.  
    sin.sin_port=htons(PORT);
  36.  
    sin.sin_addr.s_addr=inet_addr(IP);
  37.  
    socklen_t addrlen=sizeof(sin);
  38.  
    if(bind(sdp,(struct sockaddr *)&sin,addrlen)<0)
  39.  
    {
  40.  
    ERR_MSG("bind");
  41.  
    return -1;
  42.  
    }
  43.  
    printf("绑定成功\n");*/
  44.  
    //链接服务器
  45.  
    //服务器的地址信息
  46.  
    struct sockaddr_in sin;
  47.  
    sin.sin_family=AF_INET;
  48.  
    sin.sin_port=htons(SER_PORT);
  49.  
    sin.sin_addr.s_addr=inet_addr(SER_IP);
  50.  
    socklen_t addrlen=sizeof(sin);
  51.  
    if(connect(cdp,(struct sockaddr *)&sin,addrlen)<0)
  52.  
    {
  53.  
    ERR_MSG("connect");
  54.  
    }
  55.  
    while(1)
  56.  
    {
  57.  
    char buf[128];
  58.  
    bzero(buf,sizeof(buf));
  59.  
    //发送数据
  60.  
    printf("请输入数据");
  61.  
    fgets(buf,sizeof(buf),stdin);
  62.  
    buf[strlen(buf)-1]=0;
  63.  
    if(send(cdp,buf,sizeof(buf),0)<0)
  64.  
    {
  65.  
    ERR_MSG("send");
  66.  
    return -1;
  67.  
    }
  68.  
    printf("发送成功\n");
  69.  
    /* pthread_t tid;
  70.  
    pthread_create(&tid,NULL,callback,NULL);*/
  71.  
    //接受数据
  72.  
    ssize_t rcv;
  73.  
    rcv=recv(cdp,buf,sizeof(buf),0);
  74.  
    if(rcv<0)
  75.  
    {
  76.  
    ERR_MSG("recv");
  77.  
    return -1;
  78.  
    }
  79.  
    if(rcv==0)
  80.  
    {
  81.  
    printf("服务器关闭\n");
  82.  
    break;
  83.  
    }
  84.  
    printf("读取到的数据为%s\n",buf);
  85.  
    }
  86.  
    close(cdp);
  87.  
    return 0;
  88.  
    }
学新通

UDP模型服务器代码:

  1.  
    #include <stdio.h>
  2.  
    #include <sys/types.h>
  3.  
    #include <sys/socket.h>
  4.  
    #include <arpa/inet.h>
  5.  
    #include <strings.h>
  6.  
    #include <string.h>
  7.  
    #include <netinet/in.h>
  8.  
    #include <netinet/ip.h>
  9.  
    #include <unistd.h>
  10.  
    #define MSG_ERR(msg) do{\
  11.  
    printf("%d\n",__LINE__);\
  12.  
    perror(msg);}\
  13.  
    while(0)
  14.  
    #define PORT 3333
  15.  
    #define IP "192.168.8.10"
  16.  
    int main(int argc, const char *argv[])
  17.  
    {
  18.  
    //创建套接字文件
  19.  
    int sfd=socket(AF_INET,SOCK_DGRAM,0);
  20.  
    if(sfd<0)
  21.  
    {
  22.  
    MSG_ERR("socket");
  23.  
    return -1;
  24.  
    }
  25.  
    //绑定端口号
  26.  
    struct sockaddr_in sin;
  27.  
    sin.sin_family=AF_INET;
  28.  
    sin.sin_port=htons(PORT);
  29.  
    sin.sin_addr.s_addr=inet_addr(IP);
  30.  
    socklen_t addrlen=sizeof(sin);
  31.  
    if(bind(sfd,(struct sockaddr *)&sin,addrlen)<0)
  32.  
    {
  33.  
    MSG_ERR("bind");
  34.  
    }
  35.  
    printf("绑定成功\n");
  36.  
    while(1)
  37.  
    {
  38.  
    //接收数据
  39.  
    struct sockaddr_in cin;
  40.  
    socklen_t addrlen_cin=sizeof(cin);
  41.  
    char buf[128];
  42.  
    bzero(buf,sizeof(buf));
  43.  
    ssize_t rcv;
  44.  
    rcv=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&cin,&addrlen_cin);
  45.  
    if(rcv<0)
  46.  
    {
  47.  
    MSG_ERR("recv");
  48.  
    return -1;
  49.  
    }
  50.  
    if(rcv==0)
  51.  
    {
  52.  
    printf("对方客户端关闭\n");
  53.  
    break;
  54.  
    }
  55.  
    printf("客户端ip=%s,port=%d,接受的数据为%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
  56.  
    //发送数据
  57.  
    strcat(buf,"..");
  58.  
    if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,addrlen_cin)<0)
  59.  
    {
  60.  
    MSG_ERR("sendto");
  61.  
    }
  62.  
    printf("发送成功\n");
  63.  
    }
  64.  
    close(sfd);
  65.  
     
  66.  
    return 0;
  67.  
    }
学新通

UDP模型客户端代码:

  1.  
    #include <stdio.h>
  2.  
    #include <sys/types.h>
  3.  
    #include <sys/socket.h>
  4.  
    #include <arpa/inet.h>
  5.  
    #include <strings.h>
  6.  
    #include <string.h>
  7.  
    #include <netinet/in.h>
  8.  
    #include <netinet/ip.h>
  9.  
    #include <unistd.h>
  10.  
    #define MSG_ERR(msg) do{\
  11.  
    printf("%d\n",__LINE__);\
  12.  
    perror(msg);}\
  13.  
    while(0)
  14.  
    #define PORT 3333
  15.  
    #define IP "192.168.8.10"
  16.  
    int main(int argc, const char *argv[])
  17.  
    {
  18.  
    //创建数据报式套接字文件
  19.  
    int sfd=socket(AF_INET,SOCK_DGRAM,0);
  20.  
    if(sfd<0)
  21.  
    {
  22.  
    MSG_ERR("socket");
  23.  
    return -1;
  24.  
    }
  25.  
    //发送到的服务器基本地址信息
  26.  
    struct sockaddr_in sin;
  27.  
    sin.sin_family=AF_INET;
  28.  
    sin.sin_port=htons(PORT);
  29.  
    sin.sin_addr.s_addr=inet_addr(IP);
  30.  
    socklen_t addrlen=sizeof(sin);
  31.  
    while(1)
  32.  
    {
  33.  
    //发送数据
  34.  
    char buf[128];
  35.  
    bzero(buf,sizeof(buf));
  36.  
    printf("请输入向服务器发送的数据");
  37.  
    fgets(buf,sizeof(buf),stdin);
  38.  
    buf[strlen(buf)-1]=0;
  39.  
    if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,addrlen)<0)
  40.  
    {
  41.  
    MSG_ERR("sendto");
  42.  
    }
  43.  
    printf("发送成功\n");
  44.  
    //接收数据
  45.  
    struct sockaddr_in bin;
  46.  
    socklen_t addrlen_bin=sizeof(bin);
  47.  
    ssize_t rcv;
  48.  
    bzero(buf,sizeof(buf));
  49.  
    rcv=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&bin,&addrlen_bin);
  50.  
    if(rcv<0)
  51.  
    {
  52.  
    MSG_ERR("recv");
  53.  
    return -1;
  54.  
    }
  55.  
    if(rcv==0)
  56.  
    {
  57.  
    printf("对方服务器关闭\n");
  58.  
    break;
  59.  
    }
  60.  
    printf("接受的数据为%s\n",buf);
  61.  
    }
  62.  
    close(sfd);
  63.  
     
  64.  
    return 0;
  65.  
    }
学新通

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

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