我正在学习C网络编程。
现在,我正在尝试将来自客户端的命令输出打印到服务器文件夹中的文件中。
服务器代码:
#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;
}
客户代码:
#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;
}
现在的问题是:
当我(从客户端)发送有效命令时,服务器将在文件中打印结果而不会出现问题,但是当我发送无效命令时,服务器将清理文件并在终端中打印输出。
我可以在文件而不是终端中打印来自“ system()”的错误消息吗?
答案 0 :(得分:4)
命令的输出及其错误/状态消息完全分开,因此您不必担心错误消息会破坏程序的预期输出。
1>
或>
仅重定向输出,而2>
仅重定向错误:
$ 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
如果要将两者都写入同一个文件,则要使用的shell构造为> 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
因此您可以将后缀更改为:
strcat(response," > out.txt 2>&1");