Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 正确的FIFO客户端-服务器连接_C_Linux_Client Server_Mkfifo - Fatal编程技术网

C 正确的FIFO客户端-服务器连接

C 正确的FIFO客户端-服务器连接,c,linux,client-server,mkfifo,C,Linux,Client Server,Mkfifo,我试图编写简单的客户机和服务器C程序,在不同的终端上相互通信 服务器必须创建公共fifo并等待客户端。与此同时,客户机正在创建自己的fifo,通过fifo,服务器将得到响应。客户端的任务是向服务器发送由队列创建的名称,并返回ls命令的结果 我确实在寻找答案,例如:。我从第三个链接开始编写代码,然后慢慢修改 我现在得到的是一个客户端从用户那里获取输入,将其发送到服务器并接收回来。但它只起作用一次。我不知道为什么。主要功能的主体如下。如果有任何帮助,我将不胜感激 编辑: 我让它工作了!:下面是代码,

我试图编写简单的客户机和服务器C程序,在不同的终端上相互通信

服务器必须创建公共fifo并等待客户端。与此同时,客户机正在创建自己的fifo,通过fifo,服务器将得到响应。客户端的任务是向服务器发送由队列创建的名称,并返回
ls
命令的结果

我确实在寻找答案,例如:。我从第三个链接开始编写代码,然后慢慢修改

我现在得到的是一个客户端从用户那里获取输入,将其发送到服务器并接收回来。但它只起作用一次。我不知道为什么。主要功能的主体如下。如果有任何帮助,我将不胜感激

编辑: 我让它工作了!:下面是代码,也许它会帮助别人

server.c代码:

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

int main(int argc, char* argv[])
{
    int fds[2];
    char tab[BUFSIZ];
    int fd, n;

    char *myfifo = "/tmp/serwer";
    char *myfifo2 = "/tmp/client";

    pipe(fds);
    mkfifo(myfifo,0666);

    while(1)
    {
        fds[0]=open(myfifo2,O_RDONLY);
        fds[1]=open(myfifo,O_WRONLY);

        read(fds[0],tab,BUFSIZ);

        if (strcmp("klient",tab)==0) {
            printf("Od klienta: %s\n",tab);
            fd=open(tab,O_WRONLY);

            if(fork()==0)
            {
                dup2(fds[1],1);
                close(fds[1]);
                execlp("ls","ls","-l",NULL);
                close(fds[0]);
                close(fds[1]);
            }
            else
            {
                dup2(fds[0],0);
                n = read(fds[0],tab,BUFSIZ);
                write(fd,tab,n);
                close(fds[0]);
                close(fds[1]);
            }
        }
        memset(tab, 0, sizeof(tab));
        close(fd);
        close(fds[0]);
        close(fds[1]);
    }

    unlink(myfifo);
    return 0;
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
int-fds[2];
字符标签[BUFSIZ];
int-fd,n;
char*myfifo=“/tmp/serwer”;
char*myfifo2=“/tmp/client”;
管道(fds);
mkfifo(myfifo,0666);
而(1)
{
fds[0]=打开(仅限myfifo2和Ordu);
fds[1]=打开(仅限myfifo、O_WRONLY);
读取(fds[0],选项卡,BUFSIZ);
如果(strcmp(“klient”,tab)==0){
printf(“Od klienta:%s\n”,选项卡);
fd=打开(仅限制表符O_);
如果(fork()==0)
{
dup2(fds[1],1);
关闭(fds[1]);
execlp(“ls”、“ls”、“-l”、NULL);
关闭(fds[0]);
关闭(fds[1]);
}
其他的
{
dup2(fds[0],0);
n=读取(fds[0],制表符,BUFSIZ);
写入(fd,tab,n);
关闭(fds[0]);
关闭(fds[1]);
}
}
memset(tab,0,sizeof(tab));
关闭(fd);
关闭(fds[0]);
关闭(fds[1]);
}
取消链接(myfifo);
返回0;
}
client.c代码:

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

