#include #include #include "Network.h" int kbhit() { struct timeval tv; fd_set fds; tv.tv_sec = 0; tv.tv_usec = 0; FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0 select(STDIN_FILENO+1, &fds, NULL, NULL, &tv); return FD_ISSET(STDIN_FILENO, &fds); } int main(int argc, char **argv) { //UDP /*int l = 10; char c[l]; UDP::sendTo("127.0.0.1", 9995, "quit"); bool b = UDP::receiveFrom(9999, c, 10); std::cout << std::boolalpha << b << " " << c << std::endl;*/ srand(time(0)); if(argc > 1){ TCPClient socket; std::string name = std::string(argv[1]), msg; bool connected = socket.connect("127.0.0.1", 9995, name); std::cout << "connection established: " << std::boolalpha << connected << std::endl; while(socket.isConnected()){ if(kbhit()){ std::getline(std::cin, msg); socket.send({msg}); } msg = (std::string)socket.receive(); if(msg != "") std::cout << "serveur: " << msg << std::endl; } }else{ int n = 2; TCPServer socket(9995, n, true); bool hosting = socket.host(); std::cout << "host: " << std::boolalpha << hosting << std::endl; if(hosting){ //while(socket.getNbConnected() < 2) std::this_thread::sleep_for(std::chrono::milliseconds(1)); /*while(true){ std::string msg = socket.receive(), name = socket.getName(); if(msg != "" || name != "") std::cout << "from:" << name << " (" << msg.size() << "): " << msg << std::endl; }*/ bool stop = false; std::string msg; do{ std::this_thread::sleep_for(std::chrono::milliseconds(1)); if(kbhit()){ std::getline(std::cin, msg); for(int i = 0;i