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] = '\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. }  


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值