UDP-based Winsock programming (C ++)

Source: Internet
Author: User
Tags htons

UDP-based Winsock programming (C ++)

Compared with TCP-based Winsock programming, UDP-based Winsock programming lacks only one step. The Server is missingAccept connection(Accept () function call); for the Client,Request connection(Connect () function call ).

In addition, the difference is that in UDP, the data sending and receiving functions are sendto () and recvfrom.
Function prototype:

Int sendto (SOCKET s, const char FAR * buf, int len, int flags, const struct sockaddr FAR * to, int tolen );
Int recvfrom (SOCKET s, char FAR * buf, int len, int flags, struct sockaddr FAR * to, int FAR * fromlen );

Parameter flagsGenerally, it is set to 0;
ParameterIt is the target address structure information;
Parameter tolenThe size of the target address structure;

Let alone code!

Code:

Server

/* UDPServer. cpp */# include
  
   
# Include
   
    
# Include
    
     
# Pragma comment (lib, "WS2_32.lib") int main () {// initialize the socket library WSADATA data; // define the struct variable WORD w = MAKEWORD (2, 0 ); // define the socket version: WSAStartup (w, & data); // initialize the socket library char sztext [] = "Welcome \ r \ n "; // create SOCKET handle socket s; s =: SOCKET (AF_INET, SOCK_DGRAM, 0); // create UDP socket // address structure settings and byte conversion sockaddr_in addr and addr2; // create the socket address Structure Variable int n = sizeof (addr2); // The address structure variable size char buff [11] = {0}; // receives the data buffer addr. sin_family = AF_INET; addr. sin_port = htons (75); addr. sin_ad Dr. s_un.S_addr = INADDR_ANY; // bind socket: bind (s, (sockaddr *) & addr, sizeof (addr )); printf ("UDP server started \ r \ n"); // The while (true) Prompt {// receive data from the client if (: recvfrom (s, buff, 10, 0, (sockaddr *) & addr2, & n )! = 0) {printf ("% s connected \ r \ n", inet_ntoa (addr2.sin _ addr); printf ("% s \ r \ n", buff ); // send data to the client: sendto (s, sztext, sizeof (sztext), 0, (sockaddr *) & addr2, n); break ;}// close the socket:: closesocket (s); // close the socket Library: WSACleanup (); if (getchar () {return 0;} else {: Sleep (100 );}}
    
   
  

Client

/* UDPClient. cpp */# include
  
   
# Include
   
    
# Include
    
     
# Pragma comment (lib, "WS2_32.lib") // connect the socket library int main () {// initialize the socket library WSADATA data; // parameter 1 WORD w = MAKEWORD (2, 0 ); // parameter 2, indicating that the socket version is 2.0: WSAStartup (w, & data); char sztext [] = "Hello, server! \ R \ n "; // create SOCKET handle socket s; s =: SOCKET (AF_INET, SOCK_DGRAM, 0 ); // create a UDP socket // address structure setting and byte conversion sockaddr_in addr, addr2; int n = sizeof (addr2); char buff [10] = {0}; addr. sin_family = AF_INET; addr. sin_port = htons (75); addr. sin_addr.S_un.S_addr = inet_addr ("127.0.0.1"); printf ("UDP client started \ r \ n"); // send information to the server if (: sendto (s, sztext, sizeof (sztext), 0, (sockaddr *) & addr, n )! = 0) {// receiving information from the slave server: recvfrom (s, buff, 10, 0, (sockaddr *) & addr2, & n); printf ("the server says: % s \ r \ n ", buff); // close socket: closesocket (s); // close socket Library: WSACleanup ();} if (getchar ()) {return 0;} else {: Sleep (100 );}}
    
   
  

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.