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(); }
bool SocketTest::udpSocketTest() { DatagramSocket server(TCP_TEST_PORT); DatagramSocket client; InetSocketAddress toaddr(TCP_TEST_PORT); client.sendTo(toaddr, TEST_MESSAGE1); InetSocketAddress fromaddr; char buf[256]; int len = server.recvFrom(fromaddr, (SRL::byte*)buf, 255); buf[len] = '\0'; String msg = buf; TEST_ASSERT(msg == TEST_MESSAGE1) server.close(); return true; }
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; }