1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
while(1)
{
FD_ZERO(&readfs); // Read fd_set set to 0
FD_SET(listenfd,&readfs); // Listening socket set in read fd_set
// For each alive client, we monitor its socket
for(int i=0;i<clientMax;i++)
{
if(clientAlive[i]==0)
continue;
FD_SET(connfd[i],&readfs);
}
// Timeout reinitialisation
tv.tv_sec = 0;
tv.tv_usec = 500000;
// If keyboard activity
if(_kbhit())
{
char c=_getch(); // We retrieve the key
// Phase 0 : hitting i starts phase 1
if(phase==0 && c=='i')
{
phase = 1; // Next phase
// For each alive client, we send an init cam request
for(int i=0;i<clientMax;i++)
{
if(clientAlive[i]==0)
continue;
printf("client %d : init cam request sent\n",i);
// According to the defined protocol, data in phase 0 must begin with 0x01
data[0]=0x01;
data[1]=0x00;
// We add the client socket in write fd_set, to send him the data
FD_SET(connfd[i],&writefs);
}
}
/***** Je vous passe les autres phases *****/
// We reset captureNeeded boolean
captureNeeded=0;
}
// We monitor the activity of the sockets set in the Read and Write fd_set
int retval = select(maxdescriptor + 1, &readfs, &writefs, NULL , &tv);
// If 0 : no activity
if (retval==0)
{
printf("No activity .... clients : %d , phase : %d\n",nbClient,phase);
}
else
{
// Activity on the listening socket?
if (FD_ISSET (listenfd, &readfs))
{
printf("Incoming connection!\n");
// We accept a new connection
connfd[clientMax] = accept(listenfd, (struct sockaddr *) &clientInfos, &client_len);
// We update the maxdescriptor
if(connfd[clientMax]>maxdescriptor)
maxdescriptor=connfd[clientMax];
// New client is alive
clientAlive[clientMax]=1;
// We are going to send his id to him
sprintf_s(data,"%d",clientMax);
data_len=2;
// We add the client socket in write fd_set, to send him the data
FD_SET(connfd[clientMax],&writefs);
// We update the counter variables
clientMax++;
nbClient++;
}
// Activity on an alive client socket?
for(int i=0;i<clientMax;i++)
{
if(clientAlive[i]==0)
continue;
// Writing activity ?
if (FD_ISSET (connfd[i], &writefs))
{
// We send data to the client
send(connfd[i], data, data_len, 0);
// We erase the socket from the writing fd_set
FD_CLR(connfd[i],&writefs);
// If phase 3 is on :
if(phase==3 && start==0)
{
// Client i has started capturing
captureStarted[i]=1;
// We forge the phase 3 first packet of the client
data[0]=0x04;
data[1]=0x00;
// We send it
send(connfd[i], data, data_len, 0);
printf("First capture request sent to client %d\n",i);
// We reset data for the others non-started clients
data[0]=0x03;
data[1]=0x00;
}
// We check if all the clients are started
start=1;
for(int j=0;j<clientMax && start;j++)
{
if(clientAlive[j]==0)
continue;
start=start&&captureStarted[j];
}
continue;
}
// Reading activity ?
if (FD_ISSET (connfd[i], &readfs))
{
// We receive data
data_len=recv(connfd[i], data,255, 0);
// If length < 0 it means that the client has gone offline
if(data_len<0)
{
printf("Client %d disconnected\n",i);
// We update the array
clientAlive[i]=0;
// We erase its socket from all the fd_sets
FD_CLR(connfd[i],&readfs);
FD_CLR(connfd[i],&writefs);
// We close the connection
shutdown(connfd[i],2);
// We update counter
nbClient--;
continue;
}
else
{
printf("Incoming data from client %d at time %d\n",i,clock());
// New capture received
captureReceived[i]=1;
// We check if all the clients capture have been received
captureNeeded=1;
for(int j=0;j<clientMax && captureNeeded;j++)
{
if(clientAlive[j]==0)
continue;
captureNeeded=captureNeeded&&captureReceived[j];
}
}
}
}
}
// We reset the Write fd_set
FD_ZERO(&writefs);
} |
Partager