connect()函数停止程序

时间:2016-02-23 04:58:19

标签: c++ sockets networking

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

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

#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;
}

两个程序都运行没有问题,但它们没有相互连接。我已将系统配置为允许收听和连接。

服务器输出是 - 创建套接字;设置为侦听传入连接

客户端输出是 - 之前的连接()

我看到的问题是客户端没有识别端口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.

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

1 个答案:

答案 0 :(得分:1)

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