zoukankan      html  css  js  c++  java
  • POCO C++ SOCKET

    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] = '';  
    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 "  
    35.             "Content-Type: text/html "  
    36.             " "  
    37.             "<html><head><title>My 1st Web Server</title></head>"  
    38.             "<body><h1>Hello,Wordl!</h1></body></html>"  
    39.             " "  
    40.             << std::flush;  
    41.     }  
    42.     return 0;  
    43. }  


     

  • 相关阅读:
    C#&nbsp;Andriod&nbsp;AES&nbsp;加密算法
    微软企业库的&nbsp;注入和依赖&amp;nbs…
    锐捷交换机RG-3760-24 的简单配置与VLAN搭建
    TCP-IP and Advanced Topics 课程总结与报告
    Network Security Threats
    Multicast Routing
    OpenFlow, SDN, and NFV
    Mobile IP
    数据结构:树的来源与定义
    TCP Congestion Control
  • 原文地址:https://www.cnblogs.com/lidabo/p/7611976.html
Copyright © 2011-2022 走看看