连接不断失败,并在C中出现错误10049

时间:2019-04-10 22:19:45

标签: c windows visual-studio sockets winsock

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

当我将代码复制到另一个空项目时,为什么我的客户会停止工作,我感到很惊讶。

我尝试使用公共地址更改IP地址,端口号和端口转发无济于事。我已经使用ipconfig确认了我计算机的IP,并且已经在我的代码中正确输入了IP,以及端口号。该程序可以正常编译,没有任何错误。

Server.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");
    }
}

Client.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");
    }
}

1 个答案:

答案 0 :(得分:0)

非常快速/简单地搜索WSA error 10049会导致:

Winsock错误10049通常在您尝试创建一个地址不在此计算机上的套接字时发生。例如,如果您的MDaemon在IP地址为192.168.0.1的计算机上运行,​​并且尝试将MDaemon绑定到192.168.0.100,则会收到此错误消息。

因此,您需要更正新计算机的IP地址。