diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +main diff --git a/Network.cpp b/Network.cpp index e540fcf..b80749f 100644 --- a/Network.cpp +++ b/Network.cpp @@ -7,11 +7,14 @@ Network::Network(){ conf = new Config("config.txt"); + + strcpy(motd, conf->getValue("motd").c_str()); + strcat(motd, "\n"); + memset(&server, 0, sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(stoi(conf->getValue("port"))); inet_aton(conf->getValue("ip").c_str(), &server.sin_addr); - /* server.sin_addr.s_addr = INADDR_ANY; */ main_socket = socket(server.sin_family, SOCK_STREAM, 0); if(main_socket == -1){ @@ -29,8 +32,6 @@ Network::Network(){ void Network::run(){ accepted_sock.resize(stoi(conf->getValue("conn_limit"))); - /* motd = conf->getValue("motd").c_str(); */ - /* std::cout << motd << std::endl; */ while(!will_close){ FD_ZERO(&readfds); @@ -56,7 +57,7 @@ void Network::run(){ } accepted_sock.push_back(_incomming_socket); std::cout << "New Connection: " << inet_ntoa(new_sockaddr.sin_addr) << std::endl; - /* if(send(_incomming_socket, &motd, sizeof(&motd), 0) == -1){perror("sendt");} */ + if(send(_incomming_socket, motd, strlen(motd), 0) == -1){perror("sendt");} }else{ for(int fd: accepted_sock){ if(FD_ISSET(fd, &readfds)){ @@ -65,11 +66,12 @@ void Network::run(){ close(fd); accepted_sock.erase(std::find(accepted_sock.begin(), accepted_sock.end(), fd)); }else{ - msg[rcv_bytes] = '\0'; - std::cout << "he sent :" << msg << std::endl; - if(std::strcmp(msg, "ping")){ - send(fd, "pong", 4, 0); + std::cout << "he sent :" << rcv_bytes << " bytes and it says: " << msg; + if(std::strncmp("ping", msg, rcv_bytes-1) == 0){ + send(fd, "pong", 5, 0); + } + } break; } diff --git a/Network.hpp b/Network.hpp index f122645..084f3de 100644 --- a/Network.hpp +++ b/Network.hpp @@ -27,8 +27,8 @@ private: sockaddr_in server, new_sockaddr; bool will_close = false; - char msg[1024]; - const char* motd; + char msg[1025], motd[1025]; + int rcv_bytes; unsigned int sin_size = sizeof(struct sockaddr*);