+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Full Member level 5
    Points: 2,758, Level: 12

    Join Date
    Dec 2006
    Posts
    286
    Helped
    10 / 10
    Points
    2,758
    Level
    12

    why Bind( ) function returns the -1

    I am currently doing the socket programming for UDP in VC++ using Visual Studio 2005 where, I used the Bind() function to bind the socket. But I everytime gets the result..........Bind failed.
    When I checked out, the function Bind() returns -1.
    I dont get why this happen?? Please let me know.


    // Create A Socket

    int ServerSocket;
    ServerSocket = socket( PF_INET,SOCK_DGRAM, 0 );

    //Check for existance

    if ( ServerSocket < 0 )
    {
    MessageBox ("The Socket is not created!!!" );
    }

    //Bind the Socket

    int MySock;
    struct sockaddr_in MyAddr;

    MySock = socket ( PF_INET,SOCK_DGRAM, 0 );
    MyAddr.sin_family = AF_INET;
    MyAddr.sin_addr.s_addr = inet_addr( m_IPAdd );
    MyAddr.sin_port = htonl( m_iPortNo );

    bind ( ServerSocket, (SOCKADDR*) &MyAddr, sizeof( MyAddr ) );

    if ( bind ( ServerSocket, (SOCKADDR*) &MyAddr, sizeof( MyAddr ) ) == SOCKET_ERROR )
    {
    MessageBox( "bind( ) failed." );
    closesocket( ServerSocket );
    return;
    }

    •   AltAdvertisement

        
       

  2. #2
    Full Member level 3
    Points: 4,634, Level: 16

    Join Date
    Apr 2001
    Location
    California, USA
    Posts
    188
    Helped
    25 / 25
    Points
    4,634
    Level
    16

    Re: why Bind( ) function returns the -1

    You called bind twice?



--[[ ]]--