Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Usage of bind()

Usage of bind()

Scheduled Pinned Locked Moved C / C++ / MFC
comtutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dellthinker
    wrote on last edited by
    #1

    Hi all. I have a project that requires me to call bind() for Windows to bind with an address. I went here: http://msdn2.microsoft.com/en-us/library/ms737550.aspx to find out how its used but im a bit confused. It says i need sockaddr() in order to bind. Is that correct? Can someone show me what to do here because im lost. It would be nice if i could see some example code with the bind() function in it. Thanx in advance!

    J M 2 Replies Last reply
    0
    • D dellthinker

      Hi all. I have a project that requires me to call bind() for Windows to bind with an address. I went here: http://msdn2.microsoft.com/en-us/library/ms737550.aspx to find out how its used but im a bit confused. It says i need sockaddr() in order to bind. Is that correct? Can someone show me what to do here because im lost. It would be nice if i could see some example code with the bind() function in it. Thanx in advance!

      J Offline
      J Offline
      JudyL_MD
      wrote on last edited by
      #2

      dellthinker wrote:

      some example code with the bind() function in it.

      Here you go:

      hostent \*     localHost;
      char \*        localIP;
      SOCKADDR\_IN   localAddress;
      
      m\_hSocket = socket (AF\_INET, SOCK\_STREAM, IPPROTO\_TCP);
      if (m\_hSocket == INVALID\_SOCKET)
      {
          return false;
      }
      
      // build our address
      memset (&localAddress, 0, sizeof (SOCKADDR\_IN));
      
      localHost = gethostbyname ("");
      localIP   = inet\_ntoa (\*((struct in\_addr \*) \*(localHost->h\_addr\_list)));
      
      localAddress.sin\_addr.s\_addr = inet\_addr (localIP);
      localAddress.sin\_family      = AF\_INET;
      localAddress.sin\_port        = htons (usPort);
      
      // bind to it
      if (bind (m\_hSocket, (SOCKADDR \*) &localAddress, sizeof (SOCKADDR\_IN)) == SOCKET\_ERROR)
      {
          closesocket (m\_hSocket);
          m\_hSocket = INVALID\_SOCKET;
          return false;
      }
      
      // finally, listen on it
      if (listen (m\_hSocket, SOMAXCONN) == SOCKET\_ERROR)
      {
          closesocket (m\_hSocket);
          m\_hSocket = INVALID\_SOCKET;
          return false;
      }
      

      usPort is a variable that indicates the TCP port number this socket is bound to Judy

      1 Reply Last reply
      0
      • D dellthinker

        Hi all. I have a project that requires me to call bind() for Windows to bind with an address. I went here: http://msdn2.microsoft.com/en-us/library/ms737550.aspx to find out how its used but im a bit confused. It says i need sockaddr() in order to bind. Is that correct? Can someone show me what to do here because im lost. It would be nice if i could see some example code with the bind() function in it. Thanx in advance!

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        In addition to Judy's excellent reply.... A sockaddr is a generic structure since sockets can be used on any protocol, not just IP protocols.  Therefore, for IP(4) protocols, a sockaddr_in (socket address internet) structure is used since it specifically defines a complete IP address. You can also bind to the address INADDR_ANY if you want to let WinSock pick the adapter to bind to:

        localAddress.sin_addr.s_addr = INADDR_ANY;

        Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        J 1 Reply Last reply
        0
        • M Mark Salsbery

          In addition to Judy's excellent reply.... A sockaddr is a generic structure since sockets can be used on any protocol, not just IP protocols.  Therefore, for IP(4) protocols, a sockaddr_in (socket address internet) structure is used since it specifically defines a complete IP address. You can also bind to the address INADDR_ANY if you want to let WinSock pick the adapter to bind to:

          localAddress.sin_addr.s_addr = INADDR_ANY;

          Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          J Offline
          J Offline
          JudyL_MD
          wrote on last edited by
          #4

          Mark Salsbery wrote:

          In addition to Judy's excellent reply....

          :-O:-O

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups