Hello. Im doing a simple TCP server using poll. I'm trying to recieve and print messages send from client, but i can not do it properly. I can not manage multiple clients, if i have 2 clients running, only message from the first one are showed up correctly, while the second client as it is sending message too, it just print 2 or 3 message in one.
for(int i = 0; i < conexionesActuales; i++){
if(vigilar[i].revents == 0){
continue;
}
if(vigilar[i].revents != POLLIN){
cout << " Error! revents = %d\n" << vigilar[i].revents;
finalizar_servidor = true;
break;
}
if (vigilar[i].fd == s){
cout<<" Socket a la escucha es leible\n";
do{
struct sockaddr_in cliente;
socklen_t tam = sizeof(cliente);
conexion = accept(s,(struct sockaddr *)&cliente, &tam);
if (conexion < 0){
if (errno != EWOULDBLOCK){
cout<<"Error haciendo conexion con el cliente. "<<endl;
finalizar_servidor = true;
}
break;
}
char ip[INET_ADDRSTRLEN];
inet_ntop( AF_INET,&cliente.sin_addr,ip,sizeof(ip));
cout<< " Nueva conexion entrante desde " <<ip<<":"<<cliente.sin_port<<"\n";
vigilar[numConexion].fd = conexion;
vigilar[numConexion].events = POLLIN;
numConexion++;
}
while (conexion != -1);
}
else{
cerrar_conexion = false;
do{
res = recv(vigilar[i].fd, buffer, sizeof(buffer), 0);
if (res < 0){
if (errno != EWOULDBLOCK){
cout<<" recv fallo";
cerrar_conexion = true;
}
break;
}
if (res == 0){
cout<<" \n\n La conexion con el cliente fue cerrada.\n";
cerrar_conexion = true;
break;
}
buffer[res]='\0';
cout<<" Mensaje: "<< buffer;
fflush(stdout);
buffer[0]='\0';
cout<<"\n "<< res <<" bytes recibidos."<<endl;
}
while(true);
if (cerrar_conexion){
close(vigilar[i].fd);
vigilar[i].fd = -1;
recorrerArreglo = true;
}
}
}
also i need to keep the server running, ive been reading and i suppose it has to be set up at poll( , , ), but i just managed to specify a timout time, not an "always running"