2014-09-06 48 views
1

我正在开发一个客户端服务器程序,这是我的server_2文件,他将与主服务器通信。 程序在运行时在屏幕上显示这些行。我认为在mkfifo之后的这些行引起了这一点。程序在屏幕上显示奇怪的字符

i�e|楬���h�.N=��.8�� 
i�H��h� ��h� �i���Ǭ��ǬjǬ�dǬ�@��i�[email protected]�Ǭ���h����h�jǬ��ǬP 

结构

typedef struct request req; 
struct request 
{ 
    char str[256]; 
    int client_pid; 
    int login; // In case of client, to identify if is logged 
    int whois; // To identify who is the client and the server 
}; 

typedef struct answer ans; 
struct answer 
{ 
    char str[256]; 
    int server_pid; 
    int type; 
    int login; 
    int num_users; 
}; 

主营:

#include "header.h" 

int main(int argc, char *argv[]) 
{ 
    int fifo_1, fifo_2; 
    struct request req; 
    struct answer ans; 

    if(argc == 2) // Check if the command was well prompted 
    { 
     if(strcasecmp(argv[1], "show") == 0 || strcasecmp(argv[1], "close") == 0) 
     { 
      if(fifo_2 = open("FIFO_SERV", O_WRONLY) == -1) 
      { 
       perror("[SERVER_2] Error: on the FIFO_SERVER opening!\n"); 
       sleep(2); 
       exit(EXIT_FAILURE); 
      } 

      if(mkfifo("FIFO_SERV_2", 0777) == -1) 
      { 
       perror("[SERVER_2] Error: on the FIFO_SERVER_2 creation!\n"); 
       sleep(2); 
       exit(EXIT_FAILURE); 
      } 

      strcpy(req.str, argv[1]); // Copy the argumento to the structure 

      write(fifo_2, &req, sizeof(req)); // Write a request to the server 
      strcpy(req.str,""); // Clean the string 

      fifo_1 = open("FIFO_SERV_2", O_RDONLY); 

      read(fifo_1, &ans, sizeof(ans)); //Read an answ 
     } 

    //close(fifo_1); 
    unlink("FIFO_SERVER_2"); 
    sleep(2); 
    exit(EXIT_SUCCESS); 
} 
+1

你还没有给我们足够的代码...就像结构等。 – 2014-09-06 00:45:35

回答

4

运营商=的优先级规则和==使线条相当于01

if(fifo_2 = open("FIFO_SERV", O_WRONLY) == -1) 
if(fifo_2 = (open("FIFO_SERV", O_WRONLY) == -1)) 

其基本上分配0至fifo_2如果open成功和1如果open失败。值0和1也恰好是在POSIX标准库的实现(见File descriptor on wikipedia)标准输入和输出文件描述符的各个值,所以后来当执行

write(fifo_2, &req, sizeof(req)); // Write a request to the server 

你要么试图写入标准输入(未定义行为)或标准输出,具体取决于文件是否可以打开,而不是服务器。为了解决这个问题,你可以取代公开表达:

if((fifo_2 = open("FIFO_SERV", O_WRONLY)) == -1) 

然后,你可能要搞清楚为什么你不能打开文件(因为你是大概写到标准输出,这意味着open失败) 。

+0

谢谢你的提示SleuthEye,它不是打印奇怪的字符!事实上,现在该计划没有做任何事情。在我开始运行它之后,不要做任何事情。 – 2014-09-06 01:07:54