SocketStream exceptions and disconnect handling

Regan Heath regan at netmail.co.nz
Wed Mar 7 10:17:08 PST 2012


On Wed, 07 Mar 2012 17:29:25 -0000, Pedro Lacerda <pslacerda at gmail.com>  
wrote:
> 2012/3/7 Pedro Lacerda <pslacerda at gmail.com>
>
>> Hi all,
>>
>> I'm trying to handle disconnections transparently on SocketStream. I
>> thought something like this:
>>
>> void send(ubyte[] buffer) in { assert(buffer.length > 0); }
>> body {
>>     if (!stream.isAlive) connect();
>>     auto sent = stream.write(buffer);
>>     if (sent == 0)
>>         throw new Exception("Error while writing");
>> }
>>
>> But that's impossible because isAlive doesn't exists for SocketStream  
>> and
>> I can't get the underlying socket object. Is that something missing or
>> should I handle it differently?

The underlying problem is that you cannot detect if a socket is still  
connected without trying to read (recv) or write (send) to it, it is only  
once you attempt an operation that it will report that it has been  
closed.  This is why isAlive wont/can't exist in any really useful way.   
The underlying socket routines indicate closure by returning 0 bytes  
read/written.  So, the real Q here is how does socketstream (is it built  
on std.socket?) reflect this information to you?

Regan

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list