• 基于UDP套接字编程实例


    data.h

     1 #ifndef DATA_H
     2 #define DATA_H
     3 #include <stdio.h>
     4 #include <string.h>
     5 #include <sys/types.h>
     6 #include <sys/stat.h>
     7 #include <sys/socket.h>
     8 #include <stdlib.h>
     9 #include <unistd.h>
    10 #include <netinet/in.h>
    11 #include <arpa/inet.h>
    12 #include <netdb.h>
    13 #include <errno.h>
    14 #define PORT 4578
    15 #define maxn 100
    16 
    17 #endif

    server.c

     1 #include "data.h"
     2 int main()
     3 {
     4     int sockfd;
     5     struct sockaddr_in server;
     6     struct sockaddr_in client;
     7     socklen_t len;
     8     int num;
     9     char buf[maxn];
    10     if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
    11     {
    12         fprintf(stderr,"socket fail,error:%s
    ",strerror(errno));
    13         return -1;
    14     }
    15     bzero(&server,sizeof(server));
    16     server.sin_family = AF_INET;
    17     server.sin_port = htons(PORT);
    18     server.sin_addr.s_addr = htonl(INADDR_ANY);
    19     if(bind(sockfd,(struct sockaddr*)&server,sizeof(server)) == -1)
    20     {
    21         fprintf(stderr,"bind fail,error:%s
    ",strerror(errno));
    22         return -1;
    23     }
    24     len = sizeof(client);
    25     bzero(&buf,sizeof(buf));
    26     while(1)
    27     {
    28         num = recvfrom(sockfd,buf,maxn,0,(struct sockaddr*)&client,&len);
    29         if(num < 0)
    30         {
    31             perror("recvfrom() error!
    ");
    32             exit(1);
    33         }
    34         num--;
    35         buf[num] = '';
    36         printf("You got a message <%s> from client.
    ",buf);
    37         printf("It is ip is %s,port is %d.
    ",
    38             inet_ntoa(client.sin_addr), htons(client.sin_port));
    39         sendto(sockfd,buf,strlen(buf),0,(struct sockaddr*)&client,len);
    40         if(!strcmp(buf,"bye"))
    41             break;
    42     }
    43     close(sockfd);
    44 
    45 }

    client.c

    #include "data.h"
    int main()
    {
        int sockfd,num;
        char sendbuf[maxn];
        char recvbuf[maxn];
        struct sockaddr_in server;
        struct sockaddr_in peer;
        if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
        {
            fprintf(stderr,"socket fail,error %s
    ",strerror(errno));
            exit(1);
        }
        bzero(&server,sizeof(server));
        server.sin_family = AF_INET;
        server.sin_port = htons(PORT);    
        server.sin_addr.s_addr = inet_addr("127.0.0.1");
        bzero(&sendbuf,sizeof(sendbuf));
        bzero(&recvbuf,sizeof(recvbuf));
        while(fgets(sendbuf,sizeof(sendbuf),stdin) != NULL)
        {
            int tt = strlen(sendbuf);
            sendto(sockfd,sendbuf,tt,0,(struct sockaddr*)&server,sizeof(server));
            socklen_t len;
            recvfrom(sockfd,recvbuf,maxn,0,NULL,NULL);
            printf("receive message is :");
            fputs(recvbuf,stdout);
            printf("
    ");
            bzero(&sendbuf,sizeof(sendbuf));
            bzero(&recvbuf,sizeof(recvbuf));
        }
        close(sockfd);
        return 0;
    }

    运行结果:

  • 相关阅读:
    移动端,多屏幕尺寸高清屏retina屏适配的解决方案
    angular 关于 factory、service、provider的相关用法
    2016最新手机号码正则、身份证JS正则表达式
    凸包总结
    BZOJ 3653: 谈笑风生(DFS序+可持久化线段树)
    BZOJ 3652: 大新闻(数位DP+概率论)
    BZOJ 1062: [NOI2008]糖果雨(二维树状数组)
    纪中集训 Day 8 & Last Day
    纪中集训 Day 7
    纪中集训 Day 6
  • 原文地址:https://www.cnblogs.com/chenyang920/p/5499646.html
Copyright © 2020-2023  润新知