C language through socket to receive data related functions

  • 2020-04-02 03:21:06
  • OfStack

The recv () function:
The header file:


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

Definition function:


int recv(int s, void *buf, int len, unsigned int flags);

Recv () is used to receive data from the specified socket of the remote host and store the data in the memory space pointed by the buf parameter, len is the maximum length of the data to be received.

Parameter flags is generally set to 0. Other values are defined as follows:
1. MSG_OOB receives data sent out in out-of-band.
2. The data returned by MSG_PEEK will not be deleted in the system. If recv() is called again, the same data content will be returned.
3. MSG_WAITALL cannot return len size data unless there is an error or signal.
MSG_NOSIGNAL this operation does not want to be interrupted by SIGPIPE signal return value, return the number of received characters successfully, failure to return -1, error reason is stored in errno.

Error code:
    EBADF parameter s invalid socket handling code
    The EFAULT parameter has a pointer to an inaccessible memory space
    ENOTSOCK parameter s is a file descriptor, not a socket.
    EINTR is interrupted by the signal
    This action will block the process, but the socket of parameter s is not blocked
    The ENOBUFS system is out of buffer memory.
    ENOMEM is out of core memory
    EINVAL passed an incorrect parameter to the system call.

Recvfrom () function:
The header file:


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

Definition function:


int recvfrom(int s, void *buf, int len, unsigned int flags, struct sockaddr *from,int *fromlen);

Function description: the recv () is used to receive data to the remote host via the specified socket, and save the data to the by the parameter buf to memory space, the maximum length of the parameters as the len can receive data. Parameter flags generally set 0, other numerical definition refer to the recv (). The parameters from the transfer is used to specify the desire of network addresses, sockaddr structure please refer to the bind (). The argument fromlen is the length of the sockaddr structure.

Return value: the number of characters received is returned on success, -1 on failure, and the reason for the error is in errno.

Error code:
    EBADF parameter s invalid socket handling code
    The EFAULT parameter has a pointer to an inaccessible memory space.
    ENOTSOCK parameter s is a file descriptor, not a socket.
    EINTR is interrupted by the signal.
    This action will block the process, but the socket of parameter s is not blocked.
    The ENOBUFS system is out of buffer memory
    ENOMEM is out of core memory
    EINVAL passed an incorrect parameter to the system call.

Example:
Using socket UDP client this program will connect to UDP server, and the keyboard input string to the server.
Refer to sendto (). */ for the UDP server example


#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/typs.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 2345
#define SERVER_IP "127.0.0.1"
main()
{
  int s, len;
  struct sockaddr_in addr;
  int addr_len = sizeof(struct sockaddr_in);
  char buffer[256];
  //To establish a socket
  if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  {
    perror("socket");
    exit(1);
  }
  //Fill in the sockaddr_in
  bzero(&addr, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(PORT);
  addr.sin_addr.s_addr = inet_addr(SERVER_IP);
  while(1)
  {
    bzero(buffer, sizeof(buffer));
    //Gets a string from a standard input device
    len = read(STDIN_FILENO, buffer, sizeof(buffer));
    //Pass the string to the server side
    sendto(s, buffer, len, 0, &addr, addr_len);
    //Receive the string returned by the server side
    len = recvfrom(s, buffer, sizeof(buffer), 0, &addr, &addr_len);
    printf("receive: %s", buffer);
  }
}

Execution (first execute udp server and then execute udp client) :


hello //Enter a string from the keyboard
receive: hello //The string returned by the server

The recvmsg () function:
The header file:


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

Definition function:


int recvmsg(int s, struct msghdr *msg, unsigned int flags);

Recvmsg () is used to receive data from the specified socket of the remote host.

Return value: the number of characters received is returned on success, -1 on failure, and the reason for the error is in errno.

Error code:
    EBADF parameter s invalid socket handling code.
    The EFAULT parameter has a pointer to an inaccessible memory space
    ENOTSOCK parameter s is a file descriptor, not a socket.
    EINTR is interrupted by the signal.
    EAGAIN this action will block the process, but the socket of parameter s is not blocked.
    The ENOBUFS system is out of buffer memory
    ENOMEM is out of core memory
    EINVAL passed an incorrect parameter to the system call.


Related articles: