Send and receive functions in the socket

Source: Internet
Author: User
Tags error code sendmsg socket
Network I/O operations:
(a) Read ()/write ()
(ii) recv ()/send ()
(iii) READV ()/writev ()
(iv) RECVMSG ()/sendmsg ()
(Fri) recvfrom ()/sendto ()
---------------------------------------------


(1) It is recommended to use the Recvmsg ()/sendmsg () function, which is the most general I/O function and can actually replace the other functions above.
(2) Each function declaration is as follows:


#include <unistd.h>


ssize_t Read (int fd, void *buf, size_t count);
ssize_t Write (int fd, const void *buf, size_t count);


#include <sys/types.h>
#include <sys/socket.h>


ssize_t Send (int sockfd, const void *buf, size_t len, int flags);
ssize_t recv (int sockfd, void *buf, size_t len, int flags);


ssize_t sendto (int sockfd, const void *buf, size_t len, int flags,
const struct SOCKADDR *dest_addr, socklen_t Addrlen);
ssize_t recvfrom (int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);


ssize_t sendmsg (int sockfd, const struct MSGHDR *msg, int flags);
ssize_t recvmsg (int sockfd, struct MSGHDR *msg, int flags);
-------------------------------------------
(3) Read ()/write ():
1) Read function:
The read function is responsible for reading the content from FD. When read succeeds, read returns the actual number of bytes read, if the returned value is 0 to indicate that the end of the file has been read, and less than 0 indicates an error occurred. If the error is eintr, the read is caused by an interrupt, if econnrest indicates a problem with the network connection.
2) Wirte function:
The Write function writes the nbytes byte content in buf to the file descriptor FD. Returns the number of bytes written when successful. Returns 1 on failure and sets the errno variable.
Two return results:
1) The return value of write is greater than 0, indicating that some or all of the data is written.
2) The returned value is less than 0, and an error occurs. We are going to deal with the error type. If the error is EINTR, an interrupt error occurred while writing. If the epipe indicates a problem with the network connection (the other party has closed the connection).


-----------------------------------------
(4) recv ()/send ():
1) The Send function:
The parameters of send have the following meanings:
SOCKFD: The socket descriptor that represents your connection to the remote program.
Msg: A pointer to the address of the message you want to send.
Len: Is the length of the message you want to send.
Flags: Send tokens. Usually set to 0 (you can view the man pages of send for additional parameter values and understand what each parameter means).


2) Send return value:
The Send () function returns the length of the data it actually sends after it is called, and the Send () function returns –1 if an error occurs.
Attention:
Send may have less data than the length you specified for the parameter you gave it. Because if you give send () the length of the data contained in the parameter is much larger than the data sent by send (), the Send () function sends only the maximum data length that it can send, and then it believes that you will call it again for the second time to send the rest of the data. So, remember that if the return value of the Send () function is less than Len, then you need to send the rest of the data again. Fortunately, if the package is small enough (less than 1K), send () usually sends the light one at a time.


3) recv function:
SOCKFD: Is the socket descriptor for which you want to read data.
BUF: is a pointer to the memory cache area where you can store data.
Len: is the maximum size of the buffer.
Flags: A flag for the recv () function, typically 0 (see recv () for other values and meanings


4) recv return value:
Recv () returns the length of the data it actually receives. (That is, the length of the data stored in the BUF). If the return –1 represents an error (such as an outage outside the network, the other side closes the socket connection, and so on), the global variable errno stores the error code.


------------------------------------------
(5) Readv ()/writev ()
(6) Recvmsg ()/sendmsg ()
(7) Recvfrom ()/sendto ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.