可以先了解一下Boost asio基本概念,以下是Boost asio实现的同步TCP/IP通信:

  服务器程序部分,如果想保留套接字之后继续通信,可以动态申请socket_type,保存指针,因为socket_type貌似不能拷贝:

#include "stdafx.h"
#include <iostream>
#include <boost/asio.hpp> using namespace boost::asio;
using namespace std; int main()
{
try
{
typedef ip::tcp::acceptor acceptor_type;
typedef ip::tcp::endpoint endpoint_type;
typedef ip::tcp::socket socket_type; std::cout<<"Server start."<<endl;
io_service io;
acceptor_type acceptor(io, endpoint_type(ip::tcp::v4(), ));
std::cout<<acceptor.local_endpoint().address()<<endl; for (;;)
{
socket_type sock(io);
acceptor.accept(sock); std::cout<<"Client";
std::cout<<sock.remote_endpoint().address()<<endl;
sock.send(buffer("Hello asio"));
}
}
catch (std::exception &e)
{
std::cout<<e.what()<<endl;
} return ;
}

  客户端:

#include "stdafx.h"
#include <iostream>
#include <boost/asio.hpp> using namespace boost::asio;
using namespace std; int main()
{
try
{
typedef ip::tcp::acceptor acceptor_type;
typedef ip::tcp::endpoint endpoint_type;
typedef ip::tcp::socket socket_type;
typedef ip::address address_type; std::cout<<"Client start."<<endl;
io_service io;
socket_type sock(io);
endpoint_type ep(address_type::from_string("127.0.0.1"), ); sock.connect(ep); vector<char> str(, );
boost::system::error_code ec;
for (;;)//循环接收
{
sock.read_some(buffer(str), ec);
if (ec)
{
break;
}
cout<<&str[];
}
// 析构自动断开连接
}
catch (std::exception &e)
{
std::cout<<e.what()<<endl;
}
return ;
}

使用Boost asio实现同步的TCP/IP通信的更多相关文章

  1. 使用Boost asio实现异步的TCP/IP通信

    可以先了解一下Boost asio基本概念,以下是Boost asio实现的异步TCP/IP通信: 服务器: #include "stdafx.h" #include <io ...

  2. boost::asio设置同步连接超时

    boost::asio设置同步连接超时   CSDN上求助无果,只好用自创的非主流方法了.asio自带的例子里是用deadline_timer的async_wait方法来实现超时的,这种方法需要单独写 ...

  3. TCP/IP 通信

    TCP/IP 通信又叫socket 通信,是基于TCP/IP协调面向连接的一个数据传输技术.是属于OSI国际标准的传输层,三次握手 提供数据,有序,安全,端到端的传输和接收.它有三个主要协议:传输控制 ...

  4. tcp/ip通信第5期之服务器端程序

    /* 此程序是tcp/ip通信服务器端程序,测试运行在redhat5上 重构readline函数,解决粘包问题——利用“\n”识别一个消息边界 */ #include<stdio.h> # ...

  5. tcp/ip通信第5期之客户机端程序

    /*此程序是tcp/ip通信的客户机端程序, 测试运行在redhat6系统上 重构readline函数,解决粘包问题——利用“\n”识别一个消息边界 */ #include<stdio.h> ...

  6. 第4章 TCP/IP通信案例:访问Internet上的Web服务器

    第4章 TCP/IP通信案例:访问Internet上的Web服务器 4.2 部署代理服务器 书中为了演示访问Internet上的Web服务器的全过程,使用了squid代理服务器程序模拟了一个代理服务器 ...

  7. TCP/IP通信过程(以发送电子邮件为例)(转)

    1.应用程序处理 (1)A用户启动邮件应用程序,填写收件人邮箱和发送内容,点击“发送”,开始TCP/IP通信: (2)应用程序对发送的内容进行编码处理,这一过程相当于OSI的表示层功能: (3)由A用 ...

  8. linux高性能服务器编程 (四) --TCP/IP通信案例

    第四章 TCP/IP通信案例 HTTP代理服务器的大致工作原理        在HTTP通信链上,客户端和服务器之间通常存在某些中转代理服务器.它们提供对目标资源的中转访问.一个HTTP请求可能被多个 ...

  9. TCP/IP通信网络基础

    TCP/IP是互联网相关的各类协议族的总称. TCP/IP的分层管理 分层的优点:如果只有一个协议在互联网上统筹,某个地方修改就要把所有的部分整体换掉,采用分层则只需要改变相应的层.把各个接口部分规划 ...

随机推荐

  1. How to log in to Amazon EC2 using PEM format from SecureCRT

    SecureCRT requires both a private and a public key. Use the supplied key.pem file from EC2 here as y ...

  2. atprogram.exe : Atmel Studio Command Line Interface

    C:\Program Files\Atmel\Atmel Studio 6.1\atbackend\atprogram.exe No command specified.Atmel Studio Co ...

  3. MON166 User's Guide

    MON166 is a debug monitor for C16x and ST10 user programs. It consists of: A configurable monitor pr ...

  4. 新唐M0 ISP下载要点

    http://blog.csdn.net/rejoice818/article/details/7736029 一.注意:官方光盘内“Software Utilities”目录下,可找到ICP或ISP ...

  5. 内存不足(OutOfMemory)的调试分析

    32位操作系统的寻址空间是4G,其中有2G被操作系统占用,也就是说留给用户进程的内存只有2G(其中还要扣除程序加载时映像占用的部分空间,一般只有1.6G~1.8G左右可以使用). 如果进程运行中需要申 ...

  6. NFS错误Starting NFS quotas: Cannot register service: RPC: Unable to receive; errno=Connection refused

    NFS报错一例 [root@bjs0- ~]# /etc/init.d/portreserve start Starting portreserve:                          ...

  7. java android ExecutorService 线程池解析

    ExecutorService: 它也是一个接口,它扩展自Executor接口,Executor接口更像一个抽象的命令模式,仅有一个方法:execute(runnable);Executor接口简单, ...

  8. 【转】工科男IT职场求生法则

    转自:http://www.36dsj.com/archives/3459 我在IT职场打滚超过10年了,从小小的程序员做到常务副总.相对于其它行业,IT职场应该算比较光明的了,但也陷阱重重,本文说说 ...

  9. 分享:Android中利用机器码注册机制防止破解(转)

    转自:http://blog.csdn.net/huzgd/article/details/6684094 最近做一个Android应用时遇到这个问题,客户要求功能必须注册才能使用,而程序本身又不是联 ...

  10. TP复习12

    四.特殊标签 1.比较标签 eq或者 equal 等于 neq 或者notequal 不等于 gt 大于 egt 大于等于 lt 小于 elt 小于等于 heq 恒等于 nheq 不恒等于 2.范围标 ...