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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2013-09-29 C/C++代码静态检查工具Cppcheck在VS2008开发环境中的安装配置和使用
2013-09-29 MFC上下浮动与渐入渐出消息提示框实现
2013-09-29 MFC渐入渐出框实现方式二
2013-09-29 Cppcheck软件使用