Home |
Function recv |
Function recvReceive data at a socket. int recv(SOCKET socket, char * buffer, int buflen, int flags); ParameterssocketSocket returned by socket or accept. bufferBuffer to receive to. buflenMaximum bytes to receive. flagsControl flags (MSG_PEEK). return valueThe number of bytes available in the buffer, 0 if end of file; (TCP only) otherwise SOCKET_ERROR. If an error occurred, call xn_getlasterror and xn_geterror_string to return the error value. Section Error Codes further describes each error. End of file is detected when the other side closes (i.e. sends a FIN) but closesocket has not been called and the input window is empty (TCP only). Possible values for this function are:
This routine will receive up to buflen bytes and copy the data *buffer. It will never return more than buflen bytes but will return as soon as bytes are available in the buffer. For UDP and RAW, bind must be called before recv is called. For TCP, the socket must be connected before calling recv. If no data is available and the socket is in blocking mode, recv blocks on the read signal until any data is available. If no data is available and the socket is in non-blocking mode, recv returns immediately with an error condition. select may be called prior to calling recv to determine when data is available. For TCP sockets, the SO_TCP_NO_COPY socket option is available (see setsockopt). When this option is enabled, recv copies data directly from the input packet queue to user space. This eliminates copying data from the input queue to the TCP window buffers and reduces processor bandwidth usage. However, if the TCP packets are small, this can result in a less efficient use of memory. For TCP, this function returns 0 if a FIN packet has been received (i.e. the remote host did a closesocket) and the input window is empty. Raw packets are queued on every socket whose protocol matches the protocol field in the packet. The socket's protocol is a parameter to socket. The data written to buffer includes the IP header. The flags parameter provides options for calls to recv. Currently, only the option MSG_PEEK is supported. It prevents the received data to be removed from the input queue. In order to receive broadcasts, use function bind with the local address set to address INADDR_ANY. To receive multicasts, you can bind to the local IP address, INADDR_ANY, or to the multicast address. You may also bind to INADDR_ANY to receive all packets sent to a specific port number. In blocking mode, this function blocks infinitely. Use select for a timeout capability. Warning: For UDP, each call to recv dequeues one packet from the socket's input queue. If there is more data in the packet than the caller requested, the residual data will be lost. This function does not support sockets in secure mode. For secure sockets, use function net_recv instead.
|