Upgrade Socket pour multi-in

This commit is contained in:
Debucquoy Anthony 2021-12-16 00:11:41 +01:00
parent 9e418f9193
commit 2b8f34f7af
3 changed files with 13 additions and 10 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
main

View File

@ -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;
}

View File

@ -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*);