UDP boost: Implementation of ASIO

Source: Internet
Author: User

Recently, I have been studying network-related things and found that my previous understanding of UDP is weak. I am too dependent on TCP and even forget the existence of another UDP. As a result, I randomly searched the UDP socket programming code and materials on the Internet, and found that there was still a connect in the programming example written by someone. I was speechless.

Compared with TCP, UDP is an unreliable transmission protocol. When the network environment is poor, using TCP is undoubtedly the only choice. When the network environment is good, for example, UDP is undoubtedly the best choice for message transmission within the LAN and communication between processes. UDP not only has great advantages in transmission efficiency, I think the bigger advantage is that it does not need to maintain connections and can reduce a lot of logical redundancy.

Next, let's take a look at a piece of code for simple UDP communication.

The server code implements the echo function:

/** @ File udpechoserver. CPP * @ note Hangzhou hikvision System Technology Co ., ltd. all rights reserved. * @ brief an UDP server, echo what the client say. ** @ author Zou tuoyu * @ date 2012/11/28 ** @ note history: * @ note v1.0.0.0 creation * // boost # include "Boost/thread. HPP "# include" Boost/ASIO. HPP "// STL # include <string> # include <iostream> using namespace STD; int main () {boost: ASIO: io_service; Boost: ASIO :: IP: UDP: Socket udp_socket (io_service); Boost: ASIO: IP: UDP: endpoint local_add (boost: ASIO: IP: Address :: from_string ("10.64.49.70"), 7474); udp_socket.open (local_add.protocol (); udp_socket.bind (local_add); char receive_buffer [1024] = {0}; while (true) {boost :: ASIO: IP: UDP: endpoint send_point; udp_socket.receive_from (boost: ASIO: buffer (receive_buffer, 1024), send_point); cout <"Recv: "<receive_buffer <Endl; udp_socket.send_to (boost: ASIO: buffer (receive_buffer), send_point); memset (receive_buffer, 0, 1024);} return 1 ;}


Client, input

//boost#include "boost/asio.hpp"//stl#include <iostream>using namespace std;int main(){boost::asio::io_service io_service;boost::asio::ip::udp::socket socket(io_service);boost::asio::ip::udp::endpoint end_point(boost::asio::ip::address::from_string("10.64.49.70"), 7474);socket.open(end_point.protocol());char receive_buffer[1024] = {0};while (true){cout << "input:";string input_data;cin >> input_data;cout << endl;try{socket.send_to(boost::asio::buffer(input_data.c_str(), input_data.size()), end_point);socket.receive_from(boost::asio::buffer(receive_buffer, 1024), end_point);cout << "recv:" << receive_buffer << endl;}catch (boost::system::system_error &e){cout << "process failed:" << e.what() << endl;}}}



I have a superficial understanding of UDP. I am not very grateful for any leaks.

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.