What Is a Master Socket?

Socket (socket) is an abstraction layer through which applications can send or receive data, which can be opened, read, written, and closed like files. Sockets allow applications to insert I / O into the network and communicate with other applications on the network. A network socket is a combination of an IP address and a port.

Socket was originally
In order to meet different communication program pairs
Take some basic socket system calls of Berkeley Socket as examples. WinsockAPI also has these calls.
1. Create a socket socket () Any user must create a socket to communicate. Creating a socket is achieved by a system call socket () function.
(1) The format of the socket () function call:
  SOCKET PASCAL FAR socket (int af, int type, int protocol);
(2) The parameters of the socket () function: af, type, protocol. The parameter af specifies the protocol family used by the socket. That is, use it to distinguish the type of address. The protocol families supported by UNIX are: UNIXDomain (AF_UNIX), In-
temet (AF_INET), XeroxNS (AF_NS), etc. Dos and Windows only support AF_INET. The type parameter specifies the type of communication required. Including data stream (SOCK_STREAM), datagram (SOCK-DGRAM) and primitive type (S0CK_RAW). The protocol parameter specifies the specific protocol in the protocol family used by the socket. If the caller does not want to specify the protocol to be used, set it to 0 and use the default connection mode.
(3) The function call succeeds and returns a socket descriptor. The function call fails and the return value is INVALID_SOCKET. [3]
When communication to a socket ends, the function must be called to close the specified socket.
Function format:
  BOOL closesocket (SOCKETs); 
s is the descriptor of the socket to be closed after the communication task has ended. [3]
The socket call flow is shown in the following figure.
Socket call process
socket (): Create a socket.
bind (): Specify a local address. After a socket is created with socket (), it is not actually associated with any particular local or destination address. In many cases, applications do not care about the local address they use. In this case, instead of calling bind to specify the local address, the protocol software chooses one for them. However, a server process operating on a well-known port must specify a local port to the system. So once a socket is created, the server must use the bind () system call to establish a local address for the socket.
connect (): Connect the socket to the destination address. The socket created initially is not associated with any foreign destination address. The client can call connect () to bind a permanent destination address to the socket and place it in the connected state. For data stream sockets, you must call connect () to construct a TCP connection to the destination before transmitting data, and return an error code if the connection cannot be constructed. In the datagram mode, you do not have to call connect before transmitting data. If connect () is called, it does not send a connection request message like the data stream method, but only stores the destination address locally. All data sent on this socket will be sent to this address, and the programmer can Eliminate the need to specify a destination address for every data sent.
listen (): Sets the status of waiting for a connection. For a server program, when applying for a socket and calling bind () to bind to a local address, it should wait for a client program to request a connection. listen () is a function that sets a socket to this state.
accept (): Accept the connection request. The server process uses the system calls socket, bind, and listen to create a socket, bind it to a well-known port, and specify the queue length of the connection request. The server then calls accept to enter the wait state until a connection request is reached.
send () / recv () and sendto () / recvfrom (): Send and receive data. In the data stream mode, after a connection is established, or in the datagram mode, after calling connect () to bind the socket to the destination address, you can call send () and reev () functions for data transmission .
closesocket (): Close the socket. [3]

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?