-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_type.c
34 lines (29 loc) · 959 Bytes
/
socket_type.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 获取 socket 的类型
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include "error_handling.h"
int main(int argc, char *argv[])
{
int tcp_socket, udp_socket, socket_type;
socklen_t optlen;
int state;
optlen = sizeof(socket_type);
tcp_socket = socket(PF_INET, SOCK_STREAM, 0);
udp_socket = socket(PF_INET, SOCK_DGRAM, 0);
printf("SOCK_STREAM: %d, SOCK_DGRAM: %d\n", SOCK_STREAM, SOCK_DGRAM);
if (getsockopt(tcp_socket, SOL_SOCKET, SO_TYPE, (void *)&socket_type, &optlen) == -1)
{
error_handling("getsockopt() error: %s", strerror(errno));
}
printf("tcp socket type: %d\n", socket_type);
if (getsockopt(udp_socket, SOL_SOCKET, SO_TYPE, (void *)&socket_type, &optlen) == -1)
{
error_handling("getsockopt() error: %s", strerror(errno));
}
printf("udp socket type: %d\n", socket_type);
return 0;
}