问题
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:
Server:
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();
Client:
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();
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...
Thanks for your help :)
回答1:
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.
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";
}
}
}
来源:https://stackoverflow.com/questions/24364308/cryptopp-how-to-use-socketsource-and-socketsink