Upgrade Socket pour multi-in
This commit is contained in:
parent
9e418f9193
commit
2b8f34f7af
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
main
|
18
Network.cpp
18
Network.cpp
@ -7,11 +7,14 @@
|
|||||||
|
|
||||||
Network::Network(){
|
Network::Network(){
|
||||||
conf = new Config("config.txt");
|
conf = new Config("config.txt");
|
||||||
|
|
||||||
|
strcpy(motd, conf->getValue("motd").c_str());
|
||||||
|
strcat(motd, "\n");
|
||||||
|
|
||||||
memset(&server, 0, sizeof(server));
|
memset(&server, 0, sizeof(server));
|
||||||
server.sin_family = AF_INET;
|
server.sin_family = AF_INET;
|
||||||
server.sin_port = htons(stoi(conf->getValue("port")));
|
server.sin_port = htons(stoi(conf->getValue("port")));
|
||||||
inet_aton(conf->getValue("ip").c_str(), &server.sin_addr);
|
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);
|
main_socket = socket(server.sin_family, SOCK_STREAM, 0);
|
||||||
if(main_socket == -1){
|
if(main_socket == -1){
|
||||||
@ -29,8 +32,6 @@ Network::Network(){
|
|||||||
void Network::run(){
|
void Network::run(){
|
||||||
|
|
||||||
accepted_sock.resize(stoi(conf->getValue("conn_limit")));
|
accepted_sock.resize(stoi(conf->getValue("conn_limit")));
|
||||||
/* motd = conf->getValue("motd").c_str(); */
|
|
||||||
/* std::cout << motd << std::endl; */
|
|
||||||
|
|
||||||
while(!will_close){
|
while(!will_close){
|
||||||
FD_ZERO(&readfds);
|
FD_ZERO(&readfds);
|
||||||
@ -56,7 +57,7 @@ void Network::run(){
|
|||||||
}
|
}
|
||||||
accepted_sock.push_back(_incomming_socket);
|
accepted_sock.push_back(_incomming_socket);
|
||||||
std::cout << "New Connection: " << inet_ntoa(new_sockaddr.sin_addr) << std::endl;
|
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{
|
}else{
|
||||||
for(int fd: accepted_sock){
|
for(int fd: accepted_sock){
|
||||||
if(FD_ISSET(fd, &readfds)){
|
if(FD_ISSET(fd, &readfds)){
|
||||||
@ -65,11 +66,12 @@ void Network::run(){
|
|||||||
close(fd);
|
close(fd);
|
||||||
accepted_sock.erase(std::find(accepted_sock.begin(), accepted_sock.end(), fd));
|
accepted_sock.erase(std::find(accepted_sock.begin(), accepted_sock.end(), fd));
|
||||||
}else{
|
}else{
|
||||||
msg[rcv_bytes] = '\0';
|
std::cout << "he sent :" << rcv_bytes << " bytes and it says: " << msg;
|
||||||
std::cout << "he sent :" << msg << std::endl;
|
if(std::strncmp("ping", msg, rcv_bytes-1) == 0){
|
||||||
if(std::strcmp(msg, "ping")){
|
send(fd, "pong", 5, 0);
|
||||||
send(fd, "pong", 4, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -27,8 +27,8 @@ private:
|
|||||||
|
|
||||||
sockaddr_in server, new_sockaddr;
|
sockaddr_in server, new_sockaddr;
|
||||||
bool will_close = false;
|
bool will_close = false;
|
||||||
char msg[1024];
|
char msg[1025], motd[1025];
|
||||||
const char* motd;
|
|
||||||
int rcv_bytes;
|
int rcv_bytes;
|
||||||
unsigned int sin_size = sizeof(struct sockaddr*);
|
unsigned int sin_size = sizeof(struct sockaddr*);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user