In response to your question Salem, yes this is a thread function, I have included the portion of code where I create the thread and start the function, including the argument I pass the function.
Code:
/*
* Recv the data from the sender
*/
while (1)
{
/* accept a connection from a client program */
int newFD;
newFD = accept( socketFD, (struct sockaddr *)&srcAddr, (socklen_t *)&fromlen );
if ( newFD < 0 )
{
perror( "NATbox:accept()" );
return -1;
}
int threadError = pthread_create(&tid, NULL, respond, (void *) newFD);
if(threadError < 0)
{
perror("NATbox:spawn thread\n");
return -1;
}
}
int status;
int i = 0;
while( i != -1 )
{
i = wait( &status );
printf( "Finished pid = %d\n", i );
}
/*
* Return a 0 to say it worked correctly from this end
*/
return 0;
}
void * respond(void * incomingSocketFD)
{
printf("in respond\n");
char buffer[32];
int newFD;
int retval;
//copy socket
memcpy(&newFD, &incomingSocketFD, sizeof(incomingSocketFD));
//allocate space for message
message = malloc(sizeof(Message));
message->header = (char *)malloc(sizeof(char *));
while(retval != 0)
{
printf("inside while in respond\n");
/* Receive data from the client */
retval = recv(newFD, message, sizeof(Message), 0);
if(retval < 0)//error check
{
perror("NATbox:recv()");
exit(1);
}
I changed the struct as you suggested to have the header field be a fixed array of size 1, which along with your suggested changes to my malloc call have resolved my segmentation fault issue.
The header field of the Message struct is just a single character which indicates what service the client program wants, "A", "M", or "C".
Thanks for the help.
Hunter