DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  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 2017-09-29 17:31  DoubleLi  阅读(1003)  评论(0编辑  收藏  举报