getting the contents of the directory/client-server

Viewed 47

I need to create two applications - a client and a server that would exchange messages with each other . The server must process several commands submitted by the client, one of which is to receive the contents of the specified directory. I kind of tried to do it, and it even works.(by the way, is it possible to do this without writing to a file?)But only this allows you to display the contents of the current directory. How can I do so that I can set not just ls to the console, but also the name of the directory (or the path to it) and I would be given the contents?That is, if we generalize, how can I set the name of a specific directory?

And one more thing. I run the server in one terminal and the client in another. And when sending an ls command(message) to the server, I have the contents displayed both in the terminal with the client and the server.How can I make it appear only in the client, and this information is not displayed in the server?

I will be very grateful for your help.

for(;;){
    recv_id=MsgReceive(channel_id,message, sizeof(message),NULL);
    printf( "Receive message, recv_id=%d\n",recv_id);
    printf("Message is - %s\n",message);
    . . .
    else if (strstr(message, "ls")!=0){
        FILE *f=popen(message,"w");
        FILE *g;
        strcat(message, "ls.txt");
        f=popen(message,"w");
        pclose(f);
        g=fopen("ls.txt","r");
        strcpy(message,"");
        while(feof(f)==0){
            fscanf(f,"%s",&in);
            strcat(message, in);
            strcat(message,"\n");
        }
        fclose(g);
        printf("ls successfull\n");
    }
    . . .

    MsgReply(recv_id, EOK, message,sizeof(message)+1);  
}

upd:I seem to have figured out how to make it easier (yes, at the same time, the contents of the directory are not displayed on the server). But I still can't figure out how to handle this command not only for the current directory.

else if (strstr(message, "ls")!=0){
    system("ls>ls.txt");
    g=fopen("ls.txt","r");
    while(feof(g)==0){
        fscnaf(g,"%s",&in);
        strcat(message,in);
        strcat(message,"\n");
    }
    fclose(g);
}
0 Answers
Related