UDP for socket programming of computer network

Source: Internet
Author: User
Tags htons

>UDP is a non-connection-oriented unreliable network transport protocol

Most of the UDP protocol is similar to the TCP protocol, except that his client program does not need to connect, but sends the data directly.

>sendto () and recvfrom () functions

With these two functions, the data is transferred on a network that has no connection established. Can be used in the case of non-connected UDP traffic. Because the datagram socket cannot connect to the remote host, think about what we need to know before sending the data. is the IP address and port of the remote host!

The following is the declaration of the SendTo () function and the Recvfrom () function:

#include <sys/types.h>

#include <sys/socket.h>

int sendto (int sockfd, const void *msg, int len, unsigned int flags,const struct sockaddr *to, int tolen);

As you can see, this function is basically the same as the Send () function.

SOCKFD is a socket descriptor that represents your connection to a remote program.

MSG is a pointer to the address of the message you want to send.

Len is the length of the message you want to send.

Flags sends tokens. is generally set to 0. (You can view the man pages of Send to get additional parameter values and understand what each parameter means)

To is a pointer to a struct SOCKADDR structure that contains the IP address and port data for the remote host.

Tolen just indicates the size of the struct sockaddr in memory sizeof (struct sockaddr). As with Send (), sendto () returns the number of bytes it really sends (and, of course, the number of bytes that it actually sends, like send (), which may be less than the number of bytes you give it. When the error occurs, it also returns –1, and the global variable errno stores the error code. Similarly, the recv () function and the recvfrom () function are basically identical.

The declaration of Recvfrom () is:

#include <sys/types.h>

#include <sys/socket.h>

int recvfrom (int sockfd, void *buf, int len, unsigned int flags

struct sockaddr *from, int *fromlen);

The parameters have the following meanings:

SOCKFD is the socket descriptor that you want to read data from.

BUF is a pointer to the memory cache area where you can store data.

Len is the maximum size of the buffer.

The flags are a sign of the recv () function, which is generally 0 (refer to the man pages of Recv () for the other values and meanings specified.

The from is a local pointer to the structure of a struct sockaddr (which contains the source IP address and number of ports).

Fromlen is a pointer to an int data, and its size should be sizeof (STRUCTSOCKADDR). When the function returns, the data that Formlen points to is the actual size of the struct sockaddr that the form points to.

Recvfrom () returns the number of bytes it receives, and if an error occurs, it returns-1

> Specific Procedures


Server.c

1 #include <stdio.h>

2 #include <sys/types.h>

3 #include <sys/socket.h>

4 #include <stdlib.h>

5 #include <unistd.h>

6 #include <netinet/in.h>

7 #include <arpa/inet.h>

8 #include <string.h>

9 static void usage (const char* proc)

10 {

printf ("usage:%s [IP] [port]\n", proc);

12}

int main (int argc,char* argv[])

14 {

if (argc!=3)

16 {

Usage (argv[0]);

Exit (1);

19}

Sock=socket Int (af_inet,sock_dgram,0);

if (sock<0)

22 {

Perror ("sock");

Exit (2);

25}

sockaddr_in-struct-local;

Local.sin_family=af_inet;

LOCAL.SIN_ADDR.S_ADDR=INET_ADDR (argv[1]);

Local.sin_port=htons (Atoi (argv[2]));

30

if (Bind (sock, (struct sockaddr*) &local,sizeof (local)) <0)

32 {

Perror ("bind");

return 3;

35}

done=0 int;

Sockaddr_in Peer of the Notoginseng struct;

Socklen_t len=sizeof (peer);

(buf[1024 char);

40

(!done)

42 {

memset (buf, ' n ', sizeof (BUF));

Recvfrom (Sock,buf,sizeof (BUF), 0, (struct sockaddr*) &peer,&len);

printf ("#####################################\n");

printf ("Get a client,socket->%s:%d\n", Inet_ntoa (PEER.SIN_ADDR), Ntohs (Peer.sin_port));

printf ("client#:%s echo client!\n", buf);

printf ("#####################################\n");

SendTo (Sock,buf,sizeof (BUF), 0, (struct sockaddr*) &peer,len);

50}

Wuyi return 0;

52}



Client.c


1 #include <stdio.h>

2 #include <stdlib.h>

3 #include <sys/types.h>

4 #include <sys/socket.h>

5 #include <netinet/in.h>

6 #include <arpa/inet.h>

7 #include <string.h>

8 static void Usage (const char* proc)

9 {

Ten printf ("Usage:%s[remote_ip" [remote_port]\n], proc);

11}

int. int main (int argc,char* argv[])

13 {

if (argc!=3)

15 {

Usage (argv[0]);

1;

18}

19

Sock=socket Int (af_inet,sock_dgram,0);

if (sock<0)

22 {

Perror ("socket");

return 2;

25}

26

the struct sockaddr_in remote;

Remote.sin_family=af_inet;

REMOTE.SIN_ADDR.S_ADDR=INET_ADDR (argv[1]);

Remote.sin_port=htons (Atoi (argv[2]));

31

done=0 int;

(buf[1024 char);

Sockaddr_in peer of the struct;

Socklen_t len=sizeof (peer);

36

Panax Notoginseng while (!done)

38 {

("Please Enter:");

Fflush (stdout);

ssize_t _s=read (0,buf,sizeof (BUF)-1);

if (_s>0)

43 {

buf[_s]= ' + ';

SendTo (Sock,buf,sizeof (BUF), 0, (struct sockaddr*) &remote,sizeof (R emote));

memset (buf, ' n ', sizeof (BUF));

Recvfrom (Sock,buf,sizeof (BUF), 0, (struct sockaddr*) &peer,&len);

-printf ("Server echo%s,socket>%s:%d\n", Buf,inet_ntoa (Peer.sin_ad Dr), Ntohs (Peer.sin_port));

49}

50}

Wuyi return 0;

52}

Operation Result:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/85/86/wKiom1enIo6DN39iAADwVIgjd2M973.png-wh_500x0-wm_3 -wmp_4-s_1297144445.png "title=" 7.png "alt=" Wkiom1enio6dn39iaadwvigjd2m973.png-wh_50 "/>


This article is from the "11275984" blog, please be sure to keep this source http://11285984.blog.51cto.com/11275984/1835425

UDP for socket programming of computer network

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.