#include "wifi-triangulator/core.h" #include #include #include #include using namespace std; using namespace wifi_triangulator; int main(int argc, char *argv[]) { app_name = argv[0]; if (argc != 1) { fprintf(stderr, "usage: %s\n", argv[0]); return EXIT_FAILURE; } GOOGLE_PROTOBUF_VERIFY_VERSION; uint16_t port = 3333; int s; struct sockaddr_in si_me; if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) throw std::runtime_error("socket"); memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(port); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if (::bind(s, reinterpret_cast(&si_me), sizeof(si_me)) == -1) { throw std::runtime_error("bind"); } pb::envelope envelope; while (true) { size_t len = 64 * 1024; uint8_t bytes[len]; ssize_t n_read; struct sockaddr_in si_other; socklen_t addr_len; if ((n_read = ::recvfrom(s, bytes, len, 0, reinterpret_cast(&si_other), &addr_len)) == -1) { throw std::runtime_error("recvfrom: " + string(strerror(errno))); } uint16_t size = static_cast(n_read); write_stdout(reinterpret_cast(&size), 2); write_stdout(bytes, static_cast(n_read)); } google::protobuf::ShutdownProtobufLibrary(); }