CryptoPP:如何使用 SocketSource 和 SocketSink [英] CryptoPP: how to use SocketSource and SocketSink

查看:62
本文介绍了CryptoPP:如何使用 SocketSource 和 SocketSink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 SocketSource 和 SocketSink 发送一个字符串.但不知何故它无法正常工作.我只是想将它从我的服务器发送到客户端.代码如下:

I'm trying to send a string via SocketSource and SocketSink. But somehow it won't work properly. I simply want to send it from my server to the client. Here's the code:

服务器:

    CryptoPP::Socket server;
    CryptoPP::Socket client;
    sockaddr_in client_sadr;
    CryptoPP::socklen_t size_sock = sizeof(sockaddr_in);
    timeval timev = {3, 0};
    std::string test("a simple test");

    CryptoPP::Socket::StartSockets();

    server.Create(SOCK_STREAM);
    server.Bind(4213, NULL);
    server.Listen();

    server.Accept(client, (sockaddr*)&client_sadr, &size_sock);

    std::cout << "Client connected" << std::endl;

    while (!client.SendReady(&timev));
    CryptoPP::StringSource ss(test, true, new CryptoPP::SocketSink(client));

    std::cout << "Data sent" << std::endl;

    std::cin.ignore();

    client.CloseSocket();
    server.CloseSocket();

    CryptoPP::Socket::ShutdownSockets();

客户:

    CryptoPP::Socket client;
    CryptoPP::socklen_t size_sock = sizeof(sockaddr_in);
    timeval timev = {3, 0};
    std::string test;

    Socket::StartSockets();

    client.Create(SOCK_STREAM);
    client.Connect("127.0.0.1", 4213);

    std::cout << "connected" << std::endl;

    while (!client.ReceiveReady(&timev));
    CryptoPP::SocketSource(client, true, new StringSink(test));

    std::cout << test << std::endl;

    std::cin.ignore();

    client.CloseSocket();
    Socket::ShutdownSockets();

现在发生了什么:连接按预期建立,服务器发送数据,客户端接收数据并在cin.ignore()处等待.但是服务器在发送时似乎挂了,因为它不会打印数据发送".它仅在客户端关闭连接时执行此操作.我现在的问题是,我做错了什么,或者这只是 SocketSource 和 SocketSink 的正常行为,我每次都必须重新连接...

What happens now: The connection is established as wished, and the server sends the data, the client receives it and waits at cin.ignore(). But the server seemes to hang up while sending, because it won't print "Data send". It only does this, when the client closes the connection. My question now is, am I doing something wrong, or is this just the normal behavior of SocketSource and SocketSink and i have to reconnect everytime...

感谢您的帮助:)

推荐答案

以下来自test.cpp.它可能会给你一些想法.我不记得读过如何使用它们(而且我从未在程序中使用过它们).这是我见过的唯一一个 PumpAll2 和非阻塞使用的地方.

The following is from test.cpp. It might give you some ideas. I don't recall reading on how to use them (and I've never used them in a program). Its the only place I've ever seen PumpAll2 and non-blocking used.

您可能会发现它们在 Windows 上比在 Linux 上运行得更好.

You might find they work better on Windows than Linux.

void ForwardTcpPort(const char *sourcePortName, const char *destinationHost,
        const char *destinationPortName)
{
    SocketsInitializer sockInit;

    Socket sockListen, sockSource, sockDestination;

    int sourcePort = Socket::PortNameToNumber(sourcePortName);
    int destinationPort = Socket::PortNameToNumber(destinationPortName);

    sockListen.Create();
    sockListen.Bind(sourcePort);
    setsockopt(sockListen, IPPROTO_TCP, TCP_NODELAY, "\x01", 1);

    cout << "Listing on port " << sourcePort << ".\n";
    sockListen.Listen();

    sockListen.Accept(sockSource);
    cout << "Connection accepted on port " << sourcePort << ".\n";
    sockListen.CloseSocket();

    cout << "Making connection to " << destinationHost << ", port " << destinationPort << ".\n";
    sockDestination.Create();
    sockDestination.Connect(destinationHost, destinationPort);

    cout << "Connection made to " << destinationHost << ", starting to forward.\n";

    SocketSource out(sockSource, false, new SocketSink(sockDestination));
    SocketSource in(sockDestination, false, new SocketSink(sockSource));

    WaitObjectContainer waitObjects;

    while (!(in.SourceExhausted() && out.SourceExhausted()))
    {
        waitObjects.Clear();

        out.GetWaitObjects(waitObjects, CallStack("ForwardTcpPort - out", NULL));
        in.GetWaitObjects(waitObjects, CallStack("ForwardTcpPort - in", NULL));

        waitObjects.Wait(INFINITE_TIME);

        if (!out.SourceExhausted())
        {
            cout << "o" << flush;
            out.PumpAll2(false);
            if (out.SourceExhausted())
                cout << "EOF received on source socket.\n";
        }

        if (!in.SourceExhausted())
        {
            cout << "i" << flush;
            in.PumpAll2(false);
            if (in.SourceExhausted())
                cout << "EOF received on destination socket.\n";
        }
    }
}

这篇关于CryptoPP:如何使用 SocketSource 和 SocketSink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