![]() |
| Home |
|
|
Function bindBind a socket to a local port number and IP address. int bind(SOCKET socket, const struct sockaddr * addr, int * addrlen); ParameterssocketSocket returned by socket. addrIP address and port number to associate with the socket. Port number and IP address are in network byte order. This parameter has type struct sockaddr * for historical reasons. Use type struct sockaddr_in * instead. addrlenPointer to length of valid information in addr. return valueReturns 0 if successful, otherwise SOCKET_ERROR. If an error occurred, call xn_getlasterror and xn_geterror_string to return the error value. Section Error Codes further describes each error. Possible values for this function are:
This routine attaches a specific input port number and IP address to an unconnected socket. For TCP, it is called before listen/accept. Server applications will call bind to establish a well known port for clients to call on. bind does not establish any connections itself. If bind is called with INADDR_ANY for its IP address, packets from any of its interfaces are accepted. If bind is called with 0 for its port number, the system assigns a unique port number. For UDP and RAW, in order to receive broadcasts, the socket should be bound to INADDR_ANY or to the broadcast address. To receive multicasts, you can bind to the local IP address, INADDR_BROADCAST, or to the multicast address. You may also bind to INADDR_ANY to receive all packets sent to a specific port number. It is not necessary to call bind. If connect or sendto is called and bind has not been called, an implicit bind is done with a random port number in the range 1024 to 5000 and the local IP address associated with the same interface connect or sendto is using.
|