0

我在 stackoverflow 客户端-服务器代码上找到了(使用命名管道),并对其进行了修改以让客户端在 while 循环中发送消息。虽然我从服务器收到消息,但我在读/写时收到“非法搜索”,我不确定它是否是一个严重的错误,如果这里真的有问题吗?

服务器.cpp

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

int main()
{
   int client_to_server;
   char *myfifo = "/tmp/client_to_server_fifo";

   int server_to_client;
   char *myfifo2 = "/tmp/server_to_client_fifo";

   char buf[BUFSIZ];

   /* create the FIFO (named pipe) */
   mkfifo(myfifo, 0666);
   mkfifo(myfifo2, 0666);

   /* open, read, and display the message from the FIFO */
   client_to_server = open(myfifo, O_RDONLY);
   server_to_client = open(myfifo2, O_WRONLY);

   printf("Server ON.\n");

   while (1)
   {
      read(client_to_server, buf, BUFSIZ);

      if (strcmp("exit",buf)==0)
      {
         printf("Server OFF.\n");
         break;
      }

      else if (strcmp("",buf)!=0)
      {
         printf("Received: %s\n", buf);
         printf("Sending back...\n");
         write(server_to_client,buf,BUFSIZ);
      }

      /* clean buf from any data */
      memset(buf, 0, sizeof(buf));
   }

   close(client_to_server);
   close(server_to_client);

   unlink(myfifo);
   unlink(myfifo2);
   return 0;
}

客户端.cpp

#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
   int client_to_server;
   char *myfifo = "/tmp/client_to_server_fifo";

   int server_to_client;
   char *myfifo2 = "/tmp/server_to_client_fifo";

   char str[BUFSIZ];


   //printf("Input message to serwer: ");
   //scanf("%s", str);


   /* write str to the FIFO */
   client_to_server = open(myfifo, O_WRONLY);
   server_to_client = open(myfifo2, O_RDONLY);

while(true)
{

   printf("> "); 
   scanf("%s", str);
   write(client_to_server, str, sizeof(str));

   perror("Write:"); //Very crude error check

   read(server_to_client,str,sizeof(str));

   perror("Read:"); // Very crude error check

   printf("From  server: %s\n",str);
}
   close(client_to_server);
   close(server_to_client);

   /* remove the FIFO */

   return 0;
}

示例输出:

客户

> hello, server!
Write:: Success
Read:: Illegal seek
From  server: hello,
Write:: Illegal seek
Read:: Illegal seek
> From  server: server!
> its client here!
Write:: Illegal seek
Read:: Illegal seek
From  server: its
Write:: Illegal seek
Read:: Illegal seek
> From  server: client
Write:: Illegal seek
Read:: Illegal seek
> From  server: here!
> > hello, server!
// and so on ...

服务器

Server ON.
Received: hello,
Sending back...
Received: server!
Sending back...
Received: its
Sending back...
Received: client
Sending back...
Received: here!
Sending back...
Received: >
Sending back...
Received: hello,
Sending back...
Received: server!
Sending back...
Received: Write::
Sending back...
Received: Success
Sending back...
Received: Read::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: From
Sending back...
Received: server:
Sending back...
Received: hello,
Sending back...
Received: Write::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: Read::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: >
Sending back...
Received: From
Sending back...
Received: server:
Sending back...
Received: server!
Sending back...
Received: >
Sending back...
Received: its
Sending back...
Received: client
Sending back...
Received: here!
Sending back...
Received: Write::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: Read::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: From
Sending back...
Received: server:
Sending back...
Received: its
Sending back...
Received: Write::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: Read::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: >
Sending back...
Received: From
Sending back...
Received: server:
Sending back...
Received: client
Sending back...
Received: Write::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: Read::
Sending back...
Received: Illegal
Sending back...
Received: seek
Sending back...
Received: >
Sending back...
Received: From
Sending back...
Received: server:
Sending back...
Received: here!
Sending back...
Received: >
Sending back...
4

1 回答 1

2

perror如果没有错误,请不要调用。

您需要在调用之前检查上一个函数调用是否失败,perror否则在没有实际发生错误时您将收到无意义的错误消息。errno如果函数没有失败,则函数(通常)不需要设置为任何特定的值。

于 2012-12-02T14:52:59.810 回答