> -----Original Message----- > From: linux-net-owner@xxxxxxxxxxxxxxx > [mailto:linux-net-owner@xxxxxxxxxxxxxxx]On Behalf Of Martijn van > Oosterhout > Sent: Wednesday, October 05, 2005 1:34 AM > To: yh@xxxxxxxxxxxxxx > Cc: linux-net@xxxxxxxxxxxxxxx > Subject: Re: TCP connection > > > On Wed, Oct 05, 2005 at 01:24:22PM +1000, YH wrote: > > Hi, > > > > Sorry, this is really not a linux question, rather a > network programming > > question if you could still help me out: > > Firstly, walk, nay, run to your nearest bookstore and pick up > a copy of > UNIX Network Programming volume 1 by W.R. Stevens. Heartily agree. > > 1. I have a server / client program of tcp connection on linux. The > > server uses a select function to wait clients packets. It > works fine > > until a client program is terminated. Then the server is > crashed (server > > program can no longer be blocked at select). How can the > server detects > > the client terminate without crashing? > > Do you check the result of read()? It will return an error when the > client closes it's end. If you don't do that you risk a SIGPIPE. I think a read() will return 0 if the peer closes the socket, not an error, at least this should be true on the first read() after the close, assuming the socket RX queue was empty at the time. I think the SIGPIPE will occur on write()s to the socket after its closed by the peer. The send() function can be used instead of write() to avoid the SIGPIPE if you pass it MSG_NOSIGNAL as one of its flags. That way you get EPIPE back in errno and can handle the error at the point it occurs rather than trying to deal with it in a signal handler. > > 2. Is there any system function call or utilities on linux to check > > Ethernet and network status? > > ifconfig, tcpdump, many many others. > > > 3. Why we need to cast struct sockaddr_in serverAddress, > clientAddress > > to struct sockaddr *) in bind(fd, (struct sockaddr > *)&serverAddress, > > sizeof(serverAddress)) and accept(fd, (struct sockaddr > *)&clientAddress, > > &clientLength)? > > Because bind and accept work for other protocols than IP, including > IPX, AX25, etc. So bind takes the generic type and you cast > it when you > use it. > > Hope this helps, > -- > Martijn van Oosterhout <kleptog@xxxxxxxxx> > http://svana.org/kleptog/ > > Patent. n. Genius is 5% inspiration and 95% perspiration. A > patent is a > > tool for doing 5% of the work and then sitting around > waiting for someone > > else to do the other 95% so you can sue them. > - : send the line "unsubscribe linux-net" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html