c++ - connect() 函数停止程序

标签 c++ sockets networking

我正在编写两个应该相互连接的程序(客户端和服务器)。我终于解决了所有的个别错误,但是当我开始测试客户端程序时(在启动和服务器之后),连接函数停止了代码的进程。我用一些简单的文本输出对此进行了测试。

这是我的服务器代码(不是很重要)

#include "stdafx.h"

using namespace std;

int main()
{



    WSAData WinSockData;
    WORD DLLVERSION;
    DLLVERSION = MAKEWORD(2, 1);
    int wsa_success = WSAStartup(DLLVERSION, &WinSockData);
    if (wsa_success != 0)
    {   
        printf("Error %d while starting WSA\n", WSAGetLastError());
        pause();
        return 1;
    }



    SOCKET uc_socket;
    uc_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (uc_socket == INVALID_SOCKET)
    {
        printf("Error %d while creating socket\n", WSAGetLastError());
        pause();
        return 1;
    }



    SOCKADDR_IN ADDRESS;
    int AddressSize = sizeof(ADDRESS);



    ADDRESS.sin_addr.s_addr = INADDR_ANY;
    ADDRESS.sin_family = AF_INET;
    ADDRESS.sin_port = htons(444);



    int bind_success = bind(uc_socket, (SOCKADDR *)&ADDRESS, AddressSize);
    if (bind_success != 0)
    {
        printf("Error %d while binding socket\n", WSAGetLastError());
        pause();
        return 1;
    }



    int listen_success = listen(uc_socket, 12);
    if (listen_success != 0)
    {
        printf("Error %d while setting socket to listen\n", WSAGetLastError());
        pause();
        return 1;
    }


    SOCKET c_socket;
    SOCKADDR_IN client_sock;
    int client_sock_size = sizeof(client_sock);
    printf("Socket created; Set to listen for incoming connections\n");
    c_socket = accept(uc_socket, (SOCKADDR *)&client_sock, &client_sock_size);
    if (c_socket == INVALID_SOCKET)
    {
        printf("Error %d while accepting connection request\n", WSAGetLastError());
        pause();
        return 1;
    }
    else
    {
        printf("Connection established!");
    }

    pause();
    return 0;
}

还有我的客户端代码

#include "stdafx.h"

using namespace std;


int main()
{
    int wsasuccessful = -1;
    WSAData WinSockData;
    WORD DLLVERSION;
    DLLVERSION = MAKEWORD(2, 1);
    wsasuccessful = WSAStartup(DLLVERSION, &WinSockData);
    if (wsasuccessful != 0)
    {
        printf("Error %d in client while starting WSA\n", WSAGetLastError());
        pause();
        return 1;
    }



    SOCKET client_socket;
    client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (WSAGetLastError() != 0)
    {
        printf("Error %d in client while instantiating socket\n", WSAGetLastError());
        pause();
        return 1;
    }



    SOCKADDR_IN client_connection;
    client_connection.sin_family = AF_INET;
    client_connection.sin_port = htons(444);
    client_connection.sin_addr.S_un.S_addr = INADDR_LOOPBACK;


    int connection_success = -1;
    printf("before connect()\n");  //for debugging
    connection_success = connect(client_socket, (SOCKADDR *)&client_connection, sizeof(client_connection));
    printf("after conenct()\n");  //for debugging
    if (connection_success != 0)
    {
        printf("Error %d in client while instantiating socket\n", WSAGetLastError());
        pause();
        return 2;
    }
    printf("The client has found a server!");
    printf("The last WSA error was: %d", WSAGetLastError());


    pause();
    return 0;
}

这两个程序都可以正常运行,但它们没有相互连接。我已将我的系统配置为允许监听和连接。

服务器输出是—— 已创建套接字;设置为监听传入连接

客户端输出是 - connect() 之前

我所看到的问题是客户端无法识别端口 444 上的监听套接字,并且会暂停程序直到可以建立连接。

我可以使用非阻塞套接字绕过它,但这并不能解决根本问题。

编辑:忘记包含我的 stdafx.h

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <Inaddr.h>
#include <string.h>
#include <io.h>
#include <Windef.h>
#include <Ws2tcpip.h>
#include <WinNT.h>
#include <Windows.h>
#include <tchar.h>

void pause(void);  //defined in stdafx.cpp

服务端和客户端都一样

编辑 2:在服务器代码的 accept() 函数中将 SOCKADDR 更改为 SOCKADDR_IN 后,最终收到 WSA 错误 10060。

问题仍然存在,但现在至少我知道这是连接失败,而不是代码。

最佳答案

connect 是一个阻塞操作(默认情况下)。它正在尝试连接并且不会返回,直到它工作或失败。听起来它会超时,可能是由于服务器计算机上的 Windows 防火墙阻止了端口。你等了多久?它是否曾经完成(失败)

关于c++ - connect() 函数停止程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35569146/

相关文章:

c++ - QWidget 绘制扭曲角度的 QImage

c++ - Qt中的弹出菜单事件控件

qt - QTcpSocket::write(QByteArray& buf) 的返回值;

linux - 按字节限制的 tc-sfq 替代方案?

Python 套接字和 Opencv - ConnectionResetError : [WinError 10054]

C++11 memory_order_acquire 和 memory_order_release 语义?

c++ - 引用指针后面的值

java - 通过不同计算机上的套接字发送大文件

python - 套接字对和 fork 中的 FD 重用

http - Blackberry JDE HTTPConnection 问题