Date: Thu, 29 Apr 1999 12:38:16 -0400 (EDT) From: Naief BinTalal <naief@cyfari.com> To: Greg Campbell <greg.campbell@matrikon.com> Cc: freebsd-questions@FreeBSD.ORG Subject: Re: socket communication Message-ID: <Pine.BSF.4.05.9904291226190.90679-100000@destroyer.cyfari.com> In-Reply-To: <000f01be9323$ce6e5bf0$9308a8c0@matrikon.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, 30 Apr 1999, Greg Campbell wrote: > Hi, > > I would appreciate some words of wisdom from anyone > who has done some socket programming on FreeBSD. > > The question is this: > Is there anything special about connecting to a > socket from FreeBSD? I am getting an error from > the "connect" function call (nothing more than > -1 is returned which doesn't seem to map to any > error messages codes. > > The following code is what I reverted to. It is > basically the example form the Horspool book: > > #include <stdio.h> > #include <sys/types.h> > #include <sys/socket.h> > #include <netinet/in.h> > #include <netdb.h> > > #define HOSTNAME "pandora" > #define PORTNUM 2000 > #define oops(msg) {printf("%s\n",msg);exit(-1);} > > struct sockaddr_in bba; > struct hostent *hp; > FILE *rf; > int s, rfd, ch; > > > int main() > { > int x; > char return_char; > int retval; > > /* build the network address */ > bzero(&bba, sizeof(bba)); > bba.sin_family = AF_INET; > hp=gethostbyname(HOSTNAME); > if(hp==NULL) oops ("no such computer"); > bcopy(hp->h_addr, &bba.sin_addr, hp->h_length); > bba.sin_port = htons(PORTNUM); > > /* make the connection */ > s = socket(AF_INET, SOCK_STREAM, 0); > if(s==-1) oops("socket"); > if((retval=connect(s,&bba, sizeof(bba)))!= 0) cast bba to the generic socket address ie: if((retval=connect(s,(struct sockaddr *)&bba,sizeof(bba))) .. > oops("connect"); the connect system call will return -1 on error and set errno to the interger that maps to the error. If you want to know what happed you can display the error using strerror ie: #include <string.h> extern errno; #define sysoops(msg) {printf("%s: %s\n",msg,strerro(errno));exit(-1);} sysoops("connect error"); > > rf=fdopen(s,"r"); > } > > > The connect call in this snippet always returns > -1 and the program drops out with the "connect" > message. > > Let me say that the socket is open on another > FreeBSD box. A HP-UX workstation and a Windows PC > can both connect to the socket. The HP-UX box is > using this same code as the connection client > (although that really doesn't mean much - other > than I have quite a bit of computer crap in my > basement and extra time on my hands). > > Is there anything that has to be turned on in the > kernal to support sockets? I am grasping at straws > here. > > Any help would be appreciated. > > thanks, > Greg > Hope this helps ... Naief --------------------------------------------------------------------- Naief BinTalal Senior Engineer naief@cyfari.com CyFari Inc The Commerce Engine Network Operations Center. http://www.cyfari.com Daytona Beach Florida. Tel: 904-258-1116 USA. Fax: 904-257-1311 > > > To Unsubscribe: send mail to majordomo@FreeBSD.org > with "unsubscribe freebsd-questions" in the body of the message > To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.05.9904291226190.90679-100000>