gust_c
gust_c
采纳率0%
2015-09-08 02:32 阅读 1.7k

C语言udp协议,局域网下,这两段代码为什么不能通讯?

client.c 发送消息

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<errno.h>
#include<unistd.h>
#include<sys/wait.h>
#include<signal.h>
#include<stdio.h>
#include<string.h>
int main (void)
{
    int sock ;
    if((sock=socket(PF_INET,SOCK_DGRAM,0)) <0)
    {
        perror("socket");
        return 1 ;
    }
    struct sockaddr_in  servaddr;
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family =  AF_INET;
    servaddr.sin_port = htons(12345);
    servaddr.sin_addr.s_addr = inet_addr("192.168.0.104");

    char  str[4]="123";
    int i = 1;
    while (i++!= 100000000)
    {
        sendto(sock,str, sizeof(str), 0,(struct sockaddr *)&servaddr, sizeof(servaddr));
    }
    close(sock);

    return 0;
}


server.c收消息

 #include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<errno.h>
#include<unistd.h>
#include<sys/wait.h>
#include<signal.h>
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define MAXBUF 1024
int main (void)
{
    int listener,kdpfd,nfds,n;
    socklen_t len;
    len = sizeof(struct sockaddr_in);
    struct sockaddr_in my_addr,peer_addr;
    clock_t start,finish;
    double time;
    if((listener=socket(PF_INET,SOCK_DGRAM,0))==-1)
    {
        perror(" socket ");
        exit(1);
    }
    int  opt=SO_REUSEADDR;
    if(setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt))==-1)
    {
        perror("setsockopt");
        exit(1);
    }
    bzero(&my_addr,sizeof(my_addr));
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(12345);
    my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    if((bind(listener,(struct sockaddr *)&my_addr,sizeof (struct sockaddr))==-1))
    {
        perror("bind");
        exit(1);
    }
    int cnt;
    int i;
    for(i= 0 ;i<10;i++)
    {
        cnt = 0;
        start = clock();
        while(1)
        {
            recvfrom(listener,0,0,0,(struct sockaddr *)&peer_addr,&len);
            ++cnt;
            if(cnt == 100000)
                break;
        }
        finish = clock();
        time +=  (double)(finish - start)/CLOCKS_PER_SEC;
    }
    printf("%lf \n",time/10);
    close(listener);
    close(kdpfd);
    return 0;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • dasgk 世纪殇 2015-09-08 03:13

    每一步操作都检测一下返回值,看看是否有哪个api执行失败了,确定问题之后进行解决

    点赞 评论 复制链接分享
  • huimiezhex huimiezhex 2015-09-08 03:30

    其实碰到这样到问题,自己调试一下就可以了。并且setsockopt那个字段一般用于tcp,作为闲时可重用到
    客户端:

     #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<errno.h>
    #include<unistd.h>
    #include<sys/wait.h>
    #include<signal.h>
    #include<stdio.h>
    #include<string.h>
    int main (void)
    {
        int sock ;
        if((sock=socket(PF_INET,SOCK_DGRAM,0)) <0)
        {
            perror("socket");
            return 1 ;
        }
        struct sockaddr_in  servaddr;
        bzero(&servaddr,sizeof(servaddr));
        servaddr.sin_family =  AF_INET;
        servaddr.sin_port = htons(12345);
        servaddr.sin_addr.s_addr = inet_addr("192.168.13.2");
    
        char  str[4]="123";
        int i = 0;
        while (i++!= 10)
        {
            sendto(sock,str, sizeof(str), 0,(struct sockaddr *)&servaddr, sizeof(servaddr));
        printf("sendto!\n");
        }
        close(sock);
    
        return 0;
    }
    
    

    服务器
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    int main (void)
    {
    int sock ;
    if((sock=socket(PF_INET,SOCK_DGRAM,0)) <0)
    {
    perror("socket");
    return 1 ;
    }
    struct sockaddr_in servaddr;
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(12345);
    servaddr.sin_addr.s_addr = inet_addr("192.168.13.2");

    char  str[4]="123";
    int i = 0;
    while (i++!= 10)
    {
        sendto(sock,str, sizeof(str), 0,(struct sockaddr *)&servaddr, sizeof(servaddr));
    printf("sendto!\n");
    }
    close(sock);
    
    return 0;
    

    }

    
    
    点赞 评论 复制链接分享
  • huimiezhex huimiezhex 2015-09-08 03:32

    上面服务器代码弄到有问题:
    服务器:

      #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<errno.h>
    #include<unistd.h>
    #include<sys/wait.h>
    #include<signal.h>
    #include<stdio.h>
    #include<fcntl.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<string.h>
    #include<time.h>
    #define MAXBUF 1024
    int main (void)
    {
        int listener,kdpfd,nfds,n;
        socklen_t len;
        char buf[100];
        len = sizeof(struct sockaddr_in);
        struct sockaddr_in my_addr,peer_addr;
        clock_t start,finish;
        double time;
        if((listener=socket(PF_INET,SOCK_DGRAM,0))<=-1)
        {
            perror(" socket ");
            exit(1);
        }
        int  opt=SO_REUSEADDR;
        if(setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt))==-1)
        {
            perror("setsockopt");
            exit(1);
        }
        bzero(&my_addr,sizeof(my_addr));
        my_addr.sin_family = AF_INET;
        my_addr.sin_port = htons(12345);
        my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        if((bind(listener,(struct sockaddr *)&my_addr,sizeof (struct sockaddr))==-1))
        {
            perror("bind");
            exit(1);
        }
        int cnt;
        int i;
        for(i= 0 ;i<10;i++)
        {
            cnt = 0;
            start = clock();
            while(1)
            {
            memset(buf, 0, sizeof(buf));
                recvfrom(listener,buf,sizeof(buf),0,(struct sockaddr *)&peer_addr,&len);
            printf("buf:%s\n", buf);
                ++cnt;
                if(cnt == 10)
                    break;
            }
            finish = clock();
            time +=  (double)(finish - start)/CLOCKS_PER_SEC;
        printf("%lf \n",time/10);
        }
        printf("%lf \n",time/10);
        close(listener);
        close(kdpfd);
        return 0;
    }
    
    

    结果:

    root@mingji:/home/mingji/share/test# ./server
    buf:123
    buf:123
    buf:123
    buf:123
    buf:123
    buf:123
    buf:123
    buf:123
    buf:123
    buf:123
    0.000000

    点赞 评论 复制链接分享
  • huimiezhex huimiezhex 2015-09-08 03:33

    上面服务器代码弄到有问题:
    服务器:

      #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<errno.h>
    #include<unistd.h>
    #include<sys/wait.h>
    #include<signal.h>
    #include<stdio.h>
    #include<fcntl.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<string.h>
    #include<time.h>
    #define MAXBUF 1024
    int main (void)
    {
        int listener,kdpfd,nfds,n;
        socklen_t len;
        char buf[100];
        len = sizeof(struct sockaddr_in);
        struct sockaddr_in my_addr,peer_addr;
        clock_t start,finish;
        double time;
        if((listener=socket(PF_INET,SOCK_DGRAM,0))<=-1)
        {
            perror(" socket ");
            exit(1);
        }
        int  opt=SO_REUSEADDR;
        if(setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt))==-1)
        {
            perror("setsockopt");
            exit(1);
        }
        bzero(&my_addr,sizeof(my_addr));
        my_addr.sin_family = AF_INET;
        my_addr.sin_port = htons(12345);
        my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        if((bind(listener,(struct sockaddr *)&my_addr,sizeof (struct sockaddr))==-1))
        {
            perror("bind");
            exit(1);
        }
        int cnt;
        int i;
        for(i= 0 ;i<10;i++)
        {
            cnt = 0;
            start = clock();
            while(1)
            {
            memset(buf, 0, sizeof(buf));
                recvfrom(listener,buf,sizeof(buf),0,(struct sockaddr *)&peer_addr,&len);
            printf("buf:%s\n", buf);
                ++cnt;
                if(cnt == 10)
                    break;
            }
            finish = clock();
            time +=  (double)(finish - start)/CLOCKS_PER_SEC;
        printf("%lf \n",time/10);
        }
        printf("%lf \n",time/10);
        close(listener);
        close(kdpfd);
        return 0;
    }
    
    

    结果:

    root@mingji:/home/mingji/share/test# ./server
    buf:123
    buf:123
    buf:123
    buf:123
    buf:123
    buf:123
    buf:123
    buf:123
    buf:123
    buf:123
    0.000000

    点赞 评论 复制链接分享

相关推荐