int main(int argc, char* argv[])
{
    int fds[2];
    char *myfifo = "/tmp/serwer";
    char *myfifo2 = "/tmp/client";

    mkfifo(myfifo2,0666);
    fds[0]=open(myfifo,O_RDONLY);
    fds[1]=open(myfifo2,O_WRONLY);

    char tab[BUFSIZ];
    memset(tab, 0, sizeof(tab));

    write(fds[1],"klient",6);
    perror("Write:"); //Very crude error check
    read(fds[0],tab,sizeof(tab));
    perror("Read:"); // Very crude error check

    printf("Odebrano od serwera: %s\n",tab);

    close(fds[0]);
    close(fds[1]);
    unlink(myfifo2);
    return 0;
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
int-fds[2];
char*myfifo=“/tmp/serwer”;
char*myfifo2=“/tmp/client”;
mkfifo(myfifo20666);
fds[0]=打开(仅限myfifo、Ordu);
fds[1]=打开(仅限myfifo2、O_wr);
字符标签[BUFSIZ];
memset(tab,0,sizeof(tab));
写入(fds[1],“klient”,6);
perror(“Write:”;//非常粗糙的错误检查
读取(fds[0],制表符,尺寸(制表符));
perror(“Read:”;//非常粗糙的错误检查
printf(“Odebrano od serwera:%s\n”,选项卡);
关闭(fds[0]);
关闭(fds[1]);
取消链接(myfifo2);
返回0;
}

为什么不管理服务器中的两个fifo?只需更改代码即可使其正常工作

如果您真的想要建立客户机-服务器关系,服务器为许多不同的客户机提供服务,那么套接字可能是更好的选择

client.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);
   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("...received from the server: %s\n",str);
   close(client_to_server);
   close(server_to_client);

   /* remove the FIFO */

   return 0;
}
#包括
#包括
#包括
#包括
#包括
int main()
{
int客户端到服务器;
char*myfifo=“/tmp/client_to_server_fifo”;
int服务器到客户端;
char*myfifo2=“/tmp/server_to_client_fifo”;
char-str[BUFSIZ];
printf(“向serwer输入消息:”);
scanf(“%s”,str);
/*将str写入FIFO*/
客户端到服务器=打开(仅限myfifo、O_);
服务器到客户端=打开(仅限myfifo2、O\rdu);
写入(客户端到服务器,str,sizeof(str));
perror(“Write:”;//非常粗糙的错误检查
读取(服务器到客户端,str,sizeof(str));
perror(“Read:”;//非常粗糙的错误检查
printf(“…从服务器接收:%s\n”,str);
关闭(客户端到服务器);
关闭(服务器到客户端);
/*卸下FIFO*/
返回0;
}
server.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;
}
#包括
#包括
#包括
#包括
#包括
int main()
{
int客户端到服务器;
char*myfifo=“/tmp/client_to_server_fifo”;
int服务器到客户端;
char*myfifo2=“/tmp/server_to_client_fifo”;
char buf[BUFSIZ];
/*创建FIFO(命名管道)*/
mkfifo(myfifo,0666);
mkfifo(myfifo20666);
/*打开、读取并显示来自FIFO的信息*/
客户端到服务器=打开(仅限myfifo、O\RDU);
服务器到客户端=打开(仅限myfifo2、O\u);
printf(“服务器打开。\n”);
而(1)
{
读取(客户端到服务器、buf、BUFSIZ);
如果(strcmp(“退出”,buf)==0)
{
printf(“服务器关闭。\n”);
打破
}
否则如果(strcmp(“,buf)!=0)
{
printf(“收到:%s\n”,buf);
printf(“发送回…\n”);
写入(服务器到客户端、buf、BUFSIZ);
}
/*清除buf中的任何数据*/
memset(buf,0,sizeof(buf));
}
关闭(客户端到服务器);
关闭(服务器到客户端);
取消链接(myfifo);
取消链接(myfifo2);
返回0;
}

由于命名管道的工作方式,它只工作一次。每次
打开
用于读取的命名管道时,都会阻塞,直到另一个进程打开它进行写入。然后您配对,文件描述符连接您的进程。一旦任何一端关闭该连接,即为该管道的末端。为了让您的服务器“接受另一个连接”,它需要将管道的
打开
关闭
移动到其主回路中,以便可以一次又一次地配对。

从该代码中看不出这一点。你有没有让ls输出正常工作,或者你只是发送小的MSG?这种情况下常见的陷阱是死锁,即服务器在等待输入,而客户端在等待输入。他们一直在等待,因为没有人发送任何东西。不,ls命令还没有工作。这些程序工作一次-服务器等待消息,将其返回给客户端,客户端可以关闭。当我想发送另一条消息时,客户端和服务器都会失去响应。