c++ - CryptoPP: how to use SocketSource and SocketSink -
i'm trying send string via socketsource , socketsink. somehow won't work properly. want send server client. here's 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: connection established wished, , server sends data, client receives , waits @ cin.ignore(). server seemes hang while sending, because won't print "data send". this, when client closes connection. question is, doing wrong, or normal behavior of socketsource , socketsink , have reconnect everytime...
thanks help :)
the next test.cpp. might give ideas. don't recall reading on how utilize them (and i've never used them in program). place i've ever seen pumpall2 , non-blocking used.
you might find work improve on windows 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 " << destinationhost << ", port " << destinationport << ".\n"; sockdestination.create(); sockdestination.connect(destinationhost, destinationport); cout << "connection made " << destinationhost << ", starting 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"; } } } c++ windows sockets crypto++
No comments:
Post a Comment