*BSD News Article 90511


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!solace!not-for-mail
From: Johan Lithander <johan@teligent.se>
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Re: Checking Socket validity under BSD
Date: Fri, 07 Mar 1997 10:49:26 +0100
Organization: Teligent AB
Lines: 100
Message-ID: <331FE4A6.2781E494@teligent.se>
References: <331EEB2D.61B8@skynet.co.uk>
NNTP-Posting-Host: gollum.teligent.se
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: Mozilla 3.01 (X11; I; FreeBSD 2.1.0-RELEASE i386)
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.freebsd.misc:36677

Use select (messy ?????). select will not solve Your problem with
detecting that the socket has been closed on the other side, You have
to check the return value from read().If Your app is the client side of
the socket, select releases if the socket server terminates. This isn't
the case if Your app is the socket server !!(Is there a workaround for
this ???). Regarding SIGPIPE,it's only "generated" when write to a
closed socket, this isn;t the case for read, check Your man pages.
ex:
	fd_set fdset;
	int rc;
	char buffer[1024];
	.
	.

	while(1)
	{
	  FD_ZERO(&fdset);
	  FD_SET(socket_descriptor, &fdset);
	  FD_SET(another_descriptor, &fdset);
	  rc = select(FD_SETSIZE, &fdset, NULL, NULL, NULL);
	  if(rc < 0)
	   {
	     /* Check errno*/
	   }
	  if(FD_ISSET(socket_descriptor, &fdset))
	   { 
	     /* You have data to receive, or end of file */
	     if((rc = read(socket_descriptor, buffer, sizeof(buffer))) <= 0)
		{
		  /* End of file (socket disconnected) if 0
		  if < 0, check errno */
		}
		write(dest, buffer, rc);
		........
	    }
	  if(FD)ISSET(another_descriptor, &fdset))
	    {
		/* You have data, or end of file on another_desc...*/
		.
		.
	    }
	  .
	  .
	 }

Karl Pielorz wrote:
> 
> Hi,
> 
> I've written a program that creates & connects a socket to a remote
> machine, and quite happily reads & writes data to it, mostly in a
> do{}while loop.
> 
> The only problem I've got is trying to check to see if the remote
> machine has closed it's socket... According to the man pages a SIGPIPE
> would be generated if the socket gets closed / becomes invalid, which
> will cause most normal threads (that aren't looking for signals like
> SIGPIPE) to exit, this does happen - but only after you try to write()
> some data to the socket that's closed...
> 
> The program uses non-blocking IO and spends most of it's time in a loop
> like:
> 
> transferred = read( mysocket, buffer, sizeof( buffer ));
> if( transferred >0 ){
>         write( mydestination, buffer, transferred );
> }
> transferred = read( mydestination, buffer, sizeof( buffer ));
> if( transferred >0 ){
>         write( mysocket, buffer, transferred );
> }
> 
> (the above is sort of in pseudo code :(
> 
> What I'd really like to do is have something like:
> 
> if( is_still_valid( mysocket ){
>         read
>         ...
> }
> 
> The closest I can get is using select() - but it's a bit messy to say
> the least...
> 
> If anyone can suggest a better method, any help would be gratefully
> received...
> 
> Regards,
> 
> Karl Pielorz

-- 
----------------------------------------------------------------
Johan Lithander			Teligent AB
Phone:	+46 8 4480626		P.O. Box 10077
Mobile:	+46 70 5573624		S-121 27 Stockholm-Globen
Fax:	+46 8 6496321		Sweden
e-mail:	johan@teligent.se	Visiting address: Palmfeltsv. 5B
www:	http://www.teligent.se
----------------------------------------------------------------