1.代码

#include <iostream>
#include "Poco/Net/Socket.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/NetException.h"
#include "Poco/Timespan.h" using Poco::Net::Socket;
using Poco::Net::StreamSocket;
using Poco::Net::SocketAddress;
using Poco::Net::NetException;
using Poco::Net::ConnectionRefusedException;
using Poco::Net::InvalidSocketException;
using Poco::Timespan;
using Poco::TimeoutException;
using Poco::IOException; const int RECV_BUF_SIZE = 64*1024;
const int SEND_BUF_SIZE = 64*1024; int main(int argc,char * argv[])
{
int n=0;
char buffer[1024]={"\0"};
SocketAddress sa("127.0.0.1",5000);
StreamSocket ss; Timespan timeout(2000000);
try
{
ss.connect(sa,timeout);
}
catch (ConnectionRefusedException&)
{
std::cout<<"connect refuse"<<std::endl;
}
catch (NetException&)
{
std::cout<<"net exception"<<std::endl;
}
catch (TimeoutException&)
{
std::cout<<"connect time out"<<std::endl;
} //setopt timeout
Timespan timeout3(5000000);
ss.setReceiveTimeout(timeout3); //retn void
Timespan timeout4(5000000);
ss.setSendTimeout(timeout4); //retn void
Timespan timeout0 = ss.getReceiveTimeout();
Timespan timeout1 = ss.getSendTimeout();
std::cout<<"Recv Timeout : "<<timeout0.totalMicroseconds()<<std::endl;
std::cout<<"Send Timeout : "<<timeout1.totalMicroseconds()<<std::endl; //setopt bufsize
ss.setReceiveBufferSize(RECV_BUF_SIZE); //retn void
ss.setSendBufferSize(SEND_BUF_SIZE); //retn void
int recv_len=ss.getReceiveBufferSize();
int send_len=ss.getSendBufferSize();
std::cout<<"recv buf size : "<<recv_len<<std::endl;
std::cout<<"send buf size : "<<send_len<<std::endl; //setopt nodelay
ss.setNoDelay(true); //retn void try
{
n = ss.sendBytes("hello", 5); //block
std::cout<<"write length : "<<n<<std::endl;
}
catch (TimeoutException&)
{
std::cout<<"send time out"<<std::endl;
}
catch (InvalidSocketException&)
{
std::cout<<"invalid socket exception"<<std::endl;
}
catch (IOException&)
{
std::cout<<"write io exception"<<std::endl;
} while(1)
{
try
{
if(ss.available())
{
n=0;
memset(buffer,0,sizeof(buffer));
n = ss.receiveBytes(buffer,sizeof(buffer)); //block
std::cout<<"recv length : "<<n<<","<<"value : "<<buffer<<std::endl;
}
}
catch (TimeoutException&)
{
std::cout<<"recv time out"<<std::endl;
}
catch (InvalidSocketException&)
{
std::cout<<"invalid socket exception"<<std::endl;
}
} //Socket::poll有select poll epoll 三种模式,编译Poco库时确定。
/*
Timespan timer(2000000);
Socket::SocketList readList;
Socket::SocketList writeList;
Socket::SocketList exceptList;
readList.push_back(ss);
while(1)
{
if(ss.poll(timer, Socket::SELECT_READ))
{
std::cout<<"he number of bytes available that can be read : "<<ss.available()<<std::endl;
memset(buffer,'\0',sizeof(buffer));
n = ss.receiveBytes(buffer,ss.available());
std::cout<<"recv length : "<<n<<","<<"value : "<<buffer<<std::endl;
}
}
*/ ss.close(); return 0;
}

2.编译指令

g++ myStreamSocket.cpp -o mysocket -lPocoNet -lPocoFoundation
  • 1

3.运行截图

最新文章

  1. .html(),.text()和.val()的差异总结
  2. What is research (1)
  3. Android控件_TextView(显示文本框控件)
  4. poj2378 树形DP
  5. java学习之i/o
  6. ubuntu nsight上链接OpenGL
  7. 自定义Git之配置别名
  8. 使用外置无线网卡来切换mac地址
  9. sql:按年、月、日钻取时间
  10. Spring Boot基础知识
  11. 3.Java的基本数据类型.md
  12. Java_4 引用类型变量 Scanner与Random的使用
  13. Dapper入门使用,代替你的DbSQLhelper
  14. gulp学习。
  15. 小A老师的学习法
  16. json字符串与java对象的相互转换(jackson)
  17. Laravel 5.* 中路由绑定 Controller 包含子目录写法
  18. 火狐浏览器调试ajax异步页面时报错NS_ERROR_UNEXPECTER
  19. 20155310马英林 实验2 Windows口令破解
  20. 第1章-初识Vue.js

热门文章

  1. 4525: [Cerc2012]Kingdoms
  2. springboot相关链接
  3. time模块与random模块,六位含字母随机验证码
  4. CandyCrush 糖果传奇
  5. 求:斐波那契数列的第n项
  6. 孤荷凌寒自学python第二十四天python类中隐藏的私有方法探秘
  7. php学习ing
  8. [译]如何禁止Requests库的log日志信息呢?
  9. 转载:Android SQLite数据库版本升级原理解析
  10. 个人支付宝监控并自动获取交易记录对接系统API