Use of the send of and sendto of functions in C language

  • 2020-04-02 03:20:59
  • OfStack

C language send() function: data through the socket
The header file:


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

Definition function:


int send(int s, const void * msg, int len, unsigned int falgs);

Send () is used to send data from the specified socket to the host.
    The data transferred by MSG_OOB is sent out as out-of-band.
    MSG_DONTROUTE cancels the routing table query
    MSG_DONTWAIT is set to be non-interruptible
    MSG_NOSIGNAL this action does not want to be interrupted by the SIGPIPE signal.

Return value: returns the number of characters actually sent on success, and returns -1 on failure. 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.


C language sendto() function: transfer data through a socket
The header file:


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

Int sendto(int s, const void * MSG, int len, unsigned int flags, const struct sockaddr * to, int tolen);

Function description: sendto () is used to host the data from the specified socket to each other. Have been built for parameter s connection socket, if does not have to go through a connection operation. Use the UDP protocol is parameter MSG pointing to the attachment of the data content, general set 0 parameter flags, details please refer to the send (). The parameters to the network address of the transfer is used to specify the desire, sockaddr structure please refer to the bind (). The parameter tolen for the length of the sockaddr results.

Return value: returns the number of characters actually sent on success, returns -1 on failure, and the error is in errno.

Error code:
1. Illegal socket handling code for EBADF parameter s.
2. The EFAULT parameter has a pointer to an inaccessible memory space.
3. WNOTSOCK canshu s is a file descriptor, not a socket.
4. EINTR is interrupted by the signal.
5, EAGAIN this action will block the process, but the soket of parameter s is blocked by make-up lesson.
6. The buffer memory of ENOBUFS system is insufficient.
7. The parameters EINVAL passed to the system call are incorrect.

sample


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet.in.h>
#include <arpa.inet.h>
#define PORT 2345 
main()
{
  int sockfd, len;
  struct sockaddr_in addr;
  char buffer[256];
  //To establish a socket
  if(sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  {
    perror ("socket");
    exit(1);
  }
  //Fill in the sockaddr_in structure
  bzero(&addr, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(PORT);
  addr.sin_addr = hton1(INADDR_ANY);
  if(bind(sockfd, &addr, sizeof(addr)) < 0)
  {
    perror("connect");
    exit(1);
  }
  while(1)
  {
    bezro(buffer, sizeof(buffer));
    len = recvfrom(socket, buffer, sizeof(buffer), 0, &addr &addr_len);
    //Displays the network address of the client side
    printf("receive from %sn ", inet_ntoa(addr.sin_addr));
    //Returns the string to the client side
    sendto(sockfd, buffer, len, 0, &addr, addr_len);
  }
}


 


Related articles: