POCO C + + sockets

Source: Internet
Author: User
Tags server port

  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::D atagramsocket DGs (POCO::NET::IPADDRESS::IPV4);
  10. Dgs.connect (SA);
  11. //dgs.bind (SA);
  12. Std::string syslogmsg;
  13. Poco::timestamp now;
  14. syslogmsg = Poco::D atetimeformatter::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::D atagramsocket 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] = ' + ';
  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 to 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 was updated with the data.
  27. //Endl () puts a newline and then uses flush ().
  28. Str << "http/1.0 ok\r\n"
  29. "content-type:text/html\r\n"
  30. "\ r \ n"
  31. "
  32. "<body>
  33. "\ r \ n"
  34. << Std::flush;
  35. }
  36. return 0;
  37. }


POCO C + + sockets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.