UDP-based communication

Source: Internet
Author: User

XX: the drifting 100 ~ _~ {2014/10/03 10: 57}

UDP is a non-connection-oriented sock_dgram service that provides connectionless services. Data packets are sent in the form of independent packets. No guarantee is provided. data can be lost or duplicated.

The difference between UDP server and client is relatively vague. You only need to call sendto and recvfrom to send and receive data to the specified address.

1. server side:

// Udpclient. CPP: defines the entry point for the application. // # include "stdafx. H "# include" tchar. H "# pragma comment (Lib," wsock32.lib ") # include <stdio. h ># include <winsock2.h> bool initwinsock (); // The Winsock service initializes void main () {socket socket1; initwinsock (); struct sockaddr_in local; struct sockaddr_in from; int err; int fromlean = sizeof (from); Local. sin_family = af_inet; local. sin_port = htons (1234); // listener port local. sin_a DDR. s_un.s_addr = inaddr_any; // socket1 = socket (af_inet, sock_dgram, 0); BIND (socket1, (sockaddr *) & Local, sizeof local); While (true) {char buffer [1024] = "\ 0"; printf ("waiting for message from others \ n"); err = recvfrom (socket1, buffer, sizeof (buffer), 0, (sockaddr *) & from, & fromlean); If (Err! = Socket_error) {printf ("received from % s -- % s \ n", inet_ntoa (from. sin_addr), buffer); // send message sendto (socket1, buffer, sizeof buffer, 0, (sockaddr *) & from, fromlean) to the client;} Sleep (500 );} closesocket (socket1);} bool initwinsock () {int error; Word version; wsadata WSA; version = makeword (2, 2); error = wsastartup (version, & WSA ); if (error! = 0) {return false;} else {If (lobyte (WSA. wversion )! = 2 | hibyte (WSA. whighversion )! = 2) {wsacleanup (); Return false ;}} return true ;}

2. Client

// Udpclient. CPP: defines the entry point for the application. // # include "stdafx. H "# include" tchar. H "# pragma comment (Lib," wsock32.lib ") # include <stdio. h ># include <winsock2.h> bool initwinsock (); // The Winsock service initializes void main () {socket socket1; initwinsock (); struct sockaddr_in server; int err; int lean = sizeof (server); server. sin_family = af_inet; server. sin_port = htons (1234); // The listening port of the server is 1234server. sin_addr.s_un.s_ad Dr = inet_addr ("192.168.1.18"); // set it to the local address socket1 = socket (af_inet, sock_dgram, 0); While (true) {char buffer [1024] = "\ 0"; printf ("input message \ n"); scanf ("% s", buffer); If (strcmp (buffer, "bye") = 0) {break;} err = sendto (socket1, buffer, sizeof (buffer), 0, (sockaddr *) & server, lean ); if (Err! = Socket_error) {err = recvfrom (socket1, buffer, sizeof (buffer), 0, (sockaddr *) & server, & lean); If (Err! = Socket_error) {printf ("receive from server: % s \ n", buffer) ;}} closesocket (socket1);} bool initwinsock () {int error; Word version; wsadata WSA; version = makeword (2, 2); error = wsastartup (version, & WSA); If (error! = 0) {return false;} else {If (lobyte (WSA. wversion )! = 2 | hibyte (WSA. whighversion )! = 2) {wsacleanup (); Return false ;}} return true ;}


Start the client and server to view the running effect:



There are still a lot of problems with the program. Please modify it slowly!




 




UDP-based communication

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.