Winsock error 10049

This is a discussion on Winsock error 10049 within the Networking/Device Communication forums, part of the General Programming Boards category; Dear programmers, i'm trying to make a internet connection in a C program using Winsock. I found a great tut ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    52

    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."
    (source:

    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:


    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)
            errorcode=ListenOnPort(6221);
        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("0.0.0.0") 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("127.0.0.1");
    
        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());
            closesocket(s);
            WSACleanup();
           //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
        //made
        listen(s, SOMAXCONN);
        printf("Done\n");
    
        //Don't forget to clean up with CloseConnection()!
    }
    kind regards,

    Libpgeak

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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    "...(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 192.168.0.28 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
    Niara
    Last edited by Niara; 07-10-2012 at 12:54 PM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21