Pregunta

Good day!

I'm a somewhat experienced programmer in higher level languages, but this is my first time diving into socket code on lower level languages, so please bear with me.

I seem to be getting an error upon the connect() call. In my main function, WSAGetLastError() prints that this is error number 6 which according to MSDN is WSA_INVALID_HANDLE. This seems strange as there is no detailing of that particular error code on the MSDN page for the connect() function (unless I'm going blind), and my google searches are all inconclusive

I'm using a custom socket_t structure as my code is designed to (eventually) be cross platform. The socket_connect() function is called from the main code page.

socket_t definition:

typedef struct
{
    //windows-specific
    SOCKADDR_IN *addr_in;
    u_long mode;
    SOCKET socket;//acutal SOCKET structure

    // General
    bool listening;//set to true if actively listening
    bool thread_terminate;//when boolean is set to true, listening thread terminates
    void (*error_callback) (int);
    http_response_t * response;
} socket_t;

socket_connect() function:

//see socket_t definition in socket.h
//returns 0 on success, SOCKET_ERROR on WinSock failure, positive error code on ANSI DNS failure
int socket_connect(socket_t * sock, char * addr, int port)
{
    //bear in mind sock is the custom socket_t structure.  sock->socket is the actual SOCKET structure.
    //pardon the nomenclature.  rookie code.

    //DNS lookup structures
    struct addrinfo     * res           = NULL;// Result of the getaddrinfo() call
    struct sockaddr_in  * sockaddr_v4   = NULL;// IPv4 sockaddr structure

    // So-called "hints" structure detailed in the getaddrinfo() MSDN page.
    // I guess it contains information for the DNS lookup.
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family     = AF_UNSPEC;
    hints.ai_socktype   = SOCK_STREAM;
    hints.ai_protocol   = IPPROTO_TCP;

    //Perform DNS lookup
    DWORD getaddrinfo_res = getaddrinfo(addr, "80\0", &hints, &res);//hard-code Port number for now...
    if(getaddrinfo_res != 0) return getaddrinfo_res;//positive DNS error code

            //debug information
    std::cout << "DNS lookup responses:" << std::endl;
    //for each
    int i = 0;//counter
    for(struct addrinfo * ptr = res; ptr != NULL; ptr = ptr->ai_next)
    {
        std::cout << "Response number " << i + 1 << std::endl;
        std::cout << "Flags: " << ptr->ai_flags << std::endl;

        std::cout << "Family: ";
        switch(ptr->ai_family)
        {
        case AF_INET:
            sockaddr_v4 = (struct sockaddr_in *) ptr->ai_addr;//set current address
            sock->addr_in = sockaddr_v4;//set socket address

            std::cout << "AF_INET (IPv4)" << std::endl;
            std::cout << "Addr: " << inet_ntoa(sockaddr_v4->sin_addr) << std::endl;
            break;
        case AF_UNSPEC:
            std::cout << "UNSPECIFIED" << std::endl;
            break;
        default:
            std::cout << "UNKNOWN\t(" << ptr->ai_family << ")" << std::endl;
            break;
        }

        i++;
    }

    //initialize actual SOCKET
    sock->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);// TCP/IP, stream-oriented, and TCP rather than UDP; respectively
    if(sock->socket == INVALID_SOCKET) return SOCKET_ERROR;

    //actual connection
    std::cout << WSAGetLastError() << std::endl;
    int connect_res = connect(sock->socket, (SOCKADDR *) sockaddr_v4, sizeof(sockaddr_v4));///have to convert SOCKADDR_IN to a SOCKADDR pointer here.  Not sure why.
    if(connect_res == SOCKET_ERROR) return SOCKET_ERROR;

    //make nonblocking
    /*
    sock->mode = 1;
    int ioctl = ioctlsocket(sock->socket, FIONBIO, &(sock->mode));//I/O Control Socket.  Not sure what FIONBIO means.  pointer to sock->mode = 1 ensures nonblockingness.
    if(ioctl == SOCKET_ERROR) return SOCKET_ERROR;
    */// (include this?)

    return 0;
}

socket_connect() function call:

int connect_res = conley::socket_connect(sock, "www.google.com", 80);
if(connect_res > 0) std::cout << "DNS ERR " << connect_res << std::endl << std::endl;
if(connect_res < 0) std::cout << "CONNECT ERR " << WSAGetLastError() << std::endl << std::endl;
if(connect_res == 0) std::cout << "Connected" << std::endl << std::endl;

Thank you for your time! - Jake

¿Fue útil?

Solución

You're passing the wrong size for you sockaddr_in when you're calling connect.

Change this:

int connect_res = connect(sock->socket, (SOCKADDR *) sockaddr_v4, sizeof(sockaddr_v4));
//                                                            wrong size ^^^^^^^^^^^

To this:

int connect_res = connect(sock->socket, (SOCKADDR *) sockaddr_v4, sizeof(*sockaddr_v4));
//                                                            right size  ^^^^^^^^^^^

Beyond that, your program has a different bug, namely the persistance of the getaddrinfo() - returned pointer in your sock structure. That should be copied out by value into memory owned by sock. It will work with this sample because it is so isolated, but it should be changed.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top