LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   a simple file transfer server.. (http://www.linuxquestions.org/questions/programming-9/a-simple-file-transfer-server-481798/)

sachitha 09-09-2006 02:21 AM

a simple file transfer server..
 
i'm trying to create a simple file transfer program using C sockets..
i need to print the server's present working directory(PWD) in the client's prompt, when client input the word "pwd"
i have written the following coding for that.. but the pwd is printed in the server's prompt , instead of the client's prompt

Code:

//code in the str_echo.c in SERVER
.......
.......
                else if(strcmp(arg1,"pwd")==0)
                {
                int pwd;
                pwd=system("pwd");       
                snprintf(line,sizeof(line),(char*)pwd);       
                }
                else if(strcmp(arg1,"ls")==0)
                {
                int ls;
                ls=system("ls");
                snprintf(line,sizeof(line),(char*)ls);                               
                }
.........
......
                n=strlen(line);
                Writen(sockfd, line, n);

can someone help me out plz..

demon_vox 09-09-2006 07:14 PM

Hi,
the function Writeln does not exist on my man pages... maybe you wrote it and the error is in it. Or it could be that sockfd is not what you want (could you have put the stdout in it?).

And another thing (not related to the networking part): system does not return a string. That means that the output is going to stdout and you are missing it. Which is probably what you are seeing on the screen. If you want to get the output, use popen.

Most likely the networking part is just ok and the problem is the one I described in the paragraph above, but I cant tell for sure since I dont know the code.

Hope this is useful! :)

paulsm4 09-09-2006 08:08 PM

Getting back to your original question:

Demon_vox is absolutely correct. The server is printing "pwd" to the server's command-line because you're having the server run the program "pwd" ... which outputs to stdout ... the *SERVER'S* "stdout". Duh.

Opening a pipe to the "pwd" command is one solution.

Using "popen()/read()/pclose ()" (which, under the covers, opens the pipes for you) is an equivalent - and much easier to code - solution.

Simply calling "getcwd()", however, is probably the easiest solution.

Quote:

man 3 getcwd
man 3 popen
'Hope that helps .. PSM


All times are GMT -5. The time now is 08:46 AM.