Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!ccut!wnoc-tyo-news!astemgw!icspub!creamy!oucom2!digigw!gday From: gday@digigw.digital.co.jp (Gordon Day) Newsgroups: comp.protocols.nfs Subject: Re: Problem with PC-NFS programmers Toolkit Summary: try htons() Message-ID: <686@digigw.digital.co.jp> Date: 30 Nov 90 05:27:51 GMT References: <901019.190237.kjetilo@otter> Organization: Digital Electronics Corp., Osaka, Japan Lines: 58 [ problem with port numbers when using PC-NFS Toolkit deleted ] 1. RTFM 2. This is a socket problem, and shouldn't be posted to comp.protocols.nfs 3. Here's what's wrong anyway (see below) Try using ntohs(). The port number you are getting back from getservbyname() is in the _correct_ (i.e. network) byte order already. You don't have to do anything to it when assigning the port number to the server "name" before your connect() or sendto() call. You may have been programming before on a system where the host byte order was the same as the network byte order, so the port number was "readable". This is not generally the case, and you should be converting to/from network byte order when appropriate (e.g. printing it out :-). Example (not tested, of course :-)) struct hostent *hp; struct servent *sp; struct sockaddr_in server_addr; unsigned short port; . . . if ((hp = gethostbyname("myserver")) == NULL){ printf("``myserver'' is an unkown host\n"); exit(1); } memcpy((caddr_t)&server_addr.sin_addr, hp->h_addr, hp->h_length); server_addr.sin_family = AF_INET; #ifdef GUESSING_A_PORT_NUMBER port = htons((unsigned short)23); /* notice the htons() */ #else setservent(1); if ((sp = getservbyname("telnet","tcp")) == NULL){ printf("unable to get telent tcp service port\n"); exit(1); } port = sp->s_port; /* no htons() needed */ #endif server_addr.sin_port = sp->s_port; printf("telnet port (network byte order) = %u", (unsigned int)port); printf("telnet port (host byte order) = %u", (unsigned int)ntohs(port)); . . . Hope this helps, Gordon W. Day