Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void * fctThread (void *param);
- pthread_mutex_t mutexThread;
- pthread_cond_t clientCond;
- int actualClient = -1;
- Socket *clientSockets[MAXCLIENT];
- ServerSocket *serverSocket;
- #define LOCK(mutex) pthread_mutex_lock(mutex)
- #define UNLOCK(mutex) pthread_mutex_unlock(mutex)
- #define COND_WAIT(condition, mutex) pthread_cond_wait(condition, mutex)
- #define COND_SIGNAL(condition) pthread_cond_signal(condition)
- int main(int argc, const char * argv[]) {
- pthread_t threadHandles[MAXCLIENT];
- int i;
- pthread_mutex_init(&mutexThread, NULL);
- pthread_cond_init(&clientCond, NULL);
- createFICHPARCRecords(fileName);
- displayFICHPARC(fileName);
- /*Initializing all client sockets and client threads*/
- for (i=0; i<MAXCLIENT; i++) {
- clientSockets[i] = NULL;
- pthread_create(&threadHandles[i], NULL, fctThread, NULL);
- }
- //Starting the server
- try {
- serverSocket = new ServerSocket("50001");
- } catch (SocketException& exc) {
- cout<<exc.getExplanation()<<endl;
- exit(1);
- }
- while (true)
- {
- //Looking for a free socket slot
- for (i=0; i<MAXCLIENT && clientSockets[i] != NULL; i++);
- if (i == MAXCLIENT) {
- //No places anymore
- }
- else
- {
- //free socket slot found on i
- try {
- clientSockets[i] = serverSocket->acceptClient();
- cout<<"A client connected"<<endl;
- LOCK(&mutexThread);
- actualClient = i;
- COND_SIGNAL(&clientCond);
- UNLOCK(&mutexThread);
- } catch (SocketException &Exc) {
- //Someone tried to connect but failed, we should accept a new client on the same socket slot (so i--)
- cout<<Exc.getExplanation()<<endl;
- i--;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement