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 <sys/types.h>
2 #include <sys/socket.h>
3 #include <netinet/in.h>
4 #include <arpa/inet.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
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 }
Re: tcp/ip illustrated volume2 page4 code (recvfrom function)doesnot work
westnorth wrote:[color=blue]
> 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 <sys/types.h>
> 2 #include <sys/socket.h>
> 3 #include <netinet/in.h>
> 4 #include <arpa/inet.h>
> 5 #include <stdio.h>
> 6 #include <stdlib.h>
> 7 #include <string.h>
> 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 }[/color]
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