Quantcast
Viewing all articles
Browse latest Browse all 6129

Select on C not working as it should, it changes the sockets

I have a project where the server receives the petitions of 1 client from the browser. I'm supposed to make the connection persistent, that the socket doesn't change from petition to petition.

I have to send 2 files, the html and an image that it's on the html, I don't have any troubles with sending them, the problem is that when I look at Wireshark to see the traces, I see that it first establish the connection with 2 SYNC packets and then the GET /index.html, but when it ends, there are another SYNC for the next GET, the image, and when the timeout of the select happens, both connections seems to end with packets that FIN,ACK, that I suppose it's the end of the connection, both GET's are send from a different port, the image port is +4 that the html.

When I'm trying to debug with prints, it seems that both petitions came at the same time and they both get in and when they finish there is nothing to be read, I think that is the main problem, but I don't know.

void process_web_request(int descriptorFichero){    fd_set rfds;    struct timeval tv;    int retval;    FD_ZERO(&rfds);    FD_SET(descriptorFichero, &rfds);    tv.tv_sec = 20;    tv.tv_usec = 0;    while(1)     {            retval = select(descriptorFichero+1, &rfds, NULL, NULL, &tv);        if(retval)        {            //Read and process the petition...            //and down here send to client            size_t l = strlen(respuesta);            write(descriptorFichero, respuesta, l);            while ((readsz = read(fdp, buffer, BUFSIZE)) > 0)            {                write(descriptorFichero, buffer, readsz);            }            close(fdp); //File descriptor from the file we are reading            FD_SET(descriptorFichero, &rfds);        }        else        {            close(descriptorFichero);            exit(1);        }    }}

I know there is a lot to improve in this little code, but right now, I want to finish this first.

EDIT: It seems that the first petition is ok, but when it does the select() the second time it crashes, abort the process and another process different takes the second petition. But I don't have any idea how it crashes.


Viewing all articles
Browse latest Browse all 6129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>