Hi all:
I wrote a simple program to open a socket to localhost port 8201.

Codes:
......

if (WSAStartup(VERSION, &wsadata) != NO_ERROR)
    {
        WSACleanup();
        return EXIT_FAILURE;
    }

open_socket(serverName, port);

SOCKET open_socket (const char *serverName, const int port)
{
    struct hostent *server = NULL;
    SOCKADDR_IN serverAddress;

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock!= INVALID_SOCKET)
    {
        server = gethostbyname(serverName);
        if (server == NULL)
        {
            sock = INVALID_SOCKET;
        }
        else 
        {
            char *ip = server->h_addr;
            SOCKADDR_IN serverAddress = 
            {
                AF_INET,
                htons(port),
                {ip[0], ip[1], ip[2], ip[3]}
            };
        }
        if (connect(sock, (SOCKADDR*) &serverAddress, sizeof(serverAddress))<0)
        {
            sock = INVALID_SOCKET;
            printf("Connection failed.");
        }
        printf("Connection successed.");
    }
    return sock;
}

It always results in "INVALID_SOCKET". I am very confused. The header file I used is #include <winsock.h>.

Thanks

Now there are two or three places in your code where the socket may return INVALID_SOCKET, e.g the call of socket, and where the Winsock functions may fail, e,g gethostname(). So why don't you use the GetLastError() function at these places to get the error number when that happens? Then you can search for the description for that error number in MSDN or google, and you will find what is wrong. That will be better than we pointing the error for you.

if (WSAStartup(VERSION, &wsadata) != NO_ERROR)
    {
        WSACleanup();
        return EXIT_FAILURE;
    }

open_socket(serverName, port);

SOCKET open_socket (const char *serverName, const int port)
{
    struct hostent *server = NULL;
    SOCKADDR_IN serverAddress;

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock!= INVALID_SOCKET)
    {
        server = gethostbyname(serverName);
        if (server == NULL)
        {
            sock = INVALID_SOCKET;
        }
        else 
        {
            char *ip = server->h_addr;
            SOCKADDR_IN serverAddress = 
            {
                AF_INET,
                htons(port),
                {ip[0], ip[1], ip[2], ip[3]}
            };
        }
        if (connect(sock, (SOCKADDR*) &serverAddress, sizeof(serverAddress))<0)
        {
            sock = INVALID_SOCKET;
            printf("Connection failed.");
        }
        printf("Connection successed.");
    }
    return sock;
}

Okay I think it is because you are initializing a newly defined SOCKADDR local variable inside the else branch, but using the uninitialized SOCKADDR variable of the same name. See the lines marked in Red.

Here is a version that would work

#include <winsock2.h>
#include <windows.h>
#include <stdio.h>

SOCKET open_socket (const char *serverName, const int port)
{
    struct hostent *server = NULL;

    SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP );
    if (sock!= INVALID_SOCKET)
    {
        server = gethostbyname(serverName);
        if (server == NULL)
        {
            sock = INVALID_SOCKET;
            printf("GethostName Failed\n");
            return INVALID_SOCKET;
        }
        else
        {
            char *ip = server->h_addr;
            SOCKADDR_IN serverAddress =
            {
                AF_INET,
                htons(port),
                {ip[0], ip[1], ip[2], ip[3]}
            };
            printf("Server Address %d %d %d %d \n", ip[0], ip[1], ip[2], ip[3]);
            if (connect(sock, (SOCKADDR*) &serverAddress, sizeof(serverAddress))<0)
            {
                printf("Connection failed.%d", GetLastError());
                return INVALID_SOCKET;
            }
            printf("Connection successed.");
        }
    }
    else
    {
        printf("Create Socket Failed\n");
        return INVALID_SOCKET;
    }
    return sock;
}


int main()
{
    WSADATA wsadata;
    if (WSAStartup(MAKEWORD(2, 2), &wsadata) != NO_ERROR)
    {
        printf("Startup Failure\n");
        WSACleanup();
        return EXIT_FAILURE;
    }
    open_socket("localhost", 8201);
    WSACleanup();
    return 0;
}

Thank you very much. I do not know what to say. Good reply in such a short notice. I am very much obiliged.
Thank you again.

This article has been dead for over six months. Start a new discussion instead.