C Language SendTo () function-transmit data via socket and recvfrom function "Turn"

Source: Internet
Author: User
Tags htons

Related functions: Send, Sendmsg, recv, recvfrom, socket
Header files: #include <sys/types.h> #include <sys/socket.h>

Define functions: 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 wired, and if the UDP protocol is used, it does not need to be connected. Parameter msg points to the data content to be connected, the parameter flags general set 0, detailed description please refer to send (). The parameter to is used to specify the network address to be transmitted, and the structure sockaddr refer to bind (). The parameter tolen is the result length of sockaddr.

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

Error code:
1, 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 file description word, non-socket.
4, Eintr was interrupted by the signal.
5, Eagain This action will cause the process to block, but the soket of the parameter S for the missed lesson block.
6, ENOBUFS system buffer memory is insufficient.
7, EINVAL passed to the system call parameter is not correct.

Example
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet.in.h>
#include <arpa.inet.h>
#define PORT 2345/* Used by Port */
Main ()
{
int SOCKFD, Len;
struct sockaddr_in addr;
Char buffer[256];
Creating sockets
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%s\n", Inet_ntoa (ADDR.SIN_ADDR));
Return a string to client side
SendTo (sockfd, buffer, len, 0, &addr, Addr_len);
}
}
Execution: Please refer to Recvfrom ()
/*利用socket的UDPclient 此程序会连线UDPserver,并将键盘输入的字符串传给server。 UDPserver范例请参考sendto()。 */ #include<sys/stat.h> #include<fcntl.h> #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #definePORT2345 #defineSERVER_IP“127.0.0.1” main() { ints,len; structsockaddr_inaddr; intaddr_len= sizeof (structsockaddr_in); charbuffer[256]; /*建立socket*/ if ((s=socket(AF_INET,SOCK_DGRAM,0))<0){ perror (“socket”); exit (1); } /*填写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)); /*从标准输入设备取得字符串*/ len=read(STDIN_FILENO,buffer, sizeof (buffer)); /*将字符串传送给server端*/ sendto(s,buffer,len,0,(structsockaddr*)&addr,addr_len); /*接收server端返回的字符串*/ len=recvfrom(s,buffer, sizeof (buffer),0,(structsockaddr*)&addr,(socklen_t*) &addr_len); printf (“receive:%s”,buffer); } } 执行(先执行udpserver再执行udpclient) hello /*从键盘输入字符串*/ receive:hello /*server端返回来的字符串*/

C Language SendTo () function-transmit data via socket and recvfrom function "Turn"

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.