Return to BSD News archive
Path: euryale.cc.adfa.oz.au!newshost.carno.net.au!harbinger.cc.monash.edu.au!munnari.OZ.AU!spool.mu.edu!sol.ctr.columbia.edu!news.indiana.edu!vixen.cso.uiuc.edu!ais.net!arclight.uoregon.edu!leto!hammer.uoregon.edu!newsgate.cuhk.edu.hk!newsfeeder.ust.hk!news.ust.hk!eekhpang From: eekhpang@uxmail.ust.hk (Pang Kin Hung) Newsgroups: comp.unix.bsd.freebsd.misc Subject: socket client pgm read() fail! Date: 21 Jan 1997 09:46:00 GMT Organization: Hong Kong University of Science and Technology Lines: 60 Message-ID: <5c238o$4hb@ustsu10.ust.hk> NNTP-Posting-Host: eesu20.ee.ust.hk X-Newsreader: TIN [version 1.2 PL2] Xref: euryale.cc.adfa.oz.au comp.unix.bsd.freebsd.misc:34254 -- My simple client socket program couldn't read read data sent from server pgm. The read() function always returns nonzero and then dies. It really puzzles me a lot because it can successfully connect to the predefined server port. can anybody give me some help? Thks a lot kenny ---------------------------------------------------------------------- #include <stdio.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <netdb.h> #define SERV_PORT 5000 main(int argc, char *argv[]){ int s, client_addr_len; struct sockaddr_in addr, s_addr; struct hostent *servhost; char buf[10]; int n; if (argc <2){ printf("Usage: %s host \n", argv[0]); exit(1); } if ((s=socket(AF_INET, SOCK_STREAM, 0))<0){ printf("cannot open socket\n"); exit(1); } bzero((char *) &s_addr, sizeof(s_addr)); s_addr.sin_family=AF_INET; s_addr.sin_port=htons(SERV_PORT); servhost=gethostbyname(argv[1]); if (servhost==0){ printf("could not obtain addrss of %s\n", argv[1]); exit(1); } bcopy(servhost->h_addr, (char *)&(s_addr.sin_addr.s_addr), servhost->h_length); if (connect(s, (struct sockaddr *) &s_addr, sizeof(s_addr))<0){ printf("connect failed\n"); exit(1); } if ((n=recv(s, buf, n,0))<0){ printf("read failed\n"); <--------- always fails printf("n=%d\n",n); exit(1); } printf("buf=%s, nbuf=%d",buf,n); close(s); exit(0); }