Blokirajuci read mora da ti vrati barem jedan procitani bajt, osim ako nisi postavio read timeout kada moras malo gimnastike praviti.
Ovo je pseudo-kod koji radi prilicno dobro u obe varijante, sa dosta testiranja tipa vadjenje kablova i sl.
Od tipa exceptiona (tj. socket error code) mozes donekle zakljuciti sta se desilo. Naravno, svaku operaciju (read, poll) treba staviti u try..catch ako zelis "maximalnu zastitu".
Code:
read_operation:
try
{
numread = read()
}
catch (ex)
{
if (ex == timeout)
{
status = poll(0, select_read)
if (status)
disconnected = true;
else
goto read_operation;
}
else
{
disconnected = true;
}
}
Neblokirajuci:
Code:
try
{
status = poll(0, select_read);
if (status)
{
numread = read();
if (numread <= 0)
disconnected = true;
}
}
catch
{
numread = 0;
}