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.运行截图

c++ poco StreamSocket tcpclient测试用例的更多相关文章

  1. 使用AirtestProject+pytest做支付宝小程序UI自动化测试

    一,前言 1,背景 因公司业务需要做支付宝小程序的UI自动化测试,于是在网上查找小程序的自动化资料,发现微信小程序是有自己的测试框架的,但几乎找不到支付宝小程序UI自动化测试相关的资料.白piao失败 ...

  2. airtest+poco多脚本、多设备批处理运行测试用例自动生成测试报告

    一:主要内容 框架功能及测试报告效果 airtest安装.环境搭建 框架搭建.框架运行说明 airtest自动化脚本编写注意事项 二:框架功能及测试报告效果 1. 框架功能: 该框架笔者用来作为公司的 ...

  3. POCO库中文编程参考指南(11)如何使用Reactor框架?

    1 Reactor 框架概述 POCO 中的 Reactor 框架是基于 Reactor 设计模式进行设计的.其中由 Handler 将某 Socket 产生的事件,发送到指定的对象的方法上,作为回调 ...

  4. Windows 10上源码编译Poco并编写httpserver和tcpserver | compile and install poco cpp library on windows

    本文首发于个人博客https://kezunlin.me/post/9587bb47/,欢迎阅读! compile and install poco cpp library on windows Se ...

  5. 使用GDB 追踪依赖poco的so程序,core dump文件分析.

    前言 在windows 下 系统核心态程序蓝屏,会产生dump文件. 用户级程序在设置后,程序崩溃也会产生dump文件.以方便开发者用windbg进行分析. so,linux 系统也有一套这样的东东- ...

  6. poco网络库分析,教你如何学习使用开源库

    Poco::Net库中有 FTPClient HTML HTTP HTTPClient HTTPServer ICMP Logging Mail Messages NetCore NTP OAuth ...

  7. Poco库网络模块例子解析1-------字典查询

    Poco的网络模块在Poco::Net名字空间下定义   下面是字典例子解析 #include "Poco/Net/StreamSocket.h" //流式套接字 #include ...

  8. Poco::TCPServer框架解析

    Poco::TCPServer框架解析 POCO C++ Libraries提供一套 C++ 的类库用以开发基于网络的可移植的应用程序,功能涉及线程.文件.流,网络协议包括:HTTP.FTP.SMTP ...

  9. POCO TCPServer 解析

    POCO C++ Libraries提供一套 C++ 的类库用以开发基于网络的可移植的应用程序,功能涉及线程.文件.流,网络协议包括:HTTP.FTP.SMTP 等,还提供 XML 的解析和 SQL ...

随机推荐

  1. 笔记-Python-language reference-5.the import system

    笔记-Python-language reference-5.the import system 前言 经常用到import,module,对其中的机制及原理有一定的了解,但没有将各种信息前后连通起来 ...

  2. MVC WebAPI 的基本使用

    1.什么是WebAPI Web API是网络应用程序接口.包含了广泛的功能,网络应用通过API接口,可以实现存储服务.消息服务.计算服务等能力,利用这些能力可以进行开发出强大功能的web应用. 它可以 ...

  3. Android getLocationInWindow

    参考博客: http://blog.sina.com.cn/s/blog_44d19b500102vpve.html 这篇博客,我看了三遍,终于看懂了.恩,我就喜欢这样不放弃的自己. 1.getLoc ...

  4. Python 定义及使用结构体

    Python中没有专门定义结构体的方法,但可以使用class标记定义类来代替结构体,其成员可以在构造函数__init__中定义,具体方法如下. class seqNode: def __init__( ...

  5. quick sort去除无用判断

    #include <stdio.h> #include <stdlib.h> //int a[]={1000,10000,9,10,30,20,50,23,90,100,10} ...

  6. USACO Section1.2 Transformations 解题报告

    transform解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------ ...

  7. leetcode 【 Intersection of Two Linked Lists 】python 实现

    题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...

  8. Linux/Unix中系统级IO

    Linux/unix I/O:将设备映射为文件的方式,允许Unix内核引出一个简单.低级的应用接口. Linux/unix IO的系统调用函数很简单,它只有5个函数:open(打开).close(关闭 ...

  9. JavaScript之实现单选复选、菜单以及返回顶部实例

    1.单选.复选以及反选实例 其实主要是利用for循环提取标签,然后更改checked属性值实现的 <!DOCTYPE html> <html lang="en"& ...

  10. Application_Start事件中用Timer做一个循环任务

    protected void Application_Start(object sender, EventArgs e) { System.Timers.Timer timer = new Syste ...