日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1Boost之TCP,Client and Server

發布時間:2024/9/27 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1Boost之TCP,Client and Server 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


客戶端代碼

#include <boost/asio.hpp>

#include <iostream>

#include <stdlib.h>

?

using namespace std;

using namespace boost::asio;

?

void main()

{

??? io_service iosev;

??? ip::tcp::socket mysorket(iosev);

??? ip::tcp::endpoint ep(ip::address_v4::from_string("127.0.0.1"), 1100);

?

??? boost::system::error_code ec;

??? mysorket.connect(ep, ec);//鏈接

?

??? while (1)

??? {

??????? char str[1024] = { 0 };

??????? cout << "請輸入";

??????? cin >> str;

??????? cout << endl;

??????? mysorket.write_some(buffer(str), ec);

??????? memset(str, 0, 1024);//清空字符串

??????? mysorket.read_some(buffer(str), ec);

??????? cout << "收到" << str << endl;

??? }

?

??? cin.get();

}

客戶端代碼:

服務器端代碼:

#include <boost/asio.hpp>

#include <iostream>

#include <stdlib.h>

?

using namespace std;

using namespace boost::asio;

?

void main()

{

??? io_service iosev;

??? //這里使用ip v4協議,端口號是1100

??? ip::tcp::acceptor myacceptor(iosev, ip::tcp::endpoint(ip::tcp::v4(), 1100));

??? while (1)

??? {

??????? //構建TCP

??????? ip::tcp::socket mysocket(iosev);

??????? //接受套接字

??????? myacceptor.accept(mysocket);

??????? cout << "客戶端" << mysocket.remote_endpoint().address() << ":"

??????????? << mysocket.remote_endpoint().port() << "鏈接上" << endl;

?

??????? char recestr[1024] = { 0 };

??????? boost::system::error_code ec;

??????? //處理網絡異常

??????? int length = mysocket.read_some(buffer(recestr), ec);

??????? cout << "收到" << recestr << "長度" << length << endl;

??????? system(recestr);

??????? length = mysocket.write_some(buffer(recestr, length), ec);

??????? cout << "發送報文長度" << length << endl;

??? }

??? cin.get();

}

運行結果:

?

總結

以上是生活随笔為你收集整理的1Boost之TCP,Client and Server的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。