I'm writing a socket program and trying to parse command line arguments from the user. Let's say I have a function that looks like this:
void *parseUserReq(char *arg)
{
int sock;
char buffer[1024];
int readIn;
char *str1, *str2, *str3;
//cast sock back to int
sock = (int)arg;
//Get input from client
readIn = recv(sock, buffer, 1024, 0);
buffer[readIn] = '\0';
//parse the 3 strings the user is supposed to enter
str1 = (char*)malloc(sizeof(buffer)+1);
strcpy(str1, buffer); // copy header data into str1
str1 = strtok(str1, " ");
printf("%s\n", str1);
str2 = strtok(str2, " ");
printf("%s\n", str2);
str3 = strtok(str3, "\r\n");
printf("%s\n", str3);
} // End of parseUserReq
How would I call that function in main? Currently I'm doing something like this:
int main(int argc, char** argv)
{
// ^^^Calls for create, bind, listen, accept, and send^^^
char buffer[1024];
parseUserReq(buffer); // segmentation fault
return 0;
}
I can add my code for creating and binding the socket, and listening, accepting,sending if necessary.
EDIT: Just spent a while on this, and found that the issue is more complex than I initially described. I will use Valgrind to identify the source of the problem, or just rewrite the function entirely in accordance with your commentary. I'll hunt around for answers more extensively before posting next time.
Thanks again for the help everyone!