bind(2) - Linux man page
4 stars based on
It is commonly implemented as a library of linkable modules. It originated with the 4. A socket is an abstract representation handle for the local endpoint of a network communication path. The Berkeley sockets API represents it as a file descriptor file handle in the Unix philosophy that provides a common interface for input and output to streams of data.
Berkeley sockets evolved with little modification from a de facto standard into a component of the POSIX specification. Berkeley sockets originated with the 4. It became the standard interface for connecting to the Internet. Even the Winsock implementation for MS Windows, developed by unaffiliated developers, closely follows the standard. Most other programming languages provide similar interfaces, typically written as a wrapper library based on the C API.
The Berkeley socket interface is defined in several header files. The names and content of these files differ slightly between implementations. In general, they include:. The function socket creates an endpoint for communication and returns a file descriptor for the socket. The function returns -1 if an error occurred. Otherwise, it returns an bind a socket representing the newly assigned descriptor. When a socket is created using socketit is only given a protocol family, but not assigned an address.
This association with an address must be performed with the bind system call before the socket can accept connections to other hosts. After a socket has been associated with an address, listen prepares it for incoming connections. However, this is only necessary for the stream-oriented connection-oriented data modes, i.
Once a connection is accepted, it is dequeued. On success, 0 is returned. If an error occurs, -1 is returned. When bind a socket application is listening for stream-oriented connections from other hosts, it is notified of bind a socket events cf. The accept function creates a new socket for each bind a socket and removes the connection from the listen queue.
It takes the following arguments:. The accept function returns the new socket descriptor for the accepted connection, or -1 if an error occurs. All further communication with the remote host now occurs via this new socket. Datagram sockets do not require processing by accept since the bind a socket may immediately respond to the request using the listening socket.
The connect system call establishes a direct communication link to a specific remote host identified by its address via a socket, identified by its file descriptor.
When using a connection-oriented protocol, this establishes a connection. Certain types of protocols are connectionlessmost notably the User Datagram Protocol. When used with connectionless protocols, connect defines the remote address for sending and receiving data, allowing the use of functions such as send and recv.
In these cases, the connect function prevents reception of datagrams bind a socket other sources. Historically, in the BSD-derived systems, the state of a socket descriptor is undefined if the call to connect bind a socket as it is specified in the Single Unix Specificationthus, portable applications should close the socket descriptor immediately and obtain a new descriptor with socketin the case the call to connect fails.
The gethostbyname and gethostbyaddr functions are used to resolve host names and addresses in the domain name system or the local host's other resolver mechanisms e. They return a pointer to an object of type struct hostentwhich describes an Internet Protocol host. The functions take the following arguments:.
Furthermore, these functions are now considered legacy interfaces for querying the domain name system. New functions that are completely protocol-agnostic supporting IPv6 have been defined. These new function are getaddrinfo and getnameinfoand are based on a new addrinfo data structure. The socket API is a general interface for Unix networking and allows the use of various network protocols and addressing architectures. The following lists a sampling of protocol families preceded by the standard symbolic identifier defined in a modern Linux or BSD implementation:.
The original design concept of the socket interface bind a socket between protocol types families and the specific address types that each may use.
It was envisioned that a protocol family may have several address types. Address types were defined by additional symbolic constants, using the prefix AF instead of PF. The AF -identifiers are intended for all data structures that specifically deal with the address type and not the protocol family. However, this concept of separation of protocol and address type has not found implementation support and the AF -constants were defined by the corresponding protocol identifier, leaving the distinction between AF and PF constants as a technical argument of no practical consequence.
Indeed, much confusion exists in the proper usage of both forms. They permit implementation of networking protocols in bind a socket space and aid in debugging of the protocol stack. After creating a socket, it is possible to set options on it. Some of the more common options are:. A blocking socket does not return control until it has sent or received some or all data specified for the operation.
It is normal for a blocking socket not to send all data. The application bind a socket check the return value to determine how many bytes have been sent or bind a socket and it must resend any data not already processed. On the other hand, a non-blocking socket returns whatever is in the receive buffer and immediately continues. Bind a socket not written correctly, programs using non-blocking sockets are particularly susceptible to race conditions due to variances in network link speed.
A socket is typically set to blocking or nonblocking mode using the fcntl or ioctl functions. The operating system does not release the resources allocated to bind a socket socket until the socket is closed.
This is especially important if the connect call fails and will be retried. When an application closes a socket, only the interface to the socket is destroyed. It is the kernel's responsibility to destroy the socket internally. On SVR4 systems use of close may discard data. The Transmission Control Protocol TCP is a connection-oriented protocol that provides a variety of error correction and performance features for transmission of byte streams.
UDP packets may arrive out of order, multiple times, or not at all. Being connectionless means that there is no concept of a stream or permanent connection between two hosts. Such data are referred to as datagrams Datagram Sockets.
An application may set up a UDP server on port number as follows. The programs contains an infinite loop that receives UDP datagrams using recvfrom. For TCP connections, the operating system may have to retransmit the data given to it with a write call. However, the bind a socket space program is free to delete the data buffer passed to write after write returns. Other APIs, such as those supporting RDMA require that the data buffer is not released until the acknowledgement from the remote end has bind a socket received and thus make it possible to have zero memory copy operations.
Information about this standard and ongoing work on it is available from the Austin website. This article bind a socket based on material taken from the Free On-line Dictionary of Computing prior to 1 Bind a socket and incorporated under the "relicensing" terms of the GFDLversion 1.
From Wikipedia, the free encyclopedia. Rudoff, Addison Wesley, Retrieved from bind a socket https: Network socket Application programming interfaces.