Thread: opened a file using popen() to run ls on server and return output to client(telnet)

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    opened a file using popen() to run ls on server and return output to client(telnet)

    Hi,
    I am trying to implement telnet and execute "ls" command on the server side (called from the client side). For that, i have popen() a file and passed "ls" in a buffer from the client side to the server.

    I am trying to read the output of this file in the same buffer using fgets(), line at a time.
    Then writing the contents of this buffer to the client using write() system call.
    But it only displays the last file read from the file and not the whole files in the directory.Whereas, it prints the whole list on the server side.

    Code:
     fp = popen(buffer, "r");
      if (fp == NULL) {
        printf("Failed to run command\n" );
        exit;
      }
     while (fgets(buffer, sizeof(buffer)-1, fp) != NULL) {
        printf("%s", buffer); 
    }
    pclose(fp);
     n = write(newsockfd,buffer,strlen(buffer));
         if (n < 0) error("ERROR writing to socket");
    Is there anyway that i can pass the whole list of files read from the file to the client using write system call???

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by shwety View Post
    Hi,
    Code:
     while (fgets(buffer, sizeof(buffer)-1, fp) != NULL) {
        printf("%s", buffer); 
    }
    pclose(fp);
     n = write(newsockfd,buffer,strlen(buffer));
         if (n < 0) error("ERROR writing to socket");
    Is there anyway that i can pass the whole list of files read from the file to the client using write system call???
    Yes. Try moving the call to write and the error checking inside your while (fgets) loop. Read a line, write a line, read a line, write a line. Not read 100 lines, each into the same place, then write 1.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    2
    thanks for the reply !

    I did try to do it that way but at run time it used to read and display just 3 files on server side and display the first on the client side, and then terminate saying "broken pipe".

    Is there any other way this can be done?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    This is probably one of the easier ways. Perhaps I can help you figure out your broken pipe issue if you posted all of your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server application for hybrid file transfer
    By ejjjjj in forum C Programming
    Replies: 6
    Last Post: 05-16-2010, 02:27 PM
  2. Server Client return IP Adress
    By bigmen2007 in forum C Programming
    Replies: 12
    Last Post: 04-28-2010, 06:56 AM
  3. Telnet-client, get abracadabra from server
    By LuckyStr in forum C Programming
    Replies: 4
    Last Post: 08-30-2009, 05:49 AM
  4. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  5. C++ telnet client
    By GUIPenguin in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-30-2005, 11:08 AM