// multi_server.cpp #include "socket.h" #include #include 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 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; }