|
3. Writing Client Applications (TCP/SOCK_STREAM)3.1 How do I convert a string into an internet address?If you are reading a host's address from the command line, you may not know if you have an aaa.bbb.ccc.ddd style address, or a host.domain.com style address. What I do with these, is first try to use it as a aaa.bbb.ccc.ddd type address, and if that fails, then do a name lookup on it. Here is an example:
3.2 How can my client work through a firewall/proxy server?If you are running through separate proxies for each service, you shouldn't need to do anything. If you are working through sockd, you will need to "socksify" your application. Details for doing this can be found in the package itself, which is available at:
ftp://ftp.net.com/socks.cstc/socks.cstc.4.2.tar.gz you can get the socks faq at:
ftp://coast.cs.purdue.edu/pub/tools/unix/socks/FAQ 3.3 Why does connect() succeed even before my server did an accept()?From Andrew Gierth ( andrew@erlenstar.demon.co.uk): Once you have done a The other factor in this is the 'backlog' parameter for 3.4 Why do I sometimes loose a server's address when using more than one server?From Andrew Gierth ( andrew@erlenstar.demon.co.uk): Take a careful look at struct hostent. Notice that almost everything in it is a pointer? All these pointers will refer to statically allocated data. For example, if you do:
then (as you should know) a subsequent call to But if you do:
to make a copy of the You can get round this by doing a proper 'deep copy' of the 3.5 How can I set the timeout for the connect() system call?From Richard Stevens ( rstevens@noao.edu): Normally you cannot change this. Solaris does let you do this, on a
per-kernel basis with the ndd The easiest way to shorten the connect time is with an From Andrew Gierth ( andrew@erlenstar.demon.co.uk): First, create the socket and put it into non-blocking mode, then call connect(). There are three possibilities:
If the connection succeeds:
If the connection fails:
3.6 Should I bind() a port number in my client program, or let thesystem choose one for me on the connect() call?From Andrew Gierth ( andrew@erlenstar.demon.co.uk): ** Let the system choose your client's port number ** The exception to this, is if the server has been written to be picky about what client ports it will allow connections from. Rlogind and rshd are the classic examples. This is usually part of a Unix-specific (and rather weak) authentication scheme; the intent is that the server allows connections only from processes with root privilege. (The weakness in the scheme is that many O/Ss (e.g. MS-DOS) allow anyone to bind any port.) The If the server is not fussy about the client's port number, then
don't try
and assign it yourself in the client, just let If, in a client, you use the naive scheme of starting at a fixed port number
and calling The problem is if the server end of your connection does an active close. (E.G. client sends 'QUIT' command to server, server responds by closing the connection). That leaves the client end of the connection in CLOSED state, and the server end in TIME_WAIT state. So after the client exits, there is no trace of the connection on the client end. Now run the client again. It will pick the same port number, since as far as
it can see, it's free. But as soon as it calls This problem is especially dangerous because it doesn't show up unless the client and server are on different machines. (If they are the same machine, then the client won't pick the same port number as before). So you can get bitten well into the development cycle (if you do what I suspect most people do, and test client & server on the same box initially). Even if your protocol has the client closing first, there are still ways to produce this problem (e.g. kill the server). 3.7 Why do I get "connection refused" when the server isn't running?The 3.8 What does one do when one does not know how much information is commingover the socket ? Is there a way to have a dynamic buffer ?This question asked by Niranjan Perera ( perera@mindspring.com). When the size of the incoming data is unknown, you can either make the
size of the buffer as big as the largest possible (or likely) buffer, or
you can re-size the buffer on the fly during your read. When you
On the other hand, a more elegant solution that does not depend on the inner workings of the kernel is to use realloc() to expand the buffer as required in say 4K chunks (since 4K is the size of a page of ram on most systems). I may add something like this to sockhelp.c in the example code one day. Previous Next Table of Contents |
|||||||||||||||||
With any suggestions or questions please feel free to contact us |