tcp/ip illustrated volume2 page4 code (recvfrom function)does notwork - Networking

This is a discussion on tcp/ip illustrated volume2 page4 code (recvfrom function)does notwork - Networking ; this is the code,but after compile ,run . it doesn't work. use gdb debug,it stopped at recvfrom (line 24),no error return,but it stopped,how can I do? 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: tcp/ip illustrated volume2 page4 code (recvfrom function)does notwork

  1. tcp/ip illustrated volume2 page4 code (recvfrom function)does notwork

    this is the code,but after compile ,run .
    it doesn't work.
    use gdb debug,it stopped at recvfrom (line 24),no error return,but it
    stopped,how can I do?

    1 #include
    2 #include
    3 #include
    4 #include
    5 #include
    6 #include
    7 #include
    8 #define BUFFSIZE 150
    9 //#define int socklen_t

    10 int main()
    11 {
    12 struct sockaddr_in serv;
    13 char buff[BUFFSIZE];
    14 int sockfd,n;
    15 if((sockfd = socket(PF_INET,SOCK_DGRAM,0)) <0)
    16 printf("socket error");
    17 bzero((char*)&serv ,sizeof(serv));
    18 serv.sin_family=AF_INET;
    19 serv.sin_addr.s_addr=inet_addr("64.233.189.104");
    20 serv.sin_port=htons(13);

    21 if(sendto(sockfd,buff,BUFFSIZE,0,
    22 (struct sockaddr*)&serv,sizeof(serv))!=BUFFSIZE)
    23 printf("sendto error");

    24 if((n=recvfrom(sockfd,buff,BUFFSIZE,0,
    25 (struct sockaddr*)NULL,(int*) NULL))<2)
    26 printf("recvfrom error");
    27 buff[n-2]=0;
    28 printf("%s\n",buff);
    29 // exit(0);
    30 return 0;
    31 }

  2. Re: tcp/ip illustrated volume2 page4 code (recvfrom function)doesnot work

    westnorth wrote:
    > this is the code,but after compile ,run .
    > it doesn't work.
    > use gdb debug,it stopped at recvfrom (line 24),no error return,but it
    > stopped,how can I do?
    >
    > 1 #include
    > 2 #include
    > 3 #include
    > 4 #include
    > 5 #include
    > 6 #include
    > 7 #include
    > 8 #define BUFFSIZE 150
    > 9 //#define int socklen_t
    >
    > 10 int main()
    > 11 {
    > 12 struct sockaddr_in serv;
    > 13 char buff[BUFFSIZE];
    > 14 int sockfd,n;
    > 15 if((sockfd = socket(PF_INET,SOCK_DGRAM,0)) <0)
    > 16 printf("socket error");
    > 17 bzero((char*)&serv ,sizeof(serv));
    > 18 serv.sin_family=AF_INET;
    > 19 serv.sin_addr.s_addr=inet_addr("64.233.189.104");
    > 20 serv.sin_port=htons(13);
    >
    > 21 if(sendto(sockfd,buff,BUFFSIZE,0,
    > 22 (struct sockaddr*)&serv,sizeof(serv))!=BUFFSIZE)
    > 23 printf("sendto error");
    >
    > 24 if((n=recvfrom(sockfd,buff,BUFFSIZE,0,
    > 25 (struct sockaddr*)NULL,(int*) NULL))<2)
    > 26 printf("recvfrom error");
    > 27 buff[n-2]=0;
    > 28 printf("%s\n",buff);
    > 29 // exit(0);
    > 30 return 0;
    > 31 }



    What makes you think that there's an UDP daytime server
    at that address of Google?

    It is perfectly correct to get held in the recvfrom() if
    the addressed computer elects not to respond.

    --

    Tauno Voipio
    tauno voipio (at) iki fi

+ Reply to Thread