Hello -
I've got a simple client and simple server setup going. Right now, my client uses send() twice: first to send a header, and then to send a request. I know that this functionality is working correctly (I'll explain later).
On the server side, I started simple; just had a while(1) loop accepting new connections and then recv()'ing the two send()'s. This worked just fine, and I could pull all the data that was sent and print it out to ensure it was correct.
However, to allow for multiple requests to be processed at once, I wanted to detach a new pthread for every connection I had incoming. So I did something like this:
Code:
void *processRequest (void* arg) {
int socket = (int) arg;
char buf1[100];
recv(socket, buf1, 100, 0);
char buf2[100];
recv(socket, buf2, 100, 0); // BLOCKS HERE
close(socket);
// Do stuff with the buffers here.
}
int main() {
// Set up socket stuff here
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 1024*1024);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
while(1) {
int connection = accept(socket, null, null);
pthread_t thread;
pthread_create (&thread, &attr, processRequest, (void *) connection);
}
}
As noted in the comment in the code above, the program gets stuck in the second recv() call when I use pthreads. I've got no idea why. Does anyone have any suggestions?
Thanks.