简体   繁体   中英

how to print system(command) output to file C?

I'm learning C network programming.

now I'm trying to print the output of commands coming from client to file in the server's folder.

Server Code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int read_size;
char Message[1024]="Hello client , I'm the Server";

int check(char *msg){return !strcmp(msg,"exit");}

int main(){
    //creat socket
    int soc;
    soc = socket(AF_INET, SOCK_STREAM, 0);

    //define server address
    struct sockaddr_in server_address;
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(9002);
    server_address.sin_addr.s_addr = INADDR_ANY;

    //bind the socket to ip and port
    bind(soc, (struct sockaddr*) &server_address, sizeof(server_address));

    listen(soc, 5);

    puts("Listening...");

    int client_soc;
    client_soc = accept(soc, 0, 0);
    //send the message
    send(client_soc, Message, sizeof(Message), 0);
    if(client_soc<0){
        puts("Connection Error");
        return 0;
    }   
    puts("Connected");

    //Receive a message from client
    while(1){
        char response[1024]="";
        recv(client_soc,response, 1024, 0);
        //fflush(stdout);

        if(check(response)){
                        puts("Disconnected !");
                        break;
                }

        strcat(response," > out.txt");
        int sys = system(response);

        if(sys != 0)//worng command enterd
            printf("Worng Command");


    }
    //close the sockets
    close(client_soc);
    close(soc);
    return 0;
}

Client Code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

char response[1024];
char Message[1024];

int check(char *msg){
        return !strcmp(msg,"exit");
}
int main(){
        //creat a socket
        int soc;
        soc = socket(AF_INET, SOCK_STREAM, 0);

    //specify an address for the socket
    struct sockaddr_in server_address;
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(9002);
    server_address.sin_addr.s_addr = INADDR_ANY;
    int con = connect(soc , (struct sockaddr *) &server_address , sizeof(server_address));

    //check for error with the connection
    if(con == -1){
        puts("Connection Error !");
        close(soc);
        exit(0);
    }

    //recieve data from the server
    recv(soc, &response, sizeof(response), 0);

    //print recieved data
    printf("Server : %s\n",response);   

    //Send message to server
    //send(soc, Message, strlen(Message), 0);
    while(1){
        printf("Message For The Server : ");
        //scanf("%s",Message);
        gets(Message);
        send(soc, Message, strlen(Message), 0);
        //fflush(stdout);
        if(check(Message)){
            puts("Disconnected !");
            break;
        }
    }
    //close the socket
    close(soc);
        return 0;

}

now the problem is :

when I send a valid command (from the client) the server will print the result in the file without problems , but when I send invalid command it will clean the file and print the output in the terminal.

can I print the wrong message coming from "system()" in the file instead of the terminal ?

A command's output and its error/status messages are cleanly separated so that you don't have to worry about error messages corrupting the program's expected output.

1> aka > only redirects output, while 2> only redirects errors:

$ ls -l myfile filethatdoesntexist > output 2> errors

$ cat output
-rw-r--r-- 1 me me 42 Sep 11 14:08 myfile

$ cat errors
ls: cannot access 'filethatdoesntexist': No such file or directory

If you want to write both to the same file, the shell construct to use is > file 2>&1

$ ls -l myfile filethatdoesntexist > out.txt 2>&1

$ cat out.txt
ls: cannot access 'filethatdoesntexist': No such file or directory
-rw-r--r-- 1 me me 42 Sep 11 14:08 myfile

So you could change your suffix to:

strcat(response," > out.txt 2>&1");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM