The correlation function of receiving data via socket in C language _c language

Source: Internet
Author: User

Recv () function:
header file:

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

To define a function:

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

Function Description: recv () is used to receive data from a specified socket on the remote host and to save 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.

Parameter flags are generally set to 0. Other values are defined as follows:
1, Msg_oob receive the data sent out by Out-of-band.
2, Msg_peek returned data will not be deleted in the system, if the call recv () will return the same data content.
3, Msg_waitall forced to receive Len size of data to return, unless there is an error or signal generation.
4, Msg_nosignal This operation is not willing to be sigpipe signal interrupt return value succeeds returns the number of characters received, failure returns-1, the reason for the error is in errno.

Error code:
EBADF parameter S is not a valid socket processing code
A pointer in the Efault parameter points to an inaccessible memory space
Enotsock parameter S is a file descriptor, not a socket.
Eintr was interrupted by the signal.
Eagain This action will block the process, but the socket for parameter S is not blocked
The ENOBUFS system has insufficient buffer memory.
Enomem Core memory is low
Einval the parameter passed to the system call is incorrect.

Recvfrom () function:
header file:

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

To define a function:

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

Function Description: recv () is used to receive data from a specified socket from a remote host and to save the data to the memory space pointed to by the parameter buf, which is the maximum length of the data that can be received by the parameter Len. Parameter flags generally set 0, other numerical definitions please refer to recv (). Parameter from is used to specify the network address you want to transfer, structure sockaddr refer to bind (). The parameter fromlen is the structure length of the sockaddr.

Return value: Returns the number of characters received, failure returns 1, and causes the error to be stored in errno.

Error code:
EBADF parameter S is not a valid socket processing code
A pointer in the Efault parameter points to an inaccessible memory space.
Enotsock parameter S is a file descriptor, not a socket.
The eintr was interrupted by the signal.
Eagain This action will block the process, but the socket for parameter s is not blocked.
Insufficient buffer memory for ENOBUFS system
Enomem Core memory is low
Einval the parameter passed to the system call is incorrect.

Example:
* * Using the UDP client of the socket this program will connect to the UDP server and pass the keyboard input string to the server.
For a UDP server example, refer to SendTo (). */

 #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];
    Create a socket if ((s = socket (af_inet, SOCK_DGRAM, 0)) < 0) {perror ("socket");
  Exit (1);
  //Fill out 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 the string from the standard input device len = read (Stdin_fileno, buffer, sizeof (buffer));
    Sends the string to server-side sendto (s, buffer, Len, 0, &addr, Addr_len);
    The string returned by the receiving server is Len = recvfrom (s, buffer, sizeof (buffer), 0, &addr, &addr_len);
  printf ("Receive:%s", buffer); }
}

Execute (First execute UDP server and then execute UDP client):

Hello//The string returned from the keyboard input string
Receive:hello the//server end

Recvmsg () function:
header file:

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

To define a function:

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

Function Description: Recvmsg () is used to receive data from a specified socket on a remote host. The parameter s is a socket that has been established, and if the UDP protocol is used, it does not need to be wired. Parameter msg point to the content of the data structure, parameter flags general set 0, detailed description please refer to send (). Refer to Sendmsg () for the definition of structure MSGHDR.

Return value: Returns the number of characters received, failure returns 1, and causes the error to be stored in errno.

Error code:
   EBADF parameter s is not a valid socket handling code.
   efault parameter has a pointer to an inaccessible memory space
   Enotsock parameter S is a file descriptor, not a socket. The
   eintr is interrupted by a signal.
   Eagain This operation blocks the process, but the socket for parameter s is not blocked.
   Enobufs system does not have enough buffered memory
   Enomem core memory is out of control
   Einval passed to system calls incorrectly.

Related Article

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.