rendered paste body// multi_server.cpp
#include "socket.h"
#include <iostream>
#include <windows.h>
using namespace std;
void sendMessageToClient( void* socket );
bool g_bWrongSocket = false;
Socket g_sock_Wrong;
int main( void )
{
fstream pong;
string command;
int iCnt, i, ready, sock_max, max=-1;
int client_sock[FD_SETSIZE];ss
fd_set gesamt_sock, lese_sock;
try
{
Socket sock1, sock2, sock3;
struct timeval Timeout;
sock1.create();
sock1.bind(15000);
sock1.listen();
sock_max = sock1.get_m_sock();
Timeout.tv_sec = 1;
Timeout.tv_usec = 1;
for(i=0; i<FD_SETSIZE; i++)
{
client_sock[i]=-1;
}
FD_ZERO(&gesamt_sock);
FD_SET(sock1.get_m_sock(), &gesamt_sock);
for (;;)
{
//
// Immer Aktualisieren
//
lese_sock = gesamt_sock;
//
// Hier wird auf die Ankunft von Daten oder
// neuer Verbindungen von Clients gewartet
//
cout << "[+] Waiting for connections";
ready = select( sock_max+1, &lese_sock,
NULL, NULL, &Timeout );
//
// Eine neue Client-Verbindung ... ?
//
for(int iCnt=0; iCnt < MAXCONNECTIONS; iCnt++)
{
if ( FD_ISSET(sock1.get_m_sock(), &lese_sock) )
{
sock1.accept(sock2);
//
// Freien Platz für (Socket-)Deskriptor
// in client_sock suchen und vergeben
for( i=0; i< FD_SETSIZE; i++)
{
if(client_sock[i] < 0)
{
client_sock[i] = sock2.get_m_sock();
cout << ":";
iCnt--;
break;
}
}
//
// Mehr als FD_SETSIZE Client sind nicht möglich
//
if( i == FD_SETSIZE )
{
throw SockExcept("Server überlastet - zuviele Clients");
//
// Den neuen (Socket-)Deskriptor zur
// (Gesamt)Menge hinzufügen
//
FD_SET(sock2.get_m_sock(), &gesamt_sock);
//
// Anzahl der Scokets bekommen
//
if( sock2.get_m_sock() > sock_max )
{
sock_max = sock2.get_m_sock();
}
//
// höchster Index für client_sock
// für die anschließende Schleife benötigt
//
if( i > max )
{
max = i;
}
//
// ... weitere (Lese-)Deskriptoren bereit ?
//
if( --ready <= 0 )
{
continue; //Nein ...
}
}
else
{
Sleep(3500);
cout << ".";
}
}
//
// Ab hier werden alle Verbindungen von Clients auf
// die Ankunft von neuen Daten überprüft
//
for(i=0; i<=max; i++)
{
sock3.set_m_sock(client_sock[i]);
if( sock3.get_m_sock() < 0 )
{
continue;
}
//
// (Socket-)Deskriptor gesetzt ...
//
if(FD_ISSET(sock3.get_m_sock(), &lese_sock))
{
//
// ... dann die Daten lesen
//
sock3.recv(s);
cout << "[" << i+1 << "]" << "Nachricht empfangen: " << s << endl;
cout.flush();
}
/* _beginthread( sendMessageToClient, 0, (void*)sock3 );
if( true == g_bWrongSocket )
{
g_sock_Wrong.cleanup();
g_sock_Wrong.close();
}
*/
if( s == "" )
{
sock3.close();
//
//aus Menge löschen
//
FD_CLR(sock3.get_m_sock(), &gesamt_sock);
//
// auf -1 setzen
//
client_sock[i] = -1;
cout << "Ein Client hat sich beendet"
<< endl;
}
//
// Noch lesbare Deskriptoren vorhanden ... ?
//
if( --ready <= 0 )
{
break; // Nein ...
}
}
}
} // for(;;)
}
catch( SockExcept& e )
{
cout << "Exception wurde ausgworfen: "
<< e.get_SockExcept() << endl;
}
return 0;
}
void sendMessageToClient( void* sock )
{
string toSend;
cout << "Message to write: ";
cin >> toSend;
if( false == sock.send( toSend ) )
{
g_bWrongSocket = true;
g_sock_Wrong = sock;
}
return;
}