39 Network-related functions (vii)--LIVE555 source Reading (iv) network
- 39 Network-related functions (vii)--LIVE555 source Reading (iv) network
- Brief introduction
- Readsocket) read data from the socket interface
- Recv/recvfrom function
- Function Prototypes:
- Parameter description:
- Return Description:
This article by Mob Lym fabricated, welcome reprintblog.cnblogs.net/oloroso
This article by Mob Lym fabricated, welcome reprintmy.oschina.net/oloroso
Brief introduction
Network-related functions are a series of functions for manipulating network data. There are definitions of related functions in multiple files. There are also some functions socket API
that are system-related functions that are not mentioned.
Most of the functions of this series have a characteristic and require a usageenvironmet& type parameter.
Most of these methods are live555sourcecontrol\groupsock\include\GroupsockHelper.hh
declared in.
Readsocket) read data from the socket interface
readSocket
The function reads data from the socket socket
to buffe
R, and captures the address of the source to which the data is sent fromAddress
.
The function returns the number of bytes read to 出错
0
and is called socketErr(env, "recvfrom() error: ")
to set the socket interface error message.
Read data int readsocket from Socket interface (usageenvironment& env, int socket, unsigned char* buffer, unsigned buffersize,struct Socka ddr_in& fromaddress) {socklen_t addresssize = sizeof fromaddress; ssize_t recvfrom (int sockfd,void *buf,int len,unsigned int flags, struct sockaddr *from,socket_t *fromlen); Reads the data from the host via the specified socket and transmits the data to the memory space pointed to by the parameter buf, and the parameter Len is the maximum length of the data that can be received. Flag is typically set to 0. From is the source address, fromlen outgoing source length//If the correct receive returns the number of bytes received, the failure returns-1. int bytesread = Recvfrom (socket, (char*) buffer, buffersize, 0, (struct sockaddr*) &fromaddress, &addr Esssize); if (Bytesread < 0) {//##### HACK to work around bugs in Linux and windows:int err = Env.geterrno (); if (err = = 111/*econnrefused (Linux) connection request rejected by Server */#if defined (__win32__) | | defined (_WIN32)//What a Piec E of crap Windows is. sometimes//Recvfrom () Returns-1, but with a ' errno ' of 0. This appears is a real error; Just treat//it as ifIt were a read of zero bytes, and hope//We do anything else to ' reset '//This Alleg Ed Error://Junk windows. Sometimes recvfrom () returns 1, but errno is 0. It doesn't seem like a real mistake; just take it as a read 0 bytes and hopefully we don't need to do what "reset"//This so-called Error: | | Err = = 0 | | Err = = Ewouldblock#else | | Err = = Eagain#endif | | Err = = 113/*ehostunreach (Linux) */) {//Why does Linux return this for datagram sock? fromAddress.sin_addr.s_addr = 0; return 0; }//##### END HACK socketerr (env, "Recvfrom () Error:"); } return bytesread;}
Recv/recvfrom function
Function Description:
Receives a message from a socket. For recvfrom
, you can apply both to the socket 连
and to 无连接
the socket. recv
typically used only in a face-oriented socket, it is almost identical to the first 连接
recvfrom
recvfrom
五
parameter set NULL
.
If the message is too large to be fully stored in the provided buffer, the extra bytes will be based on the different sockets 丢弃
.
If no message can be read on the socket, the receive call waits for the message to arrive, except that the socket is set to non-blocking mode.
Function Prototypes:
#include <sys/types.h>#include <sys/socket.h>ssize_t recv(int sock, void *buf, size_t len, int flags);ssize_t recvfrom(int sock, void *buf, size_t len, int flags,struct sockaddr *from, socklen_t *fromlen);
Parameter description:
- Sock: The socket from which the index will receive data.
- BUF: Holds the buffer after the message is received.
- Len:buf the capacity of the indicated buffer.
- Flags: is a combination of one or more of the following flags, which can be joined together by or operation
MSG_DONTWAIT
: The operation is not blocked.
MSG_ERRQUEUE
: Indicates that the error value should be received from the socket's error queue and that, depending on the protocol, the error value is passed in as an auxiliary message, and the consumer should provide a sufficiently large buffer. The intact package that causes the error is msg_iovec
passed as general data. The datagram that causes the error is provided as the original destination address msg_name
. Errors sock_extended_err
are used in structural form and are defined as follows
#define SO_EE_ORIGIN_NONE 0#define SO_EE_ORIGIN_LOCAL 1#define SO_EE_ORIGIN_ICMP 2#define SO_EE_ORIGIN_ICMP6 3struct sock_extended_err{ u_int32_t ee_errno; /* error number */ u_int8_t ee_origin; /* where the error originated */ u_int8_t ee_type; /* type */ u_int8_t ee_code; /* code */ u_int8_t ee_pad; u_int32_t ee_info; /* additional information */ u_int32_t ee_data; /* other data */ /* More data may follow */};
Macro |
Description |
Msg_peek |
Indicates that after the data has been received, the original data is retained in the receive queue, not deleted, and subsequent read operations can receive the same data. |
Msg_trunc |
Returns the actual length of the packet, even if it is longer than the buffer provided, only valid for the packet socket. |
Msg_waitall |
Blocking operations are required until the request is fully satisfied. However, if a signal is caught, the error or connection is broken, or the next data type is received, the data will still be returned less than the requested amount. |
Msg_eor |
Indicates the end of the record, and the returned data completes a record. |
Msg_trunc |
Indicates that the datagram trailer data has been discarded because it requires more space than the supplied buffer. |
Msg_ctrunc |
Indicates that some control data has been discarded because of insufficient buffer space. |
Msg_oob |
Indicates that Out-of-band data is received (that is, data that needs to be processed preferentially). |
Msg_errqueue |
Indicates that no additional data was received except for errors from the socket error queue. |
- From: points to the area where the peer address is stored, and if NULL, the peer address is not stored.
- Fromlen: As the entry parameter, point to the memory unit that represents the maximum capacity of the from. As an exit parameter, point to the memory unit that represents the from actual length.
Return Description:
When executed successfully, returns the number of bytes received. Returned if the other end is closed 0
. Failed to return -1
, errno
set to one of the following values
- Eagain: The socket is marked as non-blocking, and the receive operation is blocked or the receive time-out
- Ebadf:sock is not a valid descriptive word.
- Econnrefuse: Remote host blocking network connection
- Efault: Error in memory space access
- EINTR: Operation interrupted by signal
- EINVAL: Invalid Parameter
- ENOMEM: Low Memory
- Enotconn: Socket associated with connection-oriented is not yet connected
- Enotsock:sock index is not a socket
39 Network-related functions (vii)--LIVE555 source Reading (iv) network