From owner-freebsd-net Wed Jul 10 9:23:32 2002 Delivered-To: freebsd-net@freebsd.org Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 3CA8437B400 for ; Wed, 10 Jul 2002 09:23:27 -0700 (PDT) Received: from omegaband.com (faust.seagullsemi.com [206.196.68.34]) by mx1.FreeBSD.org (Postfix) with ESMTP id 0C25D43E09 for ; Wed, 10 Jul 2002 09:23:27 -0700 (PDT) (envelope-from matt@omegaband.com) Received: from orcapc [67.89.124.10] by omegaband.com with ESMTP (SMTPD32-7.04) id AD72D4AB0150; Wed, 10 Jul 2002 11:14:42 -0500 Message-ID: <011001c2282e$1e5fd9c0$ad96a8c0@seagullsemi.com> From: "Matthew Finlay" To: "Chris Given" , References: Subject: Re: Bind to specific address on FreeBSD Date: Wed, 10 Jul 2002 11:23:26 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 Sender: owner-freebsd-net@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org first zero out your sockaddr_in structure before using it. bzero(&dp, sizeof(dp)); and then be sure and set the len field in sockaddr_in struct; dp.sin_len = sizeof(dp); also... get rid of the htonl(bind_to_addr) because inet_addr puts the address in network byte order. i compiled with those changes and the bind succeeded. my guess is the sockaddr_in struct had garbage in the fields that werent initialized... causing an error somewhere along the way. -matt ----- Original Message ----- From: "Chris Given" To: Sent: Tuesday, July 09, 2002 4:50 PM Subject: Bind to specific address on FreeBSD > I can't figure out why this code won't bind to 127.0.0.1 on FreeBSD. I get > an error "Can't assign requested address". > > #include > #include > #include > #include > #include > #include > #include > #include > #include > #include > #include > #include > #include > > int main() { > int sock; > struct sockaddr_in dp; > unsigned long bind_to_addr = inet_addr("127.0.0.1"); > > sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); > if(sock < 0) { > printf("Error socket : %s\n", strerror(errno)); > return -1; > } > > dp.sin_family = AF_INET; > dp.sin_addr.s_addr = htonl(bind_to_addr); > dp.sin_port = htons(1234); > > if(bind(sock, (struct sockaddr*)&dp, sizeof(struct sockaddr_in))!=0) > { > printf("Bind failed : %s\n", strerror(errno)); > } else { > printf("Bind success\n"); > } > } > > To Unsubscribe: send mail to majordomo@FreeBSD.org > with "unsubscribe freebsd-net" in the body of the message > To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-net" in the body of the message