getsockopt在windows和linux下TCP_INFO的用法
时间: 2024-03-13 16:44:48 浏览: 59
在Windows系统下,TCP_INFO选项可以通过getsockopt函数来获取TCP socket的相关信息,如下:
```c
#include <winsock2.h>
#include <mswsock.h>
#include <ws2tcpip.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0) {
printf("WSAStartup failed: %d\n", result);
return 1;
}
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
printf("Socket creation failed: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
/* Set up the sockaddr_in structure */
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(8080);
/* Connect to the server */
result = connect(sock, (struct sockaddr *)&server, sizeof(server));
if (result == SOCKET_ERROR) {
printf("Connect failed: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
/* Get the TCP socket info */
struct tcp_info info;
int infoLength = sizeof(info);
result = getsockopt(sock, IPPROTO_TCP, TCP_INFO, (char *)&info, &infoLength);
if (result != 0) {
printf("Getsockopt failed: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
printf("TCP_INFO state: %u\n", info.tcpi_state);
closesocket(sock);
WSACleanup();
return 0;
}
```
在Linux系统下,TCP_INFO选项可以通过getsockopt函数来获取TCP socket的相关信息,如下:
```c
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1) {
perror("Socket creation failed");
return 1;
}
/* Set up the sockaddr_in structure */
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(8080);
/* Connect to the server */
int result = connect(sock, (struct sockaddr *)&server, sizeof(server));
if (result == -1) {
perror("Connect failed");
close(sock);
return 1;
}
/* Get the TCP socket info */
struct tcp_info info;
socklen_t infoLength = sizeof(info);
result = getsockopt(sock, IPPROTO_TCP, TCP_INFO, (char *)&info, &infoLength);
if (result != 0) {
perror("Getsockopt failed");
close(sock);
return 1;
}
printf("TCP_INFO state: %u\n", info.tcpi_state);
close(sock);
return 0;
}
```
这两段代码都是获取TCP socket的状态信息,打印出状态码tcpi_state。这个状态码可以表示TCP连接的状态,如TCP_SYN_SENT、TCP_ESTABLISHED、TCP_FIN_WAIT1等。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)