winsock always returning -1 on socket

时间:2019-01-09 22:17:32

标签: c++ c sockets winsock

I'm trying to connect to my server on port 69 using winsock2.h. Everything appears to be compiling correctly but in a debugger maincommsock is always equal to -1. I can't seem to figure out why it isn't working, and need help. Not very experienced with socket programming, and not sure what to do now!. Any help will be greatly appreciated.

I'm not 100% sure what else to try. This is basically a function that establishes a connection on a socket, and sets connection to true, so that it can loop until it connects.

    int maincommsock;
    bool connection = false;

static void establishconn() {
    maincommsock = socket(AF_INET, SOCK_STREAM, 0);
    if (maincommsock == -1) {
        connection = false;
    }

    //OutputDebugString((LPCSTR)commservers[1]);

    /*
    struct hostent *host;
    if ((host = gethostbyname(SERV_ADDR)) == NULL) {
        connection = FALSE;
    }
    */
    SOCKADDR_IN sockaddr;
    sockaddr.sin_port = 69;
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    //sockaddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
    if (connect(maincommsock, (SOCKADDR *)(&sockaddr), sizeof(sockaddr)) != 1) {
        connection = true;
    }
}

EDIT

I used WSAStartup, and the error I'm currently getting is No connection could be made because the target machine actively refused it."

EDIT 2

used htons on the port. It says the operation completed successfully but no connections show up on the server

EDIT 3

I'm going to test it on linux, to make sure that it is actually a server side error and not a problem with my code. I'll keep you guys updated.

2 个答案:

答案 0 :(得分:1)

您的错误处理有点不足。

首先,当确实发生错误时,请停止正在执行的操作。现在,您并没有停止,只是继续进行下一个API调用,就好像没有发生错误一样。

第二,您对[["Col1","Col2","Col3","Col4"], ["A","B1","C1",""], ["A","B1","C2","D"], ["A","B2","",""]] 的错误处理是错误的。 connect()成功返回0,失败返回-1,但是您要检查connect()的返回值,因此您将以同样的方式对待成功和失败。

尝试以下方法:

!= 1

然后您可以执行以下操作:

static SOCKET establishconn(const char *addr, u_short port)
{
    SOCKET commsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (commsock == INVALID_SOCKET)
        return INVALID_SOCKET;

    //OutputDebugStringA(addr);

    SOCKADDR_IN sockaddr = {};
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(port);
    sockaddr.sin_addr.s_addr = inet_addr(addr);

    if (sockaddr.sin_addr.s_addr == INADDR_NONE)
    {
        struct hostent *host = gethostbyname(addr);
        if (!host)
        {
            closesocket(commsock);
            return INVALID_SOCKET;
        }
        sockaddr.sin_addr.s_addr = *((u_long*) host->h_addr);
    }

    if (connect(commsock, (SOCKADDR &sockaddr, sizeof(sockaddr)) == SOCKET_ERROR)
    {
        closesocket(commsock);
        return INVALID_SOCKET;
    }

    return commsock;
}

答案 1 :(得分:0)

伯克利套接字要求以网络字节顺序显示地址和端口信息。 Winsock遵循以下API:

  

SOCKADDR_IN结构中的所有数据(地址系列除外)必须以网络字节顺序(big-endian)指定。
   Windows Dev Center

使用htons()将端口值从主机转换为网络字节顺序。

sockaddr.sin_port = htons(69);

inet_addr()已经返回了网络字节顺序的值。