既存の/開いているソケット ハンドルのみが与えられた場合、ソケットが Linux で接続指向のソケットであるかどうかを判断するにはどうすればよいですか? WSAPROTOCOL_INFO
を使用して取得できる Windowsのようなものを探していますgetsockopt
。
前もって感謝します、クリストフ
Taken from here:
Socket options
These socket options can be set by using setsockopt(2) and read with getsockopt(2) with the socket level set to SOL_SOCKET for all sockets:
...
SO_PROTOCOL (since Linux 2.6.32) Retrieves the socket protocol as an integer, returning a value such as IPPROTO_SCTP. See socket(2) for details. This socket option is read-only.
SO_TYPE Gets the socket type as an integer (e.g., SOCK_STREAM). This socket option is read-only.
To correct the solution provided by xaxxon, the code has to be:
int sock_type;
socklen_t sock_type_length = sizeof(sock_type);
getsockopt(socket, SOL_SOCKET, SO_TYPE, &sock_type, &sock_type_length);
if (sock_type == SOCK_STREAM) {
getsockopt(socket, SOL_SOCKET, SO_PROTOCOL, &sock_type, &sock_type_length);
if (sock_type == IPPROTO_TCP) {
// it's TCP
} else {
// it's SCTP
}
} else if (sock_type == SOCK_DGRAM) {
// it's UDP
}