So, im making a server to handle multiple clients. The clients sends simple commands like "ls -l" or "mkdir test" and the server executes and send information back to the client.

When i connect my client, i can get the output from the server to my client, one time, but then the server program seems to get stuck.
If i use a test string with send(), it will work multiple times as it should be.
Why does my program gets stuck using execvp?

Replies To: execvp() and my program gets stuck, send() and it works.

Re: execvp() and my program gets stuck, send() and it works.

Posted 07 March 2013 - 12:51 AM

Ugh, where to begin.

1. Your server main is far too long (and deeply nested). As a rough rule of thumb, if you can't see the whole function on screen at the same time (namely both it's opening and closing braces), then it's probably time to think about splitting things up.

Have something like#define MAX_STRING_LEN 1000
and use it in all the appropriate places.

4. Pointless code

for(z=0;z<99;z++){
str1[z] = NULL;
str2[z] = NULL;
}

Not only do you get the length wrong, NULL is semantically associated with pointers, and not the nul char, otherwise written as '\0'
Also, such code is pointless if things are being done properly elsewhere (more on that later).

First, use the symbolic array length (or better yet, sizeof) to indicate how much buffer space recv has.
Second (important), you need to understand that recv does NOT make strings out of everything.
Third (very important), you need to deal with message fragmentation. This is probably not an issue for AF_UNIX sockets, but when you switch over to AF_INET, that's when the fun really starts.
In general, you can never assume that if something sends "hello world\n", that recv() will always receive "hello world\n" in a single message. Sometimes, you may get "hel" and then later "lo world\n". It is YOUR job to detect this, and build a semantically complete message before trying to process it further.
The first two points are addressed by

Re: execvp() and my program gets stuck, send() and it works.

Posted 07 March 2013 - 08:56 PM

execvp will replace the current process image with the program specified as the parameter. No instructions after an execvp call will be executed because they will no longer exists (that is that execvp was successful)