Poco Socket demo 【转】

原文:http://blog.csdn.net/shanno/article/details/7321580

UDP:

/// udp server program
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Timestamp.h"
#include "Poco/DateTimeFormatter.h"
#include <iostream>
int testDatagramSocket(){
    Poco::Net::SocketAddress sa(Poco::Net::IPAddress(), 5004);
    Poco::Net::DatagramSocket dgs(sa);
    char buffer[1024]; // 1K byte

    while(1){
        Poco::Net::SocketAddress sender;
        int n = dgs.receiveFrom(buffer, sizeof(buffer)-1, sender);
        buffer[n] = '\0';
        std::cout << sender.toString() << ":" << buffer << std::endl;
    }

    return 0;
}

   int
main ( int argc, char *argv[] )
{
   testDatagramSocket();
   return EXIT_SUCCESS;
}           /* ----------  end of function main  ---------- */

 

// udp client program
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Timestamp.h"
#include "Poco/DateTimeFormatter.h"

int testUdpClient(){
    const char* ipaddr = "192.168.8.71"; //"192.168.81.140"
    Poco::Net::SocketAddress sa(ipaddr, 5004);
    Poco::Net::DatagramSocket dgs(Poco::Net::IPAddress::IPv4);
    dgs.connect(sa);
    //dgs.bind(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;
}

   int
main ( int argc, char *argv[] )
{
   testUdpClient();
   return EXIT_SUCCESS;
}           /* ----------  end of function main  ---------- */

编译:

g++ UdpServer.cpp -lPocoNet -g -o UdpServer
g++ UdpClient.cpp -lPocoNet -g -o UdpClient

 

TCP:

// tcp server program
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/SocketStream.h"
#include "Poco/Net/SocketAddress.h"

int main(int argc, char** argv){
    Poco::Net::ServerSocket srv(5004); // does bind + listen
    for(;;){
        Poco::Net::StreamSocket ss = srv.acceptConnection();
        Poco::Net::SocketStream str(ss);

        // flush() make sure the file stream is updated with the data.
        // endl() puts a newline and then uses flush().
        str << "HTTP/1.0 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "\r\n"
            "<html><head><title>My 1st Web Server</title></head>"
            "<body><h1>Hello,world!</h1></body></html>"
            "\r\n"
            << std::flush;
    }
    return 0;
}
// client program
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/SocketStream.h"
#include "Poco/StreamCopier.h"
#include <iostream>

int main(int argc, char** argv){
    const char* ipaddr = "192.168.8.71"; // the server address.
    Poco::Net::SocketAddress sa(ipaddr, 5004); // the server port.
    Poco::Net::StreamSocket socket(sa);
    Poco::Net::SocketStream str(socket);

    // Writes all bytes readable from str into std::cout, using an internal buffer.
    Poco::StreamCopier::copyStream(str, std::cout);

    return 0;
}

编译:

g++ TcpServer.cpp -lPocoNet -g -o TcpServer
g++ TcpClient.cpp -lPocoNet -g -o TcpClient

 

posted @ 2013-01-09 12:48  Leo Forest  阅读(3554)  评论(0编辑  收藏  举报