[OT] BSD Socket question...

D

Daniel T.

In this code:

unsigned char getByte( sd )
{
unsigned char result = 0;
int bytes = recv( sd, &result, 1, 0 );
if ( bytes < 0 )
throw runtime_error( "recv error" );
if ( bytes == 1 )
return result;
if ( bytes == 0 )
{
// what here?
}
}

(Assuming a blocking socket)

What does it mean when recv returns 0? Does it mean that the socket has
disconnected?
 
K

Kristofer Pettijohn

Daniel T. said:
In this code:

unsigned char getByte( sd )
{
unsigned char result = 0;
int bytes = recv( sd, &result, 1, 0 );
if ( bytes < 0 )
throw runtime_error( "recv error" );
if ( bytes == 1 )
return result;
if ( bytes == 0 )
{
// what here?
}
}

(Assuming a blocking socket)

What does it mean when recv returns 0? Does it mean that the socket has
disconnected?

This group is for the C++ language and its standards only; not OS specific
questions... You may wish to ask in a BSD group for an answer.

Aside, you may wish to check the man page for the recv() function:

RETURN VALUES
These calls return the number of bytes received, or -1 if an error
occurred.

Kristofer
 
L

llewelly

[cc'd to comp.unix.programmer, and follow-ups set]

I think this is more on topic in comp.unix.programmer, so I'm sending
it there.

Daniel T. said:
In this code:

unsigned char getByte( sd )
{
unsigned char result = 0;
int bytes = recv( sd, &result, 1, 0 );
if ( bytes < 0 )
throw runtime_error( "recv error" );
if ( bytes == 1 )
return result;
if ( bytes == 0 )
{
// what here?
}
}

(Assuming a blocking socket)

What does it mean when recv returns 0? Does it mean that the socket has
disconnected?

I think it means the message was very short. Empty packet? 'man recv'
says this:

# RETURN VALUES
# These calls return the number of bytes received, or -1 if an error
# occurred.

on freebsd-5 .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top