Thread: WSAGetLastError() returns 10022

  1. #1
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131

    WSAGetLastError() returns 10022

    Hi.

    I just started learning networking and I managed to make this code:

    Code:
    #include <winsock.h>
    #include <iostream>
    using namespace std;
    
    int RequestedVersion = 2;
    
    void DoStuff()
    {
        SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (hSocket == INVALID_SOCKET) { cout << "Unable to create a socket" << endl; return; }
        
        sockaddr_in sockAddr;
    
        sockAddr.sin_family = AF_INET;
        sockAddr.sin_port = htons(80);
        sockAddr.sin_addr.S_un.S_addr = inet_addr("72.14.221.104");
    
        // Connect to the server
        if (connect(hSocket, (sockaddr*)(&sockAddr), sizeof(sockAddr))!=0)
        {
            // error handling code  
    		cout << "Connection failed!" << endl;
        }
        else { cout << "Socket is connected" << endl; }
        
        if (bind(hSocket, (sockaddr*)(&sockAddr), sizeof(sockAddr))!=0)
        {
            cout << "Binding failed (" << WSAGetLastError() << ")" << endl;
        }
        
    }
    
    
    int main()
    {
        WSADATA wsadata;
        if (WSAStartup(MAKEWORD(RequestedVersion, 0), &wsadata) == 0)
        {
            cout << "Startup was successful!" << endl;
            if (RequestedVersion >= wsadata.wVersion)
            {
                cout << "Version " << wsadata.wVersion << endl;
            }
            
            DoStuff();
            
            system("pause");
            if (WSACleanup())
            {
                cout << "Cleanup failed!" << endl;
            }
        }
        else
        {
            cout << "Startup failed!" << endl;
        }
    }
    it gives the following output when run:
    Startup was successful!
    Version 2
    Socket is connected
    Binding failed (10022)
    Press any key to continue . . .

    So I can't figure out what's wrong. Please help or direct me. Thank you!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Why are you binding the socket after connecting?

    Anyway, look at bind: the second parameter is the local address for the socket. If 72.14.221.104 is not a local IP assigned to your computer, it's definitely going to fail.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Interesting. I thought binding was needed to associate the connection with the socket.
    But now i don't get it. What is binding meant for afterall? And what is a "local IP assigned to my computer"?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    In short, binding is for servers. Or if you want a specific outgoing port, but you only want that in extremely rare special circumstances, and you have to do it before calling connect().

    A local IP ... well, each computer can have more than one IP. You usually have two: the local loopback 127.0.0.1, and whatever your internet/ethernet IP is. Some computers have more: my server/router has 4: one external, two on the ethernet, and the loopack.
    A local IP is one that is assigned to the computer the program is running on and not some other computer somewhere in the world.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    I tried to create a program for connecting to an internet server and that's it.
    I understood, that first I need to make a socket by using a function called socket() and i have to pass the parameters to define the type of the connection (socket(AF_INET, SOCK_STREAM, IPPROTO_TCP).

    Then I make an object of sockaddr_in and fill in the necessary variables with the data about "which server do i want to connect to and which port of the server to use".

    then i have to use the function connect() to actually request a connection with the server.

    I managed to do that part. But then I saw a function called bind() and it was said that it is used for associating a connection with a socket - i thought it was necessary before sending and receiveing data, but then it started bugging me with errors.

    So I don't have to use bind() afterall?
    Maybe you could recommend a good tutorial(not the ones that are in the sticky posts).

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, you don't need bind(). And I don't know any good tutorials. I'm not an expert in network programming, not even remotely - the answer to this question just about exhausted my knowledge, and I had to look up some stuff in the manpages.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Ok. Thank you for helping me. I think I can move on in the tutorial now.

Popular pages Recent additions subscribe to a feed