Help with reading from socket.

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.


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
    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"

Thanks in advance.
Last edited on
Topic archived. No new replies allowed.