DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
  1. // client program  
  2. #include "Poco/Net/DatagramSocket.h"  
  3. #include "Poco/Net/SocketAddress.h"  
  4. #include "Poco/Timestamp.h"  
  5. #include "Poco/DateTimeFormatter.h"  
  6.   
  7. int testUdpClient(){  
  8.     const char* ipaddr = "192.168.81.140"; //"192.168.81.140"  
  9.     Poco::Net::SocketAddress sa(ipaddr, 5004);  
  10.     Poco::Net::DatagramSocket dgs(Poco::Net::IPAddress::IPv4);  
  11.     dgs.connect(sa);  
  12.     //dgs.bind(sa);  
  13.     std::string syslogMsg;  
  14.     Poco::Timestamp now;  
  15.     syslogMsg = Poco::DateTimeFormatter::format(now, "<14>%w %f %H:%M:%S Hello,world!");  
  16.     dgs.sendBytes(syslogMsg.data(), syslogMsg.size());  
  17.   
  18.     return 0;  
  19. }  
  20.   
  21. /// server program  
  22. #include "Servers.h"  
  23. #include "Poco/Net/DatagramSocket.h"  
  24. #include "Poco/Net/SocketAddress.h"  
  25. #include "Poco/Timestamp.h"  
  26. #include "Poco/DateTimeFormatter.h"  
  27. int testDatagramSocket(){  
  28.     Poco::Net::SocketAddress sa(Poco::Net::IPAddress(), 5004);  
  29.     Poco::Net::DatagramSocket dgs(sa);  
  30.     char buffer[1024]; // 1K byte  
  31.       
  32.     while(1){  
  33.         Poco::Net::SocketAddress sender;  
  34.         int n = dgs.receiveFrom(buffer, sizeof(buffer)-1, sender);  
  35.         buffer[n] = '\0';  
  36.         std::cout << sender.toString() << ":" << buffer << std::endl;  
  37.     }  
  38.   
  39.     return 0;  
  40. }  


tcp

  1. // client program  
  2. #include "Poco/Net/SocketAddress.h"  
  3. #include "Poco/Net/StreamSocket.h"  
  4. #include "Poco/Net/SocketStream.h"  
  5. #include "Poco/StreamCopier.h"  
  6. #include <iostream>  
  7.   
  8. int main(int argc, char** argv){  
  9.     const char* ipaddr = "192.168.81.140"; // the server address.  
  10.     Poco::Net::SocketAddress sa(ipaddr, 5004); // the server port.  
  11.     Poco::Net::StreamSocket socket(sa);  
  12.     Poco::Net::SocketStream str(socket);  
  13.   
  14.     // Writes all bytes readable from str into std::cout, using an internal buffer.  
  15.     Poco::StreamCopier::copyStream(str, std::cout);  
  16.   
  17.     return 0;  
  18. }  
  19.   
  20. // server program  
  21. #include "Poco/Net/ServerSocket.h"  
  22. #include "Poco/Net/StreamSocket.h"  
  23. #include "Poco/Net/SocketStream.h"  
  24. #include "Poco/Net/SocketAddress.h"  
  25.   
  26. int main(int argc, char** argv){  
  27.     Poco::Net::ServerSocket srv(5004); // does bind + listen  
  28.     for(;;){  
  29.         Poco::Net::StreamSocket ss = srv.acceptConnection();  
  30.         Poco::Net::SocketStream str(ss);  
  31.   
  32.         // flush() make sure the file stream is updated with the data.  
  33.         // endl() puts a newline and then uses flush().  
  34.         str << "HTTP/1.0 200 OK\r\n"  
  35.             "Content-Type: text/html\r\n"  
  36.             "\r\n"  
  37.             "<html><head><title>My 1st Web Server</title></head>"  
  38.             "<body><h1>Hello,Wordl!</h1></body></html>"  
  39.             "\r\n"  
  40.             << std::flush;  
  41.     }  
  42.     return 0;  
  43. }  


 

posted on   DoubleLi  阅读(1064)  评论(0编辑  收藏  举报
编辑推荐:
· 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软件使用
点击右上角即可分享
微信分享提示