From owner-freebsd-questions Fri Apr 30 9:42:34 1999 Delivered-To: freebsd-questions@freebsd.org Received: from namep.cyfari.com (namep.cyfari.com [208.193.65.10]) by hub.freebsd.org (Postfix) with SMTP id F29DA14F55 for ; Fri, 30 Apr 1999 09:42:30 -0700 (PDT) (envelope-from naief@cyfari.com) Received: (qmail 7067 invoked by alias); 30 Apr 1999 17:47:17 -0000 Received: from destroyer.cyfari.com (208.193.65.11) by namep.cyfari.com with SMTP; 30 Apr 1999 17:47:17 -0000 Date: Thu, 29 Apr 1999 12:38:16 -0400 (EDT) From: Naief BinTalal To: Greg Campbell Cc: freebsd-questions@FreeBSD.ORG Subject: Re: socket communication In-Reply-To: <000f01be9323$ce6e5bf0$9308a8c0@matrikon.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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 > #include > #include > #include > #include > > #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 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