Retrieving the Socket Type (SO_TYPE) using getsockopt
Some socket options can only be retrieved. The SO_TYPE is one such example. This option allows a subroutine, which is passed a socket (as a file descriptor), to determine what kind of socket it is dealing with.
Below shows an example program that determines the type of the socket s.
Here is the sample Output:
[sgupta@rhel54x64 socket]$ gcc getty.c -o getty
[sgupta@rhel54x64 socket]$ ./getty
Socket s : 3
SO_TYPE : 1
SOCK_STREAM = 1
From this output, you can see that socket number 3 was reported to be of type 1 in the following output line. Note that the C macro SOCK_STREAM is the value of 1, also, confirming that the option value is correct. Just for fun, you might want to modify the program to try the value of SOCK_DGRAM in the socket(2) function call and see whether the reported value changes.
See AlsoSO_PASSCRED & SO_PEERCRED, SO_BROADCAST, SO_KEEPALIVE, SO_LINGER, SO_REUSEADD, SO_OBINLINE
Post a Comment