Thread: Winsock error 10049

  1. #1
    Registered User
    Join Date
    Sep 2011

    Winsock error 10049

    Dear programmers,

    i'm trying to make a internet connection in a C program using Winsock. I found a great tut (Programming Windows TCP Sockets in C++ for the Beginner - CodeProject) but everything did not work as aspected. I have a winsock error n.o. 10049. When I search for this i see this : "The requested address is not valid in its context."

    when I try to set up a loopback connection it gives no errors, but doesn't work either: he always think that there is a connectioncall (other words: the program doesn't "hang"). What am I doing wrong?

    here is the server code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <winsock.h>
    int ListenOnPort(int portno);
    int main()
        int errorcode=0;
        printf("Hello world!\n");
       // while(1)
        printf("errorcode is %d", errorcode);
        return 0;
    //LISTENONPORT – Listens on a specified port for incoming connections
    //or data
    int ListenOnPort(int portno)
        SOCKET s;
        WSADATA w;
        int error = WSAStartup (0x0202, &w);   // Fill in WSA info
        if (error)
            return 50; //For some reason we couldn't start Winsock
        if (w.wVersion != 0x0202) //Wrong Winsock version?
            WSACleanup ();
            return 51;
        SOCKADDR_IN addr; // The address structure for a TCP socket
        addr.sin_family = AF_INET;      // Address family
        addr.sin_port = htons (portno);   // Assign port to this socket
        //Accept a connection from any IP using INADDR_ANY
        //You could pass inet_addr("") instead to accomplish the
        //same thing. If you want only to watch for a connection from a
        //specific IP, specify that //instead.
        addr.sin_addr.s_addr = inet_addr("");
        s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); // Create socket
        if (s == INVALID_SOCKET)
            return 52; //Don't continue if we couldn't create a //socket!!
        if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
            wprintf(L"bind failed with error %u\n", WSAGetLastError());
           //We couldn't bind (this will happen if you try to bind to the same
           //socket more than once)
            return 53;
        //Now we can start listening (allowing as many connections as possible to
        //be made at the same time using SOMAXCONN). You could specify any
        //integer value equal to or lesser than SOMAXCONN instead for custom
        //purposes). The function will not //return until a connection request is
        listen(s, SOMAXCONN);
        //Don't forget to clean up with CloseConnection()!
    kind regards,


    EDIT: when I change the IPaddres to I get this error.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Have you checked your firewall settings and logs to see if it is being blocked?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2011
    There are no firewall errors / blocks. The weird thing is gives a direct "done" message, but there is no client running...

  4. #4
    Registered User
    Join Date
    Mar 2005
    "...(line 65) The function (listen) will not return until a connection request is made..."

    Are you sure? 'listen' function starts your socket listening for incoming socket connections until the maximum you specifyed (SOMAXCONN), and returns immediatelly an integer as error code (0 on success and SOCKET_ERROR on failure). So there's nothing strange on your code if you call listen and it printfs immediatelly the 'Done' statement, simply add an 'accept' to block your program until a connection request is made (check the winsock reference for more information about 'accept').

    The error code 10049 could be meaning that you don't have the local IP address; have you checked your local IP? If you have setup your network connection to be automathic you can have different IP each time you start your pc.

    Hope that helps
    Last edited by Niara; 07-10-2012 at 12:54 PM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Thanks Niara, the accept was the trick =]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WinSock error 10049
    By homer_3 in forum C Programming
    Replies: 1
    Last Post: 03-23-2010, 06:20 PM
  2. Socket Error 10049
    By pobri19 in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2008, 07:21 AM
  3. UnKnown winsock error when compiling...
    By Finchie_88 in forum Networking/Device Communication
    Replies: 1
    Last Post: 11-30-2004, 04:06 PM
  4. WinSock Defined Function Error
    By CPP-Null in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2003, 09:58 AM
  5. WSARecv Error :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 05-15-2002, 02:25 PM