Usage of bind()
-
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!
-
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!
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 -
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!
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:
-
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: