物理の駅 Physics station by 現役研究者

テクノロジーは共有されてこそ栄える

PocoによるTCP/IP通信のサーバー・クライアント実装例

Pocoはc++のライブラリの一つで、boostより比較的軽量に設計されている。バージョン1.7.3を使ってTCP通信のサーバー・クライアントの実装例を書いた。例外処理は甘いところがあるので適宜変更されたし。

サーバーの実装例

#include <iostream>

#include <Poco/Net/Socket.h>
#include <Poco/Net/TCPServer.h>
#include <Poco/Net/StreamSocket.h>

const int BYTE_LENGTH = 1024;

void tcp_send(Poco::Net::StreamSocket &ss, std::string str)
{
    if (str.size() > BYTE_LENGTH) throw std::invalid_argument("str.size() > BYTE_LENGTH");
    int ret = ss.sendBytes(str.c_str(), str.size());
    if (ret < 0) throw std::exception();
}
std::string tcp_read(Poco::Net::StreamSocket &ss)
{
    char data[BYTE_LENGTH];
    int count = ss.receiveBytes(data, BYTE_LENGTH);
    std::string str(data, count);
    if (count == BYTE_LENGTH) throw std::exception();
    return str;
}

int main() {
    int port = 15001;

    while (true) {
        Poco::Net::ServerSocket *serv;
        Poco::Net::StreamSocket *ss;
        serv = new Poco::Net::ServerSocket(port);
        serv->listen();

        std::cout << "Waiting for connection";
        ss = new Poco::Net::StreamSocket(serv->acceptConnection());
        ss->setNoDelay(true);
        std::cout << "\rConnected from " << ss->peerAddress().host().toString() << " " << ss->address().port() << std::endl;
        try {
            while (true) {
                std::string str = tcp_read(*ss);
                tcp_send(*ss, "You sent " + str);
                std::cout << "I received \"" + str << "\"." << std::endl;
            }
        }
        catch (...) {
            ss->close();
            serv->close();
        }
    }
}

クライアントの実装例

#include <iostream>

#include <Poco/Net/Socket.h>
#include <Poco/Net/StreamSocket.h>
#include <Poco/Thread.h>

const int BYTE_LENGTH = 1024;

void tcp_send(Poco::Net::StreamSocket &ss, std::string str)
{
    if (str.size() > BYTE_LENGTH) throw std::invalid_argument("str.size() > BYTE_LENGTH");
    int ret = ss.sendBytes(str.c_str(), str.size());
    if (ret < 0) throw std::exception();
}
std::string tcp_read(Poco::Net::StreamSocket &ss)
{
    char data[BYTE_LENGTH];
    int count = ss.receiveBytes(data, BYTE_LENGTH);
    std::string str(data, count);
    if (count == BYTE_LENGTH) throw std::exception();
    return str;
}

int main() {
    int port = 15001;
    std::string host = "localhost";

    while (true) {
        try {
            Poco::Net::StreamSocket *ss;
            auto address = Poco::Net::SocketAddress(host, port);
            ss = new Poco::Net::StreamSocket(address.family());
            ss->setNoDelay(true);
            ss->connect(address);
            try {

                while (true) {
                    std::string str;
                    std::cin >> str;
                    tcp_send(*ss, str);
                    std::cout << "I received \"" + tcp_read(*ss) << "\"."<< std::endl;
                }
            }
            catch (...) {
                ss->close();
            }
        }
        catch (...) {
            Poco::Thread::sleep(1000);
            std::cout << "Waiting for server to startup\n";
        }
    }
}