Example of SocketGroup
#include <iostream>
#include <string.h>
using namespace std;
#include <netlink/socket.h>
#include <netlink/socket_group.h>
const unsigned SERVER_PORT = 5000;
group->
add(newConnection);
cout <<
"\nConnection " << newConnection->
hostTo() <<
":" << newConnection->
portTo() <<
" added...";
cout.flush();
}
};
cout << "\nREAD -- ";
cout.flush();
char buffer[256];
buffer[255] = '\0';
socket->
read(buffer, 255);
size_t msgLen = strlen(buffer);
cout <<
"Message from " << socket->
hostTo() <<
":" << socket->
portTo() <<
". Text received: " << buffer;
cout.flush();
for(
unsigned i=1; i < (unsigned) group->
size(); ++i)
if(group->
get(i) != socket)
group->
get(i)->
send(buffer, msgLen + 1);
}
};
cout <<
"\nClient " << socket->
hostTo() <<
" disconnected...";
cout.flush();
delete socket;
}
};
int main() {
cout << "\nStarting Server...";
cout.flush();
OnAccept onAccept;
OnRead onRead;
OnDisconnect onDisconnect;
group.
add(&socketServer);
while(true) {
cout << "\nNo msg recieved during the last 2 seconds";
}
}