Accepting Incoming Connections: accept
The accept function is used to explicitly pull an incoming connection from the listen queue and to accept it.
int accept(int socketDescriptor, struct sockaddr* address, socklen_t* addressLength);
The accept function is a blocking function. If there is no pending incoming connection request in the listen queue, it puts the calling process into a suspended state until a new incoming connection arrives. The accept function requires the following arguments to be provided in order to accept a pending incoming connection:
- The socket descriptor specifies the socket instance that the application wants to accept a pending incoming connection on.
- The address pointer provides an address structure that gets filled with the protocol address of the connecting client. If this information is not needed by the application, it can be set to NULL.
- The address length pointer provides memory space for the size of the protocol address of the connecting client to be filled in. If this information is not needed, it can be set to NULL.
Result:
accept request is successful, the function returns the client socket descriptor that will be used; -1 and the errno global variable is set to appropriate error.
Receive Data from the Scoket: recv
The recv function could receive data from socket:
ssize_t recv(int socketDescriptor, void *buffer, size_t bufferLength, int flags);
recv function is also a blocking function. If there is no data that can be received from the given socket, it puts the calling process into suspended state until data becomes available. The recv function requires the following arguments to be provided in order to accept a pending incoming connection:
- The socket descriptor specifies the socket instance that the application wants to receive data from.
- The buffer pointer to a memory address that will be filled with the data received from the socket.
- The buffer length specifies the size of the buffer. The recv function will only fill the buffer up to this size and return.
- Flags specify additional flags for receiving.
Result:
If the recv function is successful, it returns the number of bytes received from the socket; otherwise, it returns -1 and the errno global variable is set to the appropriate error. If the function returns zero, it indicates that the socket is disconnected.
Sending Data to the Socket: send
Sending data to the socket is achieved through the send function.
ssize_t send(int socketDescriptor, void* buffer, size_t bufferLength,int flags);
Like the recv function, the send function is also a blocking function. If the socket is busy sending data, it puts the calling process into a suspended state until the socket becomes available to transmit the data. The send function requires the following arguments to be provided in order to accept a pending incoming connection:
- The socket descriptor specifies the socket instance that the application wants to send data to.
- The buffer pointer to a memory address that will be sent through the given socket.
- The buffer length specifies the size of the buffer. The send function will only transmit the buffer up to this size and return.
- Flags specify additional flags for sending.
Result:
send function returns the number of bytes transmitted; -1 and the errno global variable is set to the appropriate error. Like the recv, if it return 0, it shows that the connection of socket is failed.
Connect to Address: connect
Connecting a socket to a server socket by providing the protocol address is achieved through the connect function.
int connect(int socketDescriptor, const struct sockaddr *address,socklen_t addressLength);
The connect function requires the following arguments to be provided in order to accept a pending incoming connection:
- The socket descriptor specifies the socket instance that the application wants to connect to a protocol address.
- The address specifies the protocol address that the socket will connect.
- The address length specifies the length of the address structure provided.
If the connection attempt is successful, the connect function returns zero; otherwise, it returns -1 and the errno global variable is set to the appropriate error.