2014-09-06 46 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