gpt4 book ai didi

连接不断失败,C 语言中出现错误 10049

转载 作者:行者123 更新时间:2023-11-30 16:19:26 33 4
gpt4 key购买 nike

我是套接字编程新手,这是我的第一个服务器客户端程序。该程序一开始工作得很好,但是当我将代码从 Server.c 和 Client.c 复制到 Visual Studio 中的新项目时,我的客户端现在拒绝连接并抛出错误 10049。我仍然可以连接到我的服务器,如果我从命令提示符使用 telnet。

我很惊讶为什么当我将代码复制到另一个空项目时我的客户会停止工作,其他任何事情都没有改变。

我尝试使用我的公共(public)地址更改 IP 地址、端口号和端口转发,但没有成功。我已经使用 ipconfig 确认了我的计算机的 IP,并且在我的代码中正确输入了它以及端口号。该程序编译良好,没有任何错误。

服务器.c

#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <WS2tcpip.h>


#pragma comment (lib, "Ws2_32.lib")

void start_server(const char* ip_address, int port_number);
int server_sock, client_sock;

void main(int argc, char* argv[])
{
start_server("192.168.0.24", 100);
system("pause");
}

void start_server(const char* ip_address, int port_number)
{
WSADATA wsaData;

int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup() failed with error: %d\n", iResult);
return 1;
}

struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port_number);
InetPton(AF_INET, "", &server_addr.sin_addr.s_addr);

server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (server_sock == INVALID_SOCKET)
{
printf(L"Socket creation failed with error: %ld\n", WSAGetLastError());
}
else
{
printf("Socket created!\n");
}

int bind_status = bind(server_sock, (struct sockaddr*) & server_addr, sizeof(server_addr));
if (bind_status == SOCKET_ERROR) {
wprintf(L"bind failed with error: %ld\n", WSAGetLastError());
}
else
{
printf("Bind was successful!\n");
}

int listen_status = listen(server_sock, 3);
if (listen_status == -1)
{
wprintf(L"listen failed with error: %ld\n", WSAGetLastError());
}
else
{
printf("Listening...\n");
}

client_sock = accept(server_sock, NULL, NULL);
if (client_sock == -1)
{
wprintf(L"client socket failed with error: %ld\n", WSAGetLastError());
}
else
{
printf("Accepted new client!\n");
}
}

客户端.c

#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <WS2tcpip.h>

#pragma comment (lib, "Ws2_32.lib")


void connect_server(const char* ip_address, int port_number);

int sock;

void main()
{
connect_server("192.168.0.24", 100);
send(sock, "test", 5, 0);
system("pause");
}

void connect_server(const char* ip_address, int port_number)
{
WSADATA wsaData;

int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
wprintf("WSAStartup() failed with error: %d\n", iResult);
exit(1);
}

struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port_number);
InetPton(AF_INET, ip_address, &server_addr.sin_addr.s_addr);

sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET)
{
printf("Socket creation failed with error: %d\n", WSAGetLastError());
}
else
{
printf("Socket created!\n");
}

int connection_status = connect(sock, (struct sockaddr *) &server_addr, sizeof(server_addr));
if (connection_status == SOCKET_ERROR)
{
printf("Connection failed with error: %d\n", WSAGetLastError());
}
else
{
printf("Connected.\n");
}
}

最佳答案

尝试使用环回地址 127.0.0.1 的代码。如果它有效,那么问题肯定出在 IP 和端口号的组合上。也许使用 ipconfig/reset 来重置你的 IP 地址。

关于连接不断失败,C 语言中出现错误 10049,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55622094/

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