Poco library Chinese programming Reference Guide (8) rich socket programming

Source: Internet
Author: User

Poco library Chinese programming Reference Guide (8) rich socket programming
  • Author: Liu Da-poechant
  • Blog: blog.csdn.net/poechant
  • Email: zhongchao. USTC # gmail.com (#-> @)
  • Date: 10000l 16Th, 2012
1 socket in poco

Poco has rich socket encapsulation. The inheritance relationship is as follows:

This article only introduces streamsocket, serversocket, and initramsocket.

2 poco: Net: serversocket

Serversocket is a socket with a low encapsulation level. It uses TCP connections. The tcpserver or reactor framework is recommended for actual servers. The following is a routine:

#include "Poco/Net/ServerSocket.h"#include "Poco/Net/SocketStream.h"#include "Poco/Net/StreamSocket.h"#include <iostream>int main(int argc, char **argv){

Bind the port and start listening:

    Poco::Net::ServerSocket srv(12345);

Main service cycle:

    while (true)    {

Accept connection:

        Poco::Net::StreamSocket streamSocket = srv.acceptConnection();

Send data to socket:

        Poco::Net::SocketStream socketStream(streamSocket);        socketStream << "HTTP/1.0 200 OK\r\n"                     << "Content-Type: text/html\r\n"                     << "\r\n"                     << "
3 tcpserver framework

Poco provides a tcpserver, which, as its name implies, is a TCP server. To be accurate, tcpserver is a framework and requires the use of serversocket for accept connection. In addition, you must set the serversocket to the listening mode before transferring it to the tcpserver.

The tcpserver maintains a connection queue ). The tcpserver enables multiple threads to fetch and process connections from the connection queue. The number of threads is dynamic and related to the number of connections in the connection queue.

An abnormal connection is immediately closed without being inserted into the connection queue. The main thread of the tcpserver is responsible for placing the request connection sent from the client into the connection queue.

Tcpserver is an efficient development box framework. For details, see poco library Chinese programming Reference Guide (10) How to Use tcpserver framework? Article.

4 poco: Net: datagramsocket4.1 UDP client
#include "Poco/Net/DatagramSocket.h"  #include "Poco/Net/SocketAddress.h"  #include "Poco/Timestamp.h"  #include "Poco/DateTimeFormatter.h"  #include <string>int main(){    const char* ipaddr = "127.0.0.1";    Poco::Net::SocketAddress sa("127.0.0.1", 1234);      Poco::Net::DatagramSocket dgs;    dgs.connect(sa);     std::string syslogMsg;      Poco::Timestamp now;      syslogMsg = Poco::DateTimeFormatter::format(now, "<14>%w %f %H:%M:%S Hello,world!");      dgs.sendBytes(syslogMsg.data(), syslogMsg.size());      return 0;  }  

Unavailable:

...const char* ipaddr = "127.0.0.1";Poco::Net::SocketAddress sa("127.0.0.1", 1234);  Poco::Net::DatagramSocket dgs(sa);std::string syslogMsg;  Poco::Timestamp now; ... 

Because the mongoramsocket (socketaddress) constructor creates a mongoramsocket and then bind (). Connect () is used here ().

4.2 UDP Server
#include "Poco/Net/DatagramSocket.h"#include "Poco/Net/IPAddress.h"#include <iostream>int main(int argc, char **argv){    Poco::Net::SocketAddress socketAddress(Poco::Net::IPAddress(), 1234);    Poco::Net::DatagramSocket datagramSocket(socketAddress);    char buffer[1024];    while (true)    {        Poco::Net::SocketAddress sender;        int n = datagramSocket.receiveFrom(buffer, sizeof(buffer) - 1, sender);        buffer[n] = '\0';        std::cout << sender.toString() << ": " << buffer << std::endl;    }    return 0;}
5. Reactor framework

The reactor framework is a server framework based on the reactor Design Pattern Implemented by event/notification. It supports Asynchronization due to the existence of event/notification.

For details, see another blog article titled poco library Chinese programming Reference Guide (11) How to Use the reactor framework?

6 streamsocket

It is also a TCP socket that simplifies data transmission operations and is used on TCP servers and clients.

-

Reprinted, please indicate the csdn blog from LIU Da: blog.csdn.net/poechant

-

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.