我是一名开始使用 C 学习套接字编程的学生,当我尝试创建客户端-服务器环境时,我在编译时遇到此错误:
if (inet_pton(AF_INET6 , "10.163.3.30", &serv_addr.sin_addr) <= 0) {
^~~~~
warning: implicit declaration of function 'inet_pton' [-Wimplicit-function-declaration]
if (inet_pton(AF_INET6 , "10.163.3.30", &serv_addr.sin_addr) <= 0) {
^~~~~
C:\Users\xtsub\AppData\Local\Temp\ccWTMEPx.o:client.c:(.text+0xe9): undefined reference to `inet_pton'
下面是我的 client.c 的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#define PORT 8080 // Port number to connect to
int main() {
WSADATA wsaData;
SOCKET sock;
struct sockaddr_in serv_addr;
char buffer[1024] = {0};
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed\n");
return 1;
}
// 1. Create socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
printf("Socket creation failed: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// 2. Define server address
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 address from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("Invalid address or address not supported\n");
closesocket(sock);
WSACleanup();
return 1;
}
// 3. Connect to the server
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("Connection to server failed: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
// 4. Send data to the server
char *message = "Hello from client";
send(sock, message, strlen(message), 0);
printf("Sent to server: %s\n", message);
// 5. Receive response from the server
int valread = recv(sock, buffer, sizeof(buffer), 0);
if (valread > 0) {
buffer[valread] = '\0'; // Null-terminate the received string
printf("Received from server: %s\n", buffer);
} else {
printf("No response from server or error occurred\n");
}
// 6. Close the socket
closesocket(sock);
// Cleanup Winsock
WSACleanup();
return 0;
}
我的服务器有另一个代码,server.c 设置,它工作正常,所以我认为这可能不是编译器错误。 (编译成功,当提示 ./server 时,显示消息“服务器正在侦听端口 8080...”)。我搜索了这个错误,似乎该函数无法被我的 MinGw 文件夹中的相应库头识别。我询问了助教,遗憾的是目前还没有解决。如果有人知道解决方案,我将不胜感激。我对此一点也不精通,所以如果有任何错误/信息缺失,请多多包涵。
我尝试过的:
可能是相关信息:
[MS.Learn]:inet_pton 函数 (ws2tcpip.h)(Requirements 部分)指出(强调是我的):
支持的最低客户端:Windows 8.1、Windows Vista [桌面应用程序 | UWP 应用]
[Github]:MinGW 不支持 inet_pton? (@zxgm 的评论)(@JohnBollinger 的评论指出)提到 _WIN32_WINNT 宏(更多详细信息:[MS.Learn]:使用 Windows 头文件),在 _mingw.h 中定义。事实上,该文件将其定义为:
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x502
#endif
对应于 Windows Server 2003,它太旧了。
您所要做的就是添加宏定义before,包括ws2tcpip.h:
#define _WIN32_WINNT _WIN32_WINNT_VISTA
但是,由于 _mingw.h 包含在任何标头中,这将产生(宏重新定义)警告,您可以通过在程序开头(在任何 include 之前)移动宏定义来消除该警告.
输出(使用上述更改构建源文件):
[cfati@CFATI-5510-0:e:\Work\Dev\StackExchange\StackOverflow\q079212570]> sopr.bat ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ### [prompt]> dir /b main00.c [prompt]> gcc --version | findstr gcc 'gcc' is not recognized as an internal or external command, operable program or batch file. [prompt]> set PATH=%PATH%;f:\Install\pc064\MinGW\MinGW-W64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin [prompt]> gcc --version | findstr gcc gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0 [prompt]> gcc -fPIC -o main.exe main00.c -lws2_32 [prompt]> dir /b main.exe main00.c [prompt]>