This is used to send a message on the specified socket. The message can be sent in connection mode as well as in connectionless mode. In the case of connectionless mode, the message is sent to the specified address. Here it its syntax:
ssize_t sendto(int fdsock, const void *buff, size_t len, int flags, const struct sockaddr *recv_addr, socklen_t recv_len);
Here, we need to address the following:
- fdsock: Specifies the file descriptor of the socket.
- buff: Points to a buffer that contains the message to be sent.
- len: Specifies the length of the message in bytes.
- flags: Specifies the type of the message that is being transmitted. Usually, its value is kept as 0.
- recv_addr: Points to the sockaddr structure that contains the receiver's address. The length and format of the address depends on the address family that's been assigned to the socket.
- recv_len: Specifies the length of the sockaddr structure that's pointed to by the recv_addr argument.
On successful execution, the function returns the number of bytes sent, otherwise it returns -1.