Line | |
---|
1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <unistd.h> |
---|
4 | #include <sys/types.h> |
---|
5 | #include <sys/socket.h> |
---|
6 | #include <netinet/in.h> |
---|
7 | |
---|
8 | int main(void); |
---|
9 | |
---|
10 | int main() |
---|
11 | { |
---|
12 | int s; |
---|
13 | int optval; |
---|
14 | socklen_t optlen = sizeof(optval); |
---|
15 | |
---|
16 | /* Create the socket */ |
---|
17 | if((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { |
---|
18 | perror("socket()"); |
---|
19 | exit(EXIT_FAILURE); |
---|
20 | } |
---|
21 | |
---|
22 | /* Check the status for the keepalive option */ |
---|
23 | if(getsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, &optlen) < 0) { |
---|
24 | perror("getsockopt()"); |
---|
25 | close(s); |
---|
26 | exit(EXIT_FAILURE); |
---|
27 | } |
---|
28 | printf("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF")); |
---|
29 | |
---|
30 | /* Set the option active */ |
---|
31 | optval = 1; |
---|
32 | optlen = sizeof(optval); |
---|
33 | if(setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) { |
---|
34 | perror("setsockopt()"); |
---|
35 | close(s); |
---|
36 | exit(EXIT_FAILURE); |
---|
37 | } |
---|
38 | printf("SO_KEEPALIVE set on socket\n"); |
---|
39 | |
---|
40 | /* Check the status again */ |
---|
41 | if(getsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, &optlen) < 0) { |
---|
42 | perror("getsockopt()"); |
---|
43 | close(s); |
---|
44 | exit(EXIT_FAILURE); |
---|
45 | } |
---|
46 | printf("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF")); |
---|
47 | |
---|
48 | close(s); |
---|
49 | |
---|
50 | exit(EXIT_SUCCESS); |
---|
51 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.