gpt4 book ai didi

c++ - WinSock accept() 返回 204.204.204.204

转载 作者:行者123 更新时间:2023-11-30 04:10:37 25 4
gpt4 key购买 nike

尝试创建一个简单的 WinSock 服务器 - accept() 只是不断返回 204.204.204.204 的 IP。我的在线搜索结果显示很多人因未提供 sockaddr 长度(第 3 个 accept() 参数)而收到此错误,但我已经这样做了。

奋斗了很久;我毫不怀疑我犯了一个简单的错误,但我就是无法发现它!

附言。我知道我有一些未使用的变量正在传递和东西 - 正在进行中:)

网络.h:::

#pragma once
#include <afxwin.h>
#include <string>
#include <Windows.h>
#include <functional>
#include <WinSock2.h>
#include <iostream>
#include "Connection.h"
using namespace std;

#define LISTEN_PORT 20248
#define MAX_CLIENTS 10

typedef struct NETWORK_T
{
SOCKET c_socket; // Client Socket
sockaddr_in addr_in; // Addr_in
fd_set set; // Used to check if data in socket
int i; // Additional info
} CLIENT, SERVER;

class Network
{
public:
Network(void);
~Network(void);
void StartListenServer(string port, function<void(Connection)> callback);
void StopListenServer(void);
void Connect(string ip, string port);

private:
bool mServerRunning;
WSADATA mData;
SERVER* mServer;

static UINT ServerThread(LPVOID pParam);

bool Setup(SERVER* server, WSADATA& data);
void ServerCloseSocket(NETWORK_T* socket);
void ServerCloseSocket(SOCKET& socket);
};

网络.CPP:::

#include "Network.h"


Network::Network(void)
{
mServerRunning = false;
}


Network::~Network(void)
{

}

void Network::StartListenServer(string port, function<void(Connection)> callback)
{
if(mServerRunning)
return; // If server is running we dont want a second

mServerRunning = true;
AfxBeginThread(ServerThread, this);
}

void Network::StopListenServer(void)
{
mServerRunning = false;
}

void Network::Connect(string ip, string port)
{

}

void Network::ServerCloseSocket(NETWORK_T* socket)
{
shutdown(socket->c_socket, 2);
closesocket(socket->c_socket);
}

void Network::ServerCloseSocket(SOCKET& socket)
{
shutdown(socket, 2);
closesocket(socket);
}

bool Network::Setup(SERVER* server, WSADATA& data)
{
int res;

server->addr_in.sin_family = AF_INET;
server->addr_in.sin_port = htons(LISTEN_PORT);
server->addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
res = WSAStartup(0x101, &data);
if (res != 0)
{
cout << "WSAStartup failed.\r\n";
return false;
}
else
{
cout << "WSAStartup Version: " << data.wVersion << "\nWSADescription: " << data.szDescription << "\nWSAStatus: " << data.szSystemStatus << "\r\n";
}
server->c_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (server->c_socket == INVALID_SOCKET)
{
cout << "Invalid socket.\r\n";
return false;
}
else if (server->c_socket == SOCKET_ERROR)
{
cout << "Socket error.\r\n";
return false;
}
else
cout << "Server socket established.\r\n";
res = 1;
setsockopt(server->c_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&res, sizeof(res)); // Allows the socket to be bound to an address that is already in use
res = ::bind(server->c_socket, (struct sockaddr*)&server->addr_in, sizeof(struct sockaddr));
if (res != 0)
{
cout << "Socket BIND failed.\r\n";
return false;
}
else
cout << "Socket bound to port " << LISTEN_PORT << "\r\n";
res = listen(server->c_socket, MAX_CLIENTS);
cout << "Server listen mode: " << res << "\nMax clients: " << MAX_CLIENTS << "\r\n";
res = 1;
ioctlsocket(server->c_socket, FIONBIO, (u_long*)&res);

return true;
}

UINT Network::ServerThread(LPVOID pParam)
{
Network* ntwkPtr = (Network*)pParam;

ntwkPtr->mServer = new SERVER();

ntwkPtr->Setup(ntwkPtr->mServer, ntwkPtr->mData);

SOCKET client;
SOCKADDR_IN from;
int fromlen = sizeof(from);

while(ntwkPtr->mServerRunning)
{
char temp[512];
client = accept(ntwkPtr->mServer->c_socket, (struct sockaddr*)&from, &fromlen);
sprintf(temp,"Your IP is %s\r\n",inet_ntoa(from.sin_addr));
send(client,temp,strlen(temp),0);
cout << "Connection from " << inet_ntoa(from.sin_addr) <<"\r\n";
//ntwkPtr->ServerCloseSocket(client);
}

ntwkPtr->ServerCloseSocket(ntwkPtr->mServer);
WSACleanup();

return 0;
}

最佳答案

正如您在评论中确认的那样:accept 失败,因此没有要报告的地址。

关于c++ - WinSock accept() 返回 204.204.204.204,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20578781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com