Check state of socket

Does anybody know the NETOS function to check the state of a socket? i.e. Connected / Disconnected / Receiving / Transmitting

The closest thing I have found is getsockopt() but I cant get it to work.

Any ideas?

Thanks

Take a look at:

extern “C” int customizeMiiCheckLink(void);

I use that to detect the connection on startup. I also run a thread that checks it every 2 seconds to determine if anyone plugged a cable in or out. I reboot if that happens.

Does the original poster want socket or link status? customizeMiiCheckLink will give you link status (ISO layers 1 & 2). socket status is at level 4.

To the original poster: When you say you can’t get it (getsockopt)to work, that is not a lot of information to go on. Could you give us more detail on what is not working?

With getsockopt, I can’t find the right arguments to determine the connection state of the socket. The function asks for arguments: level and optname but I do not know what these should be set to.

Can’t you use standard socket commands? To see if a socket is still open, use select() or recv() and see if they return a -1.

-Erik

The following URL seems to contain all the info you’d need for calling getsockopt:

http://publib.boulder.ibm.com/iseries/v5r1/ic2924/index.htm?info/apis/gsocko.htm

So far I have this:

		retval = getsockopt(sockFD, SOL_SOCKET, SO_ERROR, (char*)&sock_error, &sock_error_size);
		if(retval < 0)
		{
			printf("Getsockopt error

");
printf("Error #%d
",getErrno());
tx_thread_sleep(200);
}
else
{
printf("Socket error number: %c
",sock_error);
}

But retval returns with < 0 meaning that getsockopt has failed. Also, if getErrno() is run just after the getsockopt(), it returns with 0, an error number that is not defined in errno.h

Anyone have any ideas why getsockopt() is failing?

Unfortunately you do not supply enough of your code to render an opinion. So I’ll guess, based on past history.

  1. Are you setting sock_error_size to 4 (sizeof(int)) before calling getsockopt? You need to set sock_error_size so that getsockopt knows how much space it has to work with. If you do not set it then it may have been set to 0 (zero) (clearly an error) or it could have been set to whatever was in memory (also bad).

  2. sock_error should declared as an int. It should (as you did) have a & (pointer to) and (as you did) it should be typecast as (char *).

It’s been quite awhile since the original posting and follow-up comments, but I think the question is still valid. Is there a way to determine if the other end has abruptly dropped a socket connection?

Using select() does not work - at least if called with a timeout - as it returns 0, meaning the timeout occurred. Other common suggestions are to use getpeername(), do a send() with 0 (or a small number of) bytes.

Although getpeername() appears to be present (Net+OS 7.5), it always returns an error whether the connection is good or not. Does this work only with UDP sockets (our application uses a TCP connection)? I ask because in the Notes section of the documentation for getpeername() is says “ENOTCON - Socket is not connected for a UDP packet.”

A send() with zero bytes returns an invalid argument error and a send() with one byte always succeeds whether the connection is still present or not (probably because it is just being buffered internally). I suspect using recv() in non-blocking mode would behave similarly - i.e., return an error with errno set to EWOULDBLOCK, whether the connection was good or not.

Using select() does not work - at least if called with a timeout - as it returns 0, meaning the timeout occurred. Other common suggestions are to use getpeername(), do a send() with 0 (or a small number of) bytes.

I use select() all the time. Since it was fixed to be thread safe in 7.x that is. If it returns -1, then there is a socket error and you should do a closesocket().

Also, by definition, recv() returns a <= 0 if the socket has died in blocking mode.

-Erik