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. int testUdpClient(){
  7. const char* ipaddr = "192.168.81.140"; //"192.168.81.140"
  8. Poco::Net::SocketAddress sa(ipaddr, 5004);
  9. Poco::Net::DatagramSocket dgs(Poco::Net::IPAddress::IPv4);
  10. dgs.connect(sa);
  11. //dgs.bind(sa);
  12. std::string syslogMsg;
  13. Poco::Timestamp now;
  14. syslogMsg = Poco::DateTimeFormatter::format(now, "<14>%w %f %H:%M:%S Hello,world!");
  15. dgs.sendBytes(syslogMsg.data(), syslogMsg.size());
  16. return 0;
  17. }
  18. /// server program
  19. #include "Servers.h"
  20. #include "Poco/Net/DatagramSocket.h"
  21. #include "Poco/Net/SocketAddress.h"
  22. #include "Poco/Timestamp.h"
  23. #include "Poco/DateTimeFormatter.h"
  24. int testDatagramSocket(){
  25. Poco::Net::SocketAddress sa(Poco::Net::IPAddress(), 5004);
  26. Poco::Net::DatagramSocket dgs(sa);
  27. char buffer[1024]; // 1K byte
  28. while(1){
  29. Poco::Net::SocketAddress sender;
  30. int n = dgs.receiveFrom(buffer, sizeof(buffer)-1, sender);
  31. buffer[n] = '\0';
  32. std::cout << sender.toString() << ":" << buffer << std::endl;
  33. }
  34. return 0;
  35. }

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

POCO C++ SOCKET的更多相关文章

  1. POCO库中文编程参考指南(3)Poco::Net::Socket

    POCO库中文编程参考指南(3)Poco::Net::Socket 作者:柳大·Poechant 博客:Blog.CSDN.net/Poechant 邮箱:zhongchao.ustc#gmail.c ...

  2. c++ poco StreamSocket tcpclient测试用例

    1.代码 #include <iostream> #include "Poco/Net/Socket.h" #include "Poco/Net/Stream ...

  3. POCO库中文编程参考指南(8)丰富的Socket编程

    POCO库中文编程参考指南(8)丰富的Socket编程 作者:柳大·Poechant 博客:Blog.CSDN.net/Poechant 邮箱:zhongchao.ustc#gmail.com (# ...

  4. 关于 Poco::TCPServer框架 (windows 下使用的是 select模型) 学习笔记.

    说明 为何要写这篇文章 ,之前看过阿二的梦想船的<Poco::TCPServer框架解析> http://www.cppblog.com/richbirdandy/archive/2010 ...

  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::TCPServer框架解析

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

  8. 如何编译POCO

    Poco C++库是: 一系列C++类库,类似Java类库,.Net框架,Apple的Cocoa; 侧重于互联网时代的网络应用程序 使用高效的,现代的标准ANSI/ISO C++,并基于STL 高可移 ...

  9. POCO TCPServer 解析

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

随机推荐

  1. java面试二

    技术交流群: 233513714 126.什么是ORM?答:对象关系映射(Object-Relational Mapping,简称ORM)是一种为了解决程序的面向对象模型与数据库的关系模型互不匹配问题 ...

  2. qq登录面板

  3. LC.exe已退出,代码为-1

    解决方法就是把Properties文件下的license.licx给删除,重新编译,这样就可以了.

  4. 《Cracking the Coding Interview》——第6章:智力题——题目1

    2014-03-19 06:40 题目:有20瓶药,其中19瓶装的都是1.0克的药片,只有1瓶装了1.1克的药.给你一个能称出具体克数的电子秤,只允许你称一次,怎么找出那瓶不一样的? 解法:如果药片管 ...

  5. 数据库——pymysql模块的使用(13)

    1.基本用法——建立链接,获取游标,执行sql语句,关闭 建立远程链接账号和权限 mysql> grant all on *.* to '; Query OK, rows affected, w ...

  6. Ubuntu下禁用笔记本自带键盘

    想要禁用笔记本自带键盘(Ubuntu)只要2条命令. 1. 打开终端,输入: xinput list ⎡ Virtual core pointer id=2 [master pointer (3)] ...

  7. Linux开启MySQL远程连接

    Linux开启MySQL远程连接的设置步骤 . MySQL默认root用户只能本地访问,不能远程连接管理MySQL数据库,那么Linux下如何开启MySQL远程连接?设置步骤如下: 1.GRANT命令 ...

  8. HDU 4661 Message Passing ( 树DP + 推公式 )

    参考了: http://www.cnblogs.com/zhsl/archive/2013/08/10/3250755.html http://blog.csdn.net/chaobaimingtia ...

  9. CodeForces A. Many Equal Substrings

    http://codeforces.com/contest/1029/problem/A You are given a string tt consisting of nn lowercase La ...

  10. linux shell常用语法

    特殊变量 $0 当前脚本的文件名$n 传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例如,第一个参数是$1,第二个参数是$2.$# 传递给脚本或函数的参数个数.$* 传递给脚本或函数的所有参 ...