virtual void test() { DatagramSocket socket; char data[1024] = {0,}; DatagramPacket packet(data, sizeof(data), "127.0.0.1", 8080); packet.write("hello"); packet.write(" world"); socket.send(packet); socket.close(); }
TEST(DatagramSocket, testEcho) { DatagramSocket ss; ss.connect(SocketAddress("www.opendns.com", 53)); int n = ss.sendBytes(dns_query, sizeof(dns_query)); //EXPECT_TRUE (n == sizeof(dns_query)); //char buffer[256]; //n = ss.receiveBytes(buffer, sizeof(buffer)); //EXPECT_TRUE (n == 5); //EXPECT_TRUE (std::string(buffer, n) == "hello"); ss.close(); }
void DatagramSocketTest::testUnbound() { UDPEchoServer echoServer; DatagramSocket ss; char buffer[256]; ss.connect(SocketAddress("localhost", echoServer.port())); int n = ss.sendBytes("hello", 5); assert (n == 5); n = ss.receiveBytes(buffer, sizeof(buffer)); assert (n == 5); assert (std::string(buffer, n) == "hello"); ss.close(); }
void DatagramSocketTest::testSendToReceiveFrom() { UDPEchoServer echoServer(SocketAddress("localhost", 0)); DatagramSocket ss; int n = ss.sendTo("hello", 5, SocketAddress("localhost", echoServer.port())); assert (n == 5); char buffer[256]; SocketAddress sa; n = ss.receiveFrom(buffer, sizeof(buffer), sa); assert (sa.host() == echoServer.address().host()); assert (sa.port() == echoServer.port()); assert (n == 5); assert (std::string(buffer, n) == "hello"); ss.close(); }
int run_udp_client(InetSocketAddress &addr) { try { InetSocketAddress from; SRL::byte buf[256]; DateTime dt; DatagramSocket client; while (1) { dt.update(); client.sendTo(addr, dt.asString()); Console::formatLine("sent: '%s'", dt.asString().text()); System::Sleep(1000); } client.close(); } catch (SRL::Errors::Exception &e) { Console::writeLine(e.message()); } return 0; }
virtual void run() { Selector selector; socket = new DatagramSocket(1900); socket->setReuseAddr(); socket->setBroadcast(); socket->joinGroup("239.255.255.250"); socket->registerSelector(selector); while (!interrupted()) { if (selector.select(1000) > 0) { char buffer[1024] = {0,}; int len = socket->recv(buffer, sizeof(buffer)); if (len > 0) { buffer[len - 1] = 0; cout << "[RECV] >> " << buffer << endl; } } else { idle(10); } } socket->close(); }
void DatagramLocalSocketTest::testDatagramSocketPerformance() { Timestamp::TimeDiff local, net; std::size_t initBufSize = 1; std::size_t initReps = 1; double locData = 0.0, locTime = 0.0, netData = 0.0, netTime = 0.0; std::cout << std::endl << "OS Name: " << Environment::osName() << std::endl; std::cout << "OS Version: " << Environment::osVersion() << std::endl; std::cout << "OS Architecture: " << Environment::osArchitecture() << std::endl; std::ostringstream os; os << Environment::osName() << '-' << Environment::osVersion() << '-' << Environment::osArchitecture() << "-UDP.csv"; File f(os.str()); if (f.exists()) f.remove(); FileOutputStream fos(os.str()); for (std::size_t repetitions = initReps; repetitions <= 100000; repetitions *= 10) { for (std::size_t bufSize = initBufSize; bufSize < 20000; bufSize *= 2) { char* pBuf = new char[bufSize]; { UDPLocalEchoServer echoServer(bufSize); DatagramSocket ss(SocketAddress("/tmp/poco.client.udp.sock"), true); SocketAddress addr(echoServer.address().toString()); ss.connect(addr); int recv = 0, sent = 0; Stopwatch sw; int i = 0; for (; i < repetitions; ++i) { sent = 0; recv = 0; local = 0; do { int s; sw.restart(); s = ss.sendBytes(pBuf + sent, bufSize - sent); sw.stop(); local += sw.elapsed(); sent += s; } while (sent < bufSize); do { int r; sw.restart(); r = ss.receiveBytes(pBuf + recv, bufSize - recv); sw.stop(); local += sw.elapsed(); recv += r; } while (recv < bufSize); locData += sent; locData += recv; locTime += local; poco_assert (sent == bufSize && recv == bufSize); } std::cout << "Local UDP socket, " << i << " repetitions, " << bufSize << " bytes, " << local << " [us]." << std::endl; ss.close(); } { UDPEchoServer echoServer(bufSize); DatagramSocket ss; ss.connect(SocketAddress("localhost", echoServer.port())); int recv = 0, sent = 0; Stopwatch sw; int i = 0; for (; i < repetitions; ++i) { sent = 0; recv = 0; net = 0; do { int s; sw.restart(); s = ss.sendBytes(pBuf + sent, bufSize - sent); sw.stop(); net += sw.elapsed(); sent += s; } while (sent < bufSize); do { int r; sw.restart(); r = ss.receiveBytes(pBuf + recv, bufSize - recv); sw.stop(); net += sw.elapsed(); recv += r; } while (recv < bufSize); netData += sent; netData += recv; netTime += net; poco_assert (sent == bufSize && recv == bufSize); } std::cout << "Network UDP socket, " << i << " repetitions, " << bufSize << " bytes, " << net << " [us]." << std::endl; fos << i << ',' << bufSize << ','; ss.close(); } delete pBuf; double ratio = local ? ((double) net) / ((double) local) : (double) net; double diff = ((double) net) - ((double) local); std::cout << "Ratio: " << ratio << "(" << diff / 1000.0 << "[us/msg]" << ')' << std::endl; fos << ratio << std::endl; } } poco_assert (locData == netData); double locDTR = ((locData / (locTime / Timestamp::resolution())) * 8) / 1000000; double netDTR = ((netData / (netTime / Timestamp::resolution())) * 8) / 1000000; std::cout << "=================================" << std::endl << "Local DTR: " << locDTR << " [Mbit/s]" << std::endl << "Network DTR: " << netDTR << " [Mbit/s]" << std::endl << "Local sockets speedup: " << ((locDTR / netDTR) * 100) - 100 << '%' << std::endl << "=================================" << std::endl; fos << "=================================" << std::endl << "Local DTR: " << locDTR << " [Mbit/s]" << std::endl << "Network DTR: " << netDTR << " [Mbit/s]" << std::endl << "Local sockets speedup: " << ((locDTR / netDTR) * 100) - 100 << '%' << std::endl << "=================================" << std::endl; fos.close(); }