Boost library in work (37) network UDP Server 7

Source: Internet
Author: User

we have discussed TCP servers and clients in the network. In fact, UDP servers and clients are also divided into synchronous and asynchronous servers. The biggest difference between UDP and TCP is that TCP is based on connections, while UDP is connectionless. The so-called connection means that when the other party interrupts the service, the other party can perceive it, while UDP cannot determine whether the other party is interrupted. Another point is that the packets sent by TCP have traffic control and order control, while UDP does not have traffic control and order control. Therefore, when UDP is used, it is basically used to send irrelevant sequence or when it is lost. For example, UDP is widely used in instant voice communication and video communication. During speech communication, it is meaningless to play out outdated speech that has been lost. In addition, because different customers have different bandwidths and processing speeds in video conferences, Video Synchronization for different customers also discards data processing through slow customers, otherwise, customers with slow speeds will accumulate more and more video data, so that new videos are invisible. Next we will use the boost library to implement a UDP server, and it is a synchronization-based server. Code :

// Boost_026.cpp: defines the entry point for the console application. // # include "stdafx. H "# include <ctime> # include <boost/ASIO/IP/TCP. HPP> # include <boost/ASIO. HPP> # include <boost/bind. HPP> # include <boost/enable_shared_from_this.hpp> # include <boost/shared_ptr.hpp> # include <boost/array. HPP> # include <iostream> # include <string> // converts the current time to a string. STD: String make_daytime_string () {using namespace STD; // For time_t, time and ctime; time_t now = time (0); Return ctime (& now );} //// create a UDP time server. // Software developer: Cai junsheng // QQ: 9073204 // void testudp (void) {// define an I/O service. Boost: ASIO: io_service; // create a UDP socket. The protocol is V4 and the port is 13. boost: ASIO: IP: UDP: Socket socket (io_service, boost: ASIO: IP: UDP: endpoint (boost: ASIO: IP :: UDP: V4 (), 13); // processes the commands sent from all clients cyclically. For (;) {boost: array <char, 1> recv_buf; Boost: ASIO: IP: UDP: endpoint remote_endpoint; Boost: System :: error_code error; // receives client data. Socket. receive_from (boost: ASIO: buffer (recv_buf), remote_endpoint, 0, error); // if an error occurs, an exception is thrown. If (error & error! = Boost: ASIO: Error: message_size) {Throw boost: System: system_error (error) ;}// gets the current time string. STD: String message = make_daytime_string (); Boost: System: error_code ignored_error; // send the time string to the client. Socket. send_to (boost: ASIO: buffer (Message), remote_endpoint, 0, ignored_error) ;}} int _ tmain (INT argc, _ tchar * argv []) {// testudp (); Return 0 ;}

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.