views:

14

answers:

1

Friends,

I have a non-blocking TCP socket (on AIX). When I attempted connect() I got EINPROGRESS. My question is if I call recv() before connection completion, what would be the (most apprpriate) error code?

I saw that, in case connection fails, and I call recv(), I got ECONNREFUSED; means I got an error corresponding to my earlier connect() attempt. Taking same logic I should get EINPROGRESS for recv(). Am I right in my approach ?

If yes, this raises another question - Why such error codes are not included amongst error codes of recv()?

+1  A: 

I have only seen EAGAIN returned in this case, just as you would see in the case where there's no data to read. For writing to a non-connected socket, you typically get ENOTCONN, though I believe some platforms might give you EAGAIN.

Here's a trivial Python script to demonstrate:

import socket
# Any address that does not succeed or fail right away will do
ADDR = "192.168.100.100"
PORT = 23
s = socket.socket()
s.setblocking(False)
try:
    s.connect((ADDR, PORT))
except socket.error, e:
    print "Connect gave us",e
try:
    s.recv(1)
except socket.error, e:
    print "Read gave us",e
try:
    s.send("x")
except socket.error, e:
    print "Write gave us",e

For me, it gives: Connect gave us (36, 'Operation now in progress') Read gave us (35, 'Resource temporarily unavailable') Write gave us (57, 'Socket is not connected')

These are EINPROGRESS, EAGAIN, and ENOTCONN respectively.

nickm
Thanks, that's sensible. I'll wait for few more replies, before accepting this.
RIPUNJAY TRIPATHI