[Boost] Write a simple socket communication program with ASIO

Source: Internet
Author: User

boost/asio库中封装了很多关于scoket的函数,当然,asio库还包含很多底层的库。我们可以用socket编写一个基于UDP协议的黑框通讯程序。要想使用asio里面的函数,大多都需要先创建一个io_service对象,然后通过这个serveice来构造不同的对象,所以第一步,我们得创建两个对象:
    boost::asio::io_service io_service;    boost::asio::ip::udp::socket udp_socket(io_service);
这里唯一需要注意的就是命名空间的使用,两处不同。创建了一个基于UDP的socket对象,我们还需要给这个对象绑定IP和端口,因为有了这两个参数,程序才能正确的发送接收信息。
boost::asio::ip::udp::endpoint local_add(boost::asio::ip::address::from_string("192.168.0.141"2000);
我们创建local_add这个对象,这个对象包含了Ip和端口两个参数,Ip地址我填的是局域网的,如果没有的话可以填127.0.0.1的本机默认地址.创建完成之后,我们就需要,给scoket对象绑定local_add对象
    udp_socket.open(local_add.protocol());    udp_socket.bind(local_add);
open 实现了打开这个Ip的端口,这样我们就可以通过这个端口通信,bind实现了这个scoket绑定在这个端口,这个参数是区别服务端和客户端区别的地方,服务端需要绑定这个端口来接收所有发送到这个端口的数据,而客户端只需要发送到这个端口,不需要绑定在这个端口。完成到这里,可以说,一个小型的服务器,就搭建到了本机上,我们还需要完成通讯部分的编写,这里我们就简单的完成,接收一个数据,然后返回这个数据。
Char receive_str[1024x768] = {0}; while(1)    {Boost:: ASIO:: IP:: UDP:: EndpointSendpoint; Udp_socket.receive_from (Boost:: ASIO:: Buffer(Receive_str,1024x768), Sendpoint); cout <<"Received"<< sendpoint.address (). to_string () <<":"<< receive_str << Endl; Udp_socket.send_to (Boost:: ASIO:: Buffer("Service Side return Success"), Sendpoint); memset (RECEIVE_STR,0,1024x768); }    }
sendpoint用来存储收到客户端的ip和端口,receive_from和send_to对应接收这个端口的数据和发送给这个端口。这样的话,服务端的代码全部编写完成,当然光有一个服务端,并没有什么卵用,我们还需要写一个客户端。之前说过,客户端和服务端的区别在于是否绑定Ip和端口,所以,基本上我们只需要把通信部分的操作改成先发信再接收,就可以了,即:
Char receive_str[1024x768] = {0};//String while(1)    {Boost:: ASIO:: IP:: UDP:: EndpointSendpoint;//of the requestIPand port string s;        Cin >> S; Udp_socket.send_to (Boost:: ASIO:: Buffer(S.c_str (), S.size ()), local_add); Udp_socket.receive_from (Boost:: ASIO:: Buffer(Receive_str,1024x768), Local_add);//Collect cout <<"Received"<< receive_str << Endl; memset (RECEIVE_STR,0,1024x768);//Empty string}
完成后我们测试一下:![测试:](http://img.blog.csdn.net/20150827104752519)最后附上两个源码:server:
#include <iostream>using namespace Std;#include <Boost/asio.Hpp>//using namespace Boost::asio;voidMain () {boost:: ASIO:: Io_serviceIo_service; Boost:: ASIO:: IP:: UDP:: SocketUdp_socket (Io_service); Boost:: ASIO:: IP:: UDP:: EndpointLocal_add (boost:: ASIO:: IP:: Address:: from_string("192.168.0.141"), -); Udp_socket.Open (Local_add.Protocol ()); Udp_socket.Bind (Local_add); Char Receive_str[1024x768]= {0};        while (1) {Boost::asio::ip::udp::endpoint sendpoint;        Udp_socket.receive_from (Boost::asio::buffer (Receive_str, 1024x768), sendpoint);        cout << "received" << sendpoint.address (). to_string () << ":" << receive_str << Endl;        Udp_socket.send_to (Boost::asio::buffer ("Server return Success"), Sendpoint);    memset (receive_str, 0, 1024); }}

Client

#include <iostream>using namespace STD;#include <boost/asio.hpp>//using namespace Boost::asio;voidMain () {Boost::asio::io_service io_service;    Boost::asio::ip::udp::socket Udp_socket (Io_service); Boost::asio::ip::udp::endpoint Local_add (boost::asio::ip::address::from_string ("192.168.0.141"), -); Udp_socket.open (Local_add.protocol ());Charreceive_str[1024x768] = {0};//String     while(1) {Boost::asio::ip::udp::endpoint sendpoint;//requested IP and Port        stringSCin>> s;        Udp_socket.send_to (Boost::asio::buffer (S.c_str (), S.size ()), local_add); Udp_socket.receive_from (Boost::asio::buffer (RECEIVE_STR,1024x768), Local_add);//Charge        cout<<"Received"<< receive_str << Endl;memset(Receive_str,0,1024x768);//empty string}}
如果在server端添加system(“str”);的命令行功能,就实现了最简单的远控程序,有兴趣的可以试试。

Need to boost library static library, compilation can not pass the Baidu boost Lib compilation method.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Boost] Write a simple socket communication program with ASIO

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.