gpt4 book ai didi

c++ - UDP 客户端到 UDP 服务器的问题,出现 10057 错误

转载 作者:行者123 更新时间:2023-11-27 23:39:32 28 4
gpt4 key购买 nike

我在连接到我的 UDP 服务器时遇到问题。在 VS 上它没有显示任何错误,除非我进行错误检查时它会给我一个错误 10057。

UDP 客户端:

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <Winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#pragma comment(lib, "ws2_32.lib")

#define ZeroMemory

using namespace std;

WSADATA wsadata;
SOCKET Client;
SOCKADDR_IN Server;
unsigned int Port = 5020;
int ret;
char buf[4096];
int len, tolen, fromlen, namelen;


int main(int argc, char * argv[])
{
// Initialize Winsocket
ret = WSAStartup(MAKEWORD(2, 2), &wsadata);
// CHECKS THE SOCKET STATUS
if (ret == SOCKET_ERROR)
{
printf("Client: Winstock Status is: %s ", wsadata.szSystemStatus);
WSACleanup();
cout << endl;
}
else
{
printf("Client: Winstock Status is: %s ", wsadata.szSystemStatus);
cout << endl;
}

// Client Address
Server.sin_family = AF_INET;
Server.sin_port = htons(Port);
inet_pton(AF_INET, "127.0.0.1", &Server.sin_addr);

// Create Socket
ret = Client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

// CHECKS IF SOCKET IS CREATED
if (Client == INVALID_SOCKET)
{
cout << "Client: Can't Create Socket! ERROR: %s " << WSAGetLastError();
WSACleanup();
cout << endl;
}

// Bind Socket
ret = bind(Client, (sockaddr*)& Server,sizeof(Server));

// CHECKS IF SOCKET IS BINDED
if (ret == INVALID_SOCKET)
{
cout << "Client: Failed to bind socket" << WSAGetLastError(); ;
cout << endl;
}

string s(argv[1]);


// SendTo()
ret = sendto
(
Client,
s.c_str(),
s.size() + 1,
0,
(sockaddr*) & Server,
sizeof(Server)
);

if (ret == SOCKET_ERROR);
{
cout << "That did not work! Error Code: " << WSAGetLastError();
cout << endl;
}


// CloseSocket
closesocket(Client);

// CleanUp Winsocket
WSACleanup();

return 0;
}

UDP 服务器:

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <Winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#pragma comment(lib, "ws2_32.lib")

using namespace std;

WSADATA wsadata;
SOCKET ServerSocket;
SOCKADDR_IN ServerAddress, Client;
unsigned int Port = 5020;
char buf[4096];
int ret;
int len, tolen, fromlen, namelen;

int main()
{

// Initializing Socket
ret = WSAStartup(MAKEWORD(2, 2), &wsadata);
if (ret == SOCKET_ERROR)
{
cout << "Server: Failed to initialized socket. Error: " << wsadata.szSystemStatus;
cout << endl;
}
else
{
cout << "Server: Successfully initialized socket." << wsadata.szSystemStatus;
cout << endl;
}

// Sever Address
ServerAddress.sin_family = AF_INET;
ServerAddress.sin_port = htons(Port);
inet_pton(AF_INET, "127.0.0.1", &ServerAddress.sin_addr);

// Create Socket
ret = ServerSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (ret == -1)
{
cout << "Server: Failed to create socket. Error: " << WSAGetLastError();
cout << endl;
}
else
{
cout << "Server: Socket has been created ";
cout << endl;
}

// Bind Socket
ret = bind(ServerSocket, (sockaddr*)& ServerAddress, sizeof(ServerAddress));
if (ret == -1)
{
cout << "Server: Failed to bind socket. Error: " << WSAGetLastError();
cout << endl;
}
else
{
cout << "Server: Socket is binded to address ";
cout << endl;
}

int ClientLength = sizeof(Client);

while(true)
{

// receivefrom
ret = recvfrom
(
ServerSocket,
buf,
len,
0,
(sockaddr*)& Client,
&ClientLength
);

if (ret == SOCKET_ERROR)
{
cout << "Error receiving from client" << WSAGetLastError();
}

// display message
char ClientIP[256];

inet_ntop(AF_INET, &Client.sin_addr, ClientIP, 256);

cout << "message recieve from: " << ClientIP << " : " << buf << endl;
}

// Close Socket
closesocket(ServerSocket);

// Cleanup Winsocket
WSACleanup();

return 0;
}

我先执行了客户端,没问题,执行了服务器,然后才显示了问题。

// SendTo()
ret = sendto
(
Client,
s.c_str(),
s.size() + 1,
0,
(sockaddr*) & Server,
sizeof(Server)
);

if (ret == SOCKET_ERROR);
{
cout << "That did not work! Error Code: " << WSAGetLastError();
cout << endl;
}

最佳答案

两个错误:

  1. ret = Client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    如果您正在编写 UDP 客户端,则需要创建 UDP 套接字,而不是 TCP 套接字:

    ret = Client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  2. ret = bind(Client, (sockaddr*)& Server,sizeof(Server));

    这是在客户端。为什么要尝试 bind() 到服务器端点?您使用 bind() 设置套接字的本地 地址,而不是远程 地址。使用 connect() 作为远程地址(使用 sendto() 时不需要这样做)。

关于c++ - UDP 客户端到 UDP 服务器的问题,出现 10057 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56449761/

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