*BSD News Article 86116


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.carno.net.au!harbinger.cc.monash.edu.au!munnari.OZ.AU!news.ecn.uoknor.edu!feed1.news.erols.com!howland.erols.net!EU.net!uknet!usenet1.news.uk.psi.net!uknet!dispatch.news.demon.net!demon!microl.demon.co.uk!tarrant.microlise.co.uk!news
From: andrewg@microlise.co.uk (Andrew Gierth)
Newsgroups: comp.unix.programmer,comp.unix.bsd.freebsd.misc
Subject: Re: UDP Sockets sendto() failures
Date: 04 Jan 1997 22:15:26 +0000
Organization: Microlise Engineering Ltd.
Lines: 47
Message-ID: <ufk9ptm6ip.fsf@zen.microlise.co.uk>
References: <E3DxuK.6I8@netplex-tech.com> <E3H7yK.18t@netplex-tech.com>
NNTP-Posting-Host: zen.microlise.co.uk
X-NNTP-Posting-Host: microl.demon.co.uk
In-reply-to: vikas@netplex-tech.Com's message of Sat, 4 Jan 1997 09:06:19 GMT
X-Attribution: AG
X-Mayan-Date: Long count = 12.19.3.14.8; tzolkin = 9 Lamat; haab = 11 Kankin
X-Geek: (V3.1)
 GCS/IT/CC d-(pu) s:+> a- C++++$ UACHV++++$ P--- L E++ W(--) N+++ o? K? w---
 O+++ M- V-- PS+ PE Y+(--) PGP- t+@ 5+++ X R@ tv(-) b++ DI? !D G++ e h* !r y
Cc: Vikas Aggarwal <vikas@netplex-tech.Com>
X-Newsreader: Gnus v5.1
Xref: euryale.cc.adfa.oz.au comp.unix.programmer:48310 comp.unix.bsd.freebsd.misc:33541

>>>>> "Vikas" == Vikas Aggarwal <vikas@netplex-tech.Com> writes:

 Vikas> Since a number of people have suggested that it is more likely that I
 Vikas> have a bug in my code, I am posting my code below.

A bug indeed - byte-order claims another victim.

 Vikas>  * I cannot seem to get sendto() working on FreeBSD 2.1.x (works on
 Vikas>  * SunOS 4.1.x)

This is a BIG, BIG clue that should shout "BYTE ORDER" at you loudly.

Sparcs and 68000's are big-endian, which makes them tolerant of missing
conversions, since host and network byte-order are the same. x86's are
little-endian, so the conversions matter.

 Vikas> /*
 Vikas>  * Send a UDP message on an open file descriptor to the destination specified.
 Vikas>  */
 Vikas> send_udpmesg(buf, bufsize, dgramfd, host, port)
 Vikas>   char *buf;
 Vikas>   char *host;	/* destination IP address */
 Vikas>   int  port, bufsize, dgramfd;

*Always*, *ALWAYS*, document, for port numbers, whether they are in host
or network order.

 Vikas> {
 Vikas>   int nsent;
 Vikas>   static int connected;
 Vikas>   char  *desthost;
 Vikas>   struct sockaddr_in dest_addr_in;

 Vikas>   if (host == NULL)  desthost = "127.0.0.1";		/* localhost */
 Vikas>   else  desthost = host;

 Vikas>   bzero(&dest_addr_in, sizeof(dest_addr_in));
 Vikas>   dest_addr_in.sin_family = AF_INET;
 Vikas>   dest_addr_in.sin_addr.s_addr = inet_addr(desthost);
 Vikas>   dest_addr_in.sin_port = port;

sin_port must always be in network order... you need an htons() call there.

-- 
Andrew Gierth (andrewg@microlise.co.uk)

"Ceterum censeo Microsoftam delendam esse" - Alain Knaff in nanam