diff --git a/repos/dde_linux/src/test/lxip/udp_client/main.cc b/repos/dde_linux/src/test/lxip/udp_client/main.cc new file mode 100644 index 000000000..3bed37979 --- /dev/null +++ b/repos/dde_linux/src/test/lxip/udp_client/main.cc @@ -0,0 +1,88 @@ +/* + * \brief Minimal datagram client demonstration using socket API + * \author Martin Stein + * \date 2016-08-17 + */ + +/* + * Copyright (C) 2016 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU General Public License version 2. + */ + +/* Genode includes */ +#include + +/* libc includes */ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace Genode; + +int main(void) +{ + int s; + static Timer::Connection _timer; + _timer.msleep(2000); + log("Create new socket ..."); + if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + error("No socket available!"); + return -1; + } + log("Now, I will bind ..."); + struct sockaddr_in in_addr; + in_addr.sin_family = AF_INET; + in_addr.sin_port = htons(8765); + in_addr.sin_addr.s_addr = INADDR_ANY; + if(bind(s, (struct sockaddr*)&in_addr, sizeof(in_addr))) { + error("bind failed!"); + return -1; + } + enum { ADDR_STR_SZ = 16 }; + char serv_addr[ADDR_STR_SZ] = { 0 }; + Xml_node libc_node = config()->xml_node().sub_node("libc"); + try { libc_node.attribute("server_ip").value(serv_addr, ADDR_STR_SZ); } + catch(...) { + error("Missing \"server_ip\" attribute."); + throw Xml_node::Nonexistent_attribute(); + } + unsigned port = 0; + try { libc_node.attribute("server_port").value(&port); } + catch (...) { + error("Missing \"server_port\" attribute."); + throw Xml_node::Nonexistent_attribute(); + } + log("Start the client loop ..."); + for(int j = 0; j != 5; ++j) { + _timer.msleep(2000); + struct sockaddr_in addr; + addr.sin_family = AF_INET; + socklen_t len = sizeof(addr); + addr.sin_port = htons(port); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr(serv_addr); + char buf[1024]; + Genode::snprintf(buf, sizeof(buf), "UDP server at %s:%u", serv_addr, port); + ssize_t n = sizeof(buf); + n = sendto(s, buf, n, 0, (struct sockaddr*)&addr, len); + n = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len); + if (n == 0) { + warning("Invalid reply!"); + continue; + } + if (n < 0) { + error("Error ", n); + break; + } + log("Received \"", String<64>(buf), " ...\""); + } + _timer.msleep(2000); + return 0; +} diff --git a/repos/dde_linux/src/test/lxip/udp_client/target.mk b/repos/dde_linux/src/test/lxip/udp_client/target.mk new file mode 100644 index 000000000..42b319351 --- /dev/null +++ b/repos/dde_linux/src/test/lxip/udp_client/target.mk @@ -0,0 +1,3 @@ +TARGET = test-lxip_udp_client +LIBS = libc libc_lxip config +SRC_CC = main.cc