How to use the Send () function and sendto () function in C language _c language

Source: Internet
Author: User
Tags socket

C-language Send () function: data transfer via socket
header file:

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

To define a function:

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

Function Description: Send () is used to pass data from the specified socket to the other host. The parameter s is a socket that has been established for the connection. The parameter msg points to the content of the data to be wired, and the parameter len is the length of the data. Parameter flags are generally set to 0, and other values are defined as follows:
The data transmitted by Msg_oob is sent out Out-of-band.
Msg_dontroute Cancel Routing Table query
Msg_dontwait is set to not block operation
Msg_nosignal This action does not want to be interrupted by a sigpipe signal.

Return value: Success returns the number of characters actually sent out, and the failure returns-1. The reason for the error exists 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 operation 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.


C language SendTo () function: data transfer via socket
header file:

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

Definition function: int sendto (int s, const void * msg, int len, unsigned int flags, const struct SOCKADDR * to, int tolen);

Function Description: SendTo () is used to pass data from the specified socket to the other host. The parameter s is a socket that has been built, and if the UDP protocol is used, it does not need to be wired. Parameter msg points to the data content to be connected, parameter flags general set 0, detailed description please refer to send (). parameter to specifies the network address to be routed, structure sockaddr refer to bind (). The parameter tolen is the result length of the sockaddr.

Return value: Success returns the number of characters actually transmitted, failure returns 1, and the reason for the error is in errno.

Error code:
1, the EBADF parameter s illegal socket processing code.
2, the Efault parameter has a pointer to the memory space that cannot be accessed.
3, Wnotsock Canshu S is a document description word, not socket.
4, Eintr was interrupted by the signal.
5, Eagain This action will block the process, but the parameter S Soket for the missed lesson block.
6, ENOBUFS system buffer memory is insufficient.
7, Einval passed to the system call is not correct parameters.

Example

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet.in.h>
#include < arpa.inet.h>
#define PORT 2345/* Use port *
/main ()
{
  int sockfd, Len;
  struct sockaddr_in addr;
  Char buffer[256];
  Create
  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 client-side network address
    printf ("Receive from%s\n", Inet_ntoa (ADDR.SIN_ADDR));
    Returns the string to the client side
    sendto (sockfd, buffer, len, 0, &addr, Addr_len);
  }


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.