UDP Communication Programming---6

Source: Internet
Author: User
Tags htons

First, the function of

1.1 Functions used by the server

Create socket----->socket

Bind address-------->bind

Accept Data-------->recvfrom

Send Data-------->sendto

End Connection-------->close

1.2 Client Use functions

Create socket------>socket

Send Data-------->sento

Accept Data-------->recvfrom

End Connection-------->close

Second, function learning

2.1 Sending Data SendTo

2.1.1 Contains header files

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

2.1.2 Function prototypes

ssize_t sendto (int sockfd, const void *buf, size_t len, int flags,
const struct SOCKADDR *dest_addr, socklen_t Addrlen);

2.1.3 Function parameters

SOCKFD: Socket created

*BUF: The data address to send

Len: Data length

Flags: Identifiers

DEST_ADDR: Destination Machine Address

Addrlen: Address length

2.1.4 return value

The amount of data actually sent

2.2 Receiving Data Recvfrom

2.2.1 Contains header files

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

2.2.2 Function prototypes

ssize_t recvfrom (int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);

2.2.3 Function parameters

SOCKFD: Socket created

*BUF: Address of data storage

Len: Data length

Flags: Identifiers

DEST_ADDR: The address of the data source party

Addrlen: pointer to address length

2.2.4 return value

The length of the message in bytes, or 0 if no message is available or the other party has been ordered, or 1 if an error occurs.

Third, write the server code

Touch udp_server.c

chmod 777 UDP_SERVER.C

1 #include <stdio.h>2 #include <String.h>3 #include <sys/socket.h>4 #include <netinet/In.h>567#define PORTNUM 88888#define MSG_SIZE 1289IntMain ()10{11IntSOCKFD;12CharBuffer[msg_size];13structSockaddr_in server_addr;14structSockaddr_in client_addr;15IntAddrlen;16IntN17//1. Creating socketsSOCKFD = socket (Af_inet,sock_dgram,0);1920//2. Binding AddressBzero (&AMP;SERVER_ADDR,sizeofstructsockaddr_in));server_addr.sin_family =Af_inet;Server_addr.sin_port =Htons (Portnum);SERVER_ADDR.SIN_ADDR.S_ADDR =Htonl (Inaddr_any);Bind (SOCKFD, (struct sockaddr *) (&AMP;SERVER_ADDR),sizeofstructSOCKADDR));2627//3. Acceptance of data28While1)29{Addrlen =sizeofstruct sockaddr);//Get Data length AddressBzero (Buffer,sizeof(buffer));n = recvfrom (sockfd,buffer,msg_size,0, (struct sockaddr *) (&CLIENT_ADDR), &addrlen); 0;34 printf ( "server Received:%s\n ",buffer); 35 }36 //4. End connection 37  close ( SOCKFD); 38 return 0; 39}             

Compile run:

Iv. Writing client code

Touch Udp_client,c

chmod 777 Udp_client.c

#include <stdio.h>#include <String.h>#include <sys/socket.h>#include <netinet/In.h>#define BUFFER_SIZE 128#define PORTNUM 8888int main (int argc,char * *Argv[]) {IntSOCKFD;CharBuffer[buffer_size];structSockaddr_in server_addr;if (argc! =2) {printf ("Usage:%s server_ip\n", argv[0]); Exit1); }//1. Create Socket SOCKFD = socket (Af_inet,sock_dgram,0);//Initialize server address bzero (&AMP;SERVER_ADDR,sizeofstructsockaddr_in)); server_addr.sin_family =Af_inet; Server_addr.sin_port =Htons (Portnum); Inet_aton (argv[1],&SERVER_ADDR.SIN_ADDR);//while (1) {printf (please Input char: \n "); Fgets (Buffer,buffer_size,stdin); SendTo (Sockfd,buffer,strlen (buffer), 0, (struct SOCKADDR *) (&SERVER_ADDR), sizeof (struct sockaddr)); Bzero (buffer,buffer_size); } //3. Close connection  close ( SOCKFD); return 0   

Compile run:

4.1 You can see the server running results

UDP Communication Programming---6

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.