help!! why socket has no respone?

wang rui wang_work2002@hotmail.com
Sat Oct 19 02:04:00 GMT 2002


Hi,friends

i have written a simple client-server app to test udp socket,but they do 
not work correctly! For the server has
no respone while request coming in.

the program is below:

Client.c:

#include <stdio.h>
#include <sys/types.h>
#include <ctype.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>

int main(int argc,char* argv[]) {
  int sockfd;
  struct sockaddr_in their_addr;
  int addr_len;
  char sendbuf[8192]="\0";

  if (argc<2) { 
    printf("Usage : client HOSTADDR\n");
    exit(1);
  }

  if ((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
    printf("error in socket!\n");
    exit(1);
  }

  their_addr.sin_family=AF_INET;
  their_addr.sin_port=htons(161);
  their_addr.sin_addr.s_addr=inet_addr(argv[1]);
  bzero(&(their_addr.sin_zero),8);
  addr_len=sizeof(their_addr);
  while (fgets(sendbuf,8192,stdin)!=NULL) {
    sendto(sockfd,sendbuf,strlen(sendbuf),0,(struct 
sockaddr*)&their_addr,addr_len);
  }

  close(sockfd);
  exit(1);
}
Server.c:

#include <stdio.h>
#include <sys/types.h>
#include <ctype.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>

void pro_udp_service(int sockfd);

int main(int argc,char* argv[]) {
  int sockfd;
  struct sockaddr_in my_addr;
  int addr_len;
  struct protoent *ppe;
  int htons_port;
  if ((ppe=getprotobyname("udp"))==0) {
    printf("error in getprotobyname!\n");
    exit(1);
  }
  if ((sockfd=socket(AF_INET,SOCK_DGRAM,ppe->p_proto))==-1) {
    printf("error in socket!\n");
    exit(1);
  }

  my_addr.sin_family=AF_INET;  
  my_addr.sin_port=htons(161);
  my_addr.sin_addr.s_addr=INADDR_ANY;
  if (bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr))==-1) 
{
    printf("error in binding!\n");
    exit(1);
  }

  pro_udp_service(sockfd);
  close(sockfd);
  exit(1);
}


void pro_udp_service(int sockfd) {
  char recvbuf[8192];
  int n=0,len;
  struct sockaddr* addr;
  
  while (1) {
    len=sizeof(struct sockaddr_in);
    if ((n=recvfrom(sockfd,recvbuf,sizeof(recvbuf),0,addr,&len))==-1) {
      printf("error in recieving data!\n");
      exit(1);
    }
  }
}

with "tcpdump" i can see that the udp packet is truely sent from client to 
server,but the server doesn't work with the request.

19:35:27.487859 z.32770 > w.snmp:  [len6<asnlen49] (DF)
0x0000	 4500 0024 0000 4000 4011 a418 c0a8 0ab3	E..$..@.@.......
0x0010	 c0a8 0aad 8002 00a1 0010 23dc 3131 3131	..........#.1111
0x0020	 3131 310a 0000 0000 0000 0000 0000     	111...........

But,on the other hand,if do such change,it works fine:
Client.c:
their_addr.sin_port=htons(161); ==> their_addr.sin_port=htons(6161); /* 
portnum>1024 */

Server.c:
my_addr.sin_port=htons(161); ==> my_addr.sin_port=htons(6161);

can you tell me why this happens?It puzzles me for a long time.
and how can i write my server deamon with port number below 1024?
thanks a lot for your help.
wang

_________________________________________________________________
ÏíÓÃÊÀ½çÉÏ×î´óµÄµç×ÓÓʼþϵͳ¡ª MSN Hotmail¡£ http://www.hotmail.com 



More information about the Libc-alpha mailing list