Bind();

This is a discussion on Bind(); within the Networking/Device Communication forums, part of the General Programming Boards category; I've started using WinSock recently and now that I have it working, I am getting an error in bind() when ...

  1. #1
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    Bind();

    I've started using WinSock recently and now that I have it working, I am getting an error in bind() when I run it. Here is the code for bind():
    Code:
     if( bind( socket, ( SOCKADDR* )( &sockAddr ), sizeof( sockAddr ) ) == SOCKET_ERROR )
      {
        std::cout << "Failed to bind!" << std::endl;
      }
    Whenever I run the program it is fine until it gets there, and of course prints Failed to bind! and I cannot figure out what is wrong. If it isn't this, the full code is attached.

    - SirCrono6
    Attached Files Attached Files
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,255
    you are filling your sockaddr structure incorrectly. The port needs to be converted to network byte order. Change:
    Code:
    sockAddr.sin_port = 50;
    to
    Code:
    sockAddr.sin_port = htons(50);

  3. #3
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    For the client, should I also have htons( 50 ) or something different (I've seen others)? Oh, and that worked, thanks
    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,255
    yes, you should. Whenever you fill a sockaddr or sockaddr_in structure, the data must be in network byte order.

  5. #5
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    Question Hmm

    Okay, I've done the same thing to the client program, and I am getting an error at the client's connect() function. I'm guessing it's not the call to connect() itself, so I'll just post all the code.
    Code:
    #include <iostream>
    #include <windows.h>
    #include <WINSOCK2.h>
    
    int main( void )
    {
      WSADATA bWsaDat;
      SOCKET bSocket;
      SOCKADDR_IN bSockAddr;
      HOSTENT *bHost;
      char bSendMe[] = "Awesome, I sent!", bServer[] = "localhost";
      char bRecieveMe[50];
      int bRetVal = SOCKET_ERROR;
      if( WSAStartup( MAKEWORD( 2, 0 ), &bWsaDat ) != 0 )
      {
        std::cout << "WSA initialization failed!" << std::endl;
      }
      bSocket = ( AF_INET, SOCK_STREAM, 0 );
      if( bSocket == INVALID_SOCKET )
      {
        std::cout << "Failed to create socket!" << std::endl;
      }
      std::cout << "Waiting for host..." << std::endl;
      while( bHost == NULL )
      {
        bHost = gethostbyname( bServer );
        if( bHost != NULL )
        {
          break;
        }
      }
      bSockAddr.sin_port = htons( 50 ); //I've added it here...
      bSockAddr.sin_family = AF_INET;
      bSockAddr.sin_addr.s_addr = *( unsigned long* ) bHost->h_addr;
      if( connect( bSocket, ( SOCKADDR* )( &bSockAddr ), sizeof( bSockAddr ) ) != 0 )
      {
        std::cout << "Failed to establish connection with server!" << std::endl;
      }
      while( bRetVal == SOCKET_ERROR )
      {
        bRetVal = send( bSocket, bSendMe, strlen( bSendMe ) + 1, 0 );
        if( bRetVal == 0 || bRetVal == WSAECONNRESET )
        {
          std::cout << "Connection closed at other end!" << std::endl;
          break;
        }
      }
      bRetVal = SOCKET_ERROR;
      while( bRetVal == SOCKET_ERROR )
      {
        bRetVal = recv( bSocket, bRecieveMe, 50, 0 );
        if( bRetVal == 0 || bRetVal == WSAECONNRESET )
        {
          std::cout << "Connection closed at other end!" << std::endl;
          break;
        }
      }
      std::cout << bRecieveMe << std::endl;
      closesocket( bSocket );
      WSACleanup();
      std::cin.get();
      return 0;
    }
    Thanks, and all the variables have b infront of them because I thought it caused the first error
    - SirCrono6

    Edit: Maybe it's not that, because now the server is failing to bind() again
    Last edited by SirCrono6; 02-26-2005 at 06:18 PM.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,255
    Code:
    bSocket = ( AF_INET, SOCK_STREAM, 0 );
    should be
    Code:
    bSocket = socket( AF_INET, SOCK_STREAM, 0 );
    Code:
    while( bHost == NULL )
      {
        bHost = gethostbyname( bServer );
        if( bHost != NULL )
        {
          break;
        }
      }
    The above code is bad for a couple reasons. First, if gethostbyname() fails once, it's just going to put you in an endless loop of failure. Second, since bHost is never initialized, what happens if it starts out as something other than 0? Your gethostbyname() function would never get called.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bind() fails with WSAEACCES
    By pheres in forum Tech Board
    Replies: 2
    Last Post: 02-24-2009, 01:58 PM
  2. Can I bind a UDP socket to a port, but send to any other port?
    By trillianjedi in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-25-2009, 04:27 PM
  3. What does bind() do, exactly?
    By Hunter2 in forum Networking/Device Communication
    Replies: 9
    Last Post: 07-07-2005, 01:12 PM
  4. bind
    By chrismiceli in forum Linux Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. bind() error
    By PutoAmo in forum C Programming
    Replies: 5
    Last Post: 05-23-2002, 04:57 PM

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