Binds to the given local address.
Parameter
-
local_sockaddr
- thestruct
sockaddr contained in a string or an Addrinfo object
Example
require 'socket' # use Addrinfo socket = Socket.new(:INET, :STREAM, 0) socket.bind(Addrinfo.tcp("127.0.0.1", 2222)) p socket.local_address #=> #<Addrinfo: 127.0.0.1:2222 TCP> # use struct sockaddr include Socket::Constants socket = Socket.new( AF_INET, SOCK_STREAM, 0 ) sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' ) socket.bind( sockaddr )
Unix-based Exceptions
On unix-based based systems the following system exceptions may be raised if the call to bind fails:
-
Errno::EACCES - the specified sockaddr is protected and the current user does not have permission to bind to it
-
Errno::EADDRINUSE - the specified sockaddr is already in use
-
Errno::EADDRNOTAVAIL - the specified sockaddr is not available from the local machine
-
Errno::EAFNOSUPPORT - the specified sockaddr is not a valid address for the family of the calling
socket
-
Errno::EBADF - the sockaddr specified is not a valid file descriptor
-
Errno::EFAULT - the sockaddr argument cannot be accessed
-
Errno::EINVAL - the
socket
is already bound to an address, and the protocol does not support binding to the new sockaddr or thesocket
has been shut down. -
Errno::EINVAL - the address length is not a valid length for the address family
-
Errno::ENAMETOOLONG - the pathname resolved had a length which exceeded PATH_MAX
-
Errno::ENOBUFS - no buffer space is available
-
Errno::ENOSR - there were insufficient STREAMS resources available to complete the operation
-
Errno::ENOTSOCK - the
socket
does not refer to a socket -
Errno::EOPNOTSUPP - the socket type of the
socket
does not support binding to an address
On unix-based based systems if the address family of the calling
socket
is Socket::AF_UNIX the follow exceptions may be raised
if the call to bind fails:
-
Errno::EACCES - search permission is denied for a component of the prefix path or write access to the
socket
is denied -
Errno::EDESTADDRREQ - the sockaddr argument is a null pointer
-
Errno::EISDIR - same as Errno::EDESTADDRREQ
-
Errno::EIO - an i/o error occurred
-
Errno::ELOOP - too many symbolic links were encountered in translating the pathname in sockaddr
-
Errno::ENAMETOOLLONG - a component of a pathname exceeded NAME_MAX characters, or an entire pathname exceeded PATH_MAX characters
-
Errno::ENOENT - a component of the pathname does not name an existing file or the pathname is an empty string
-
Errno::ENOTDIR - a component of the path prefix of the pathname in sockaddr is not a directory
-
Errno::EROFS - the name would reside on a read only filesystem
Windows Exceptions
On Windows systems the following system exceptions may be raised if the call to bind fails:
-
Errno::ENETDOWNâ the network is down
-
Errno::EACCES - the attempt to connect the datagram socket to the broadcast address failed
-
Errno::EADDRINUSE - the socket's local address is already in use
-
Errno::EADDRNOTAVAIL - the specified address is not a valid address for this computer
-
Errno::EFAULT - the socket's internal address or address length parameter is too small or is not a valid part of the user space addressed
-
Errno::EINVAL - the
socket
is already bound to an address -
Errno::ENOBUFS - no buffer space is available
-
Errno::ENOTSOCK - the
socket
argument does not refer to a socket
See
-
bind manual pages on unix-based systems
-
bind function in Microsoft's Winsock functions reference
Please login to continue.