我有此客户端和服务器程序。我正在尝试添加一个历史命令,当您在命令行中键入“历史”时,它会显示最近的10条命令,但是我遇到了问题,根本无法显示,但我不确定为什么它不会工作。键入“历史记录”后,它将不会显示任何内容,也无法键入任何命令,这之后我不得不重新启动代码才能键入unix命令。该程序通过发送诸如“ ls”之类的unix命令来工作或“日期”,它将信息从服务器发送回客户端。可能是因为我的代码有点bug,但它仍然可以正常工作。谁能指出我正确的方向?
客户:
Invalid 'X-Frame-Options' header encountered when loading 'https://cross-domain-services.firebaseapp.com/auth': 'ALLOW-FROM http://localhost:4200/' is not a recognized directive. The header will be ignored.
服务器:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFFSIZE 2048
char history[10][50];
int historyCounter = 0;
char *encryptedMessage(char command[]){
int i = 0;
while (command[i] != '\0'){
command[i] += 5;
i += 1;
}
return command;
}
int main(int argc, char *argv[]){
/*Created variables for socket, server address, arguement size, and a buffer*/
int clientSocket, ret, portnum;
struct sockaddr_in serverAddr;
int received = 0;
unsigned int echolen;
char buffer[BUFFSIZE];
/*If there is less than 3 arguments, print out a statement explaining to execute program
along witjh server ip and port*/
if(argc < 3){
fprintf(stderr,"usage %s <server-ip-addr> <server-port>\n", argv[0]);
exit(0);
}
/*Convert port number from the commandline using atoi*/
portnum = atoi(argv[2]);
/*Create socket, and check if there is an error in the connection*/
clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if(clientSocket < 0){
printf("Error in connection.\n");
exit(1);
}
printf("Client Socket is created.\n");
/*Memset server address*/
memset(&serverAddr, '\0', sizeof(serverAddr));
// memset(&buffer, '\0', sizeof(buffer));
// serverAddr.sin_family = AF_INET;
// serverAddr.sin_port = htons(PORT);
// serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
// bzero((char *) &serverAddr, sizeof(serverAddr));
/*Take ip address from commandline and set it into serverAddr*/
serverAddr.sin_family = AF_INET;
if(!inet_aton(argv[1], &serverAddr.sin_addr)){
fprintf(stderr, "Error invalid server IP address\n");
exit(1);
}
/*Take the port number and set it in the sin_port for connection*/
serverAddr.sin_port = htons(portnum);
/*Connect the client to the server, if less than 0, then there is an error in the connection*/
ret = connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
if(ret < 0){
printf("Error in connection.\n");
exit(1);
}
printf("Connected to server.\n");
/*Initialize argument buffer*/
char s[100];
/*In the while loop we take the arguments after we connect and put them in stdin, we insert \0 at the
end of s array*/
while(1){
int _switch = 0;
fgets(s, 100, stdin);
historyCounter++;
//s[strlen(s)-1]='\0';
echolen = strlen(s);
if(historyCounter < 10){
for(int i = 0; i < historyCounter; i++){
strcpy(history[i+1], history[i]);
}
}
strcpy(history[0], s);
/* send() from client, if it doesn't equal echolen then show it failed */
if(strcmp(s,"history") == 0){
_switch = 1;
int a = 0;
int b = 0;
for (int a = 0; a < historyCounter; a++){
printf("%d", historyCounter);
while(history[a][b] != '\0'){
printf("%c", history[a][b]);
b++;
}
b = 0;
printf("\n");
}
printf("\n");
}
if(_switch == 0){
if (send(clientSocket, s, echolen, 0) != echolen)
{
printf("Failed");
}
if(strcmp(s,"quit") == 0) //check if exit is typed
exit(0);
fprintf(stdout, "Message from server: ");
//fprintf(stdin, "%s\n", s);
while (received < echolen)
{
ssize_t bytes = 0;
/* recv() from server, the bytes we receive are from the clientSocket */
if ((bytes = recv(clientSocket, buffer, echolen, 0)) < 1)
{
printf("Failed to get Information");
}
received += bytes;
buffer[bytes] = '\0';
/*WE receive the information and print in stdout what's in the buffer*/
fprintf(stdout, buffer);
}
ssize_t bytes = 0;
/*Place /0 at end of the buffer*/
do {
buffer[bytes] = '\0';
printf("%s\n", buffer);
} while((bytes = recv(clientSocket, buffer, BUFFSIZE-1, 0))>=BUFFSIZE-1);
buffer[bytes] = '\0';
printf("%s\n", buffer);
printf("\n");
}
/*Above is essentially printing out the information within the buffer from the recv()*/
}
}
答案 0 :(得分:0)
fgets()读取直到换行符,但还在输入缓冲区的末尾复制该换行符。在用于历史记录命令的strcmp()中,它可能会失败。请检查fgets()的手册页。