我是使用 winsock2 的新手,并且已经为我试图用来向我在同一台计算机上运行的客户端发送字符串的服务器汇总了以下代码(使用相同的端口连接到 127.0.0.1因为服务器设置为监听)。
如果这很重要,我正在使用 MinGW。
我遇到的问题是 listen() 似乎提前完成但返回成功代码。这是一个问题,因为当调用 accept() 时,它似乎永远阻塞。无论我是否正在运行客户端程序,都会发生此事件,并且我曾尝试在之前和之后运行客户端程序,但这似乎并不影响它。
// -1: "Could not initialize WSA."
// -2: "Could not create listener socket."
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <cstdio>
#define port 0x0ABC
UINT64 trStrLen (char* str)
{
if (str == NULL) return 0;
UINT64 pos = 0;
while (*(str + pos) != '\0') pos++;
return pos;
};
#include <cstdio>
int main ()
{
WSADATA wsadata;
if (WSAStartup(MAKEWORD(2,0),&wsadata)) return -1;
SOCKET server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
SOCKADDR_IN sin;
memset(&sin,0,sizeof(SOCKADDR_IN));
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = INADDR_ANY;
int socksize = sizeof(SOCKADDR);
while (bind(server,(SOCKADDR*)(&sin),socksize) == SOCKET_ERROR) return -2;
char* TEMP_TO_SEND = "Billy Mays does not approve.";
UINT64 TEMP_SEND_LEN = trStrLen(TEMP_TO_SEND);
printf("Server online.\n");
while (true)
{
printf("Waiting for connections.\n");
while (listen(server,SOMAXCONN) == SOCKET_ERROR);
printf("Client requesting connection.\n");
SOCKET client = accept(server,NULL,NULL);
printf("Accept is no longer blocking.\n");
if (client != INVALID_SOCKET)
{
printf("Attempting to send information to the client...\n");
if (send(client,TEMP_TO_SEND,TEMP_SEND_LEN,0) == SOCKET_ERROR) printf("The information wasn't sent properly.\n");
else printf("The client received the information.\n");
}
else printf("Couldn't establish a connection to the client.\n");
};
};
这可能是显而易见的,但我没有看到它,所以任何提示都会有所帮助。