gpt4 book ai didi

c中基于控制台的聊天应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 07:29:15 25 4
gpt4 key购买 nike

我创建了这个应用程序,两个进程在其中进行通信。一切都好但我希望当用户按下 esc 时进程自动结束。s*其次,它只从用户那里得到一行。在一个过程中一次*。在进入第二行之前,我们还必须向另一个进程添加一行。这是进程 1(我调用服务器)

的代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
extern int errno;
#define FIFO1 "/tmp/fifo.1"
#define FIFO3 "/tmp/fifo.3"
#define PERMS 0666
#define MESSAGE1 "client Says:"

main()
{
char buff[BUFSIZ];
int readfd, writefd;
int n, size;
if ((mknod (FIFO1, S_IFIFO | PERMS, 0) < 0) && (errno != EEXIST)) {
perror ("mknod FIFO1");
exit(1);
}
if (mkfifo(FIFO3, PERMS) < 0 && (errno != EEXIST)) {
unlink (FIFO1);
perror("mknod FIFO3");
exit(1);
}
if ((readfd = open(FIFO1, 0)) < 0) {
perror ("open FIFO1");
exit(1);
}
if ((writefd = open(FIFO3, 1)) < 0) {
perror ("open FIFO3");
exit(1);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
loop:
while(1)
{



if ((n = read(readfd, buff, 100)) < 0) {
perror ("server read"); exit (1);
}

write(1,MESSAGE1,strlen(MESSAGE1));
if (write(1, buff, n) != n) {
perror ("client write2"); exit(1);
}


/////////////////////////////////////////////////////////////////////////////////////////////


while(1)
{

printf("server says:");
//strcpy(buff,"I say:");
fgets(buff,100,stdin);
n=strlen(buff) + 1;
if (write(writefd, buff,n) < n) {
perror("server write1"); exit (1);
}
goto loop;
}

}//end of first for
close (readfd); close (writefd);
}

第二个过程(我叫client)

#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
extern int errno;
#define FIFO1 "/tmp/fifo.1"
#define FIFO3 "/tmp/fifo.3"
#define PERMS 0666
#define MESSAGE1 "server Says:"


main()
{
char buff[BUFSIZ];
char buf[]="logout";
int readfd, writefd, n, size;
if ((writefd = open(FIFO1, 1)) < 0) {
perror ("client open FIFO1"); exit(1);
}
if ((readfd = open(FIFO3, 0)) < 0) {
perror ("client open FIFO3"); exit(1);
}
///////////////////////////////////////////////////////////
loop:
while(1)
{

printf("client says:");
fgets(buff,100,stdin);
n=strlen(buff) + 1;
if (write(writefd, buff,n) < n)
{
perror("server write1"); exit (1);
}

////////////////////////////////////////////
while(1)
{


if ((n = read(readfd, buff, 100)) < 0)
{
perror ("client read"); exit(1);
}


write(1,MESSAGE1,strlen(MESSAGE1));
if (write(1, buff, n) != n)
{
perror ("client write2"); exit(1);
}


goto loop;
}
}//end of first for
close(readfd); close(writefd);
/* Remove FIFOs now that we are done using them */
if (unlink (FIFO1) < 0) {
perror("client unlink FIFO1");
exit(1);
}
if (unlink (FIFO3) < 0) {
perror("client unlink FIFO3");
exit(1);
}
exit(0);
}

最佳答案

如果我理解正确,您希望这两个程序是非阻塞的,即它们应该能够从或者用户或者 来自管道。

如果是这种情况,那么我建议您查看 select系统调用。它可用于轮询来自任意文件描述符的输入。

您可以执行类似于以下伪代码的操作:

while (1)
{
/* Poll for input */
select(...);

if (is_pipe_readable())
read_from_pipe_and_print_to_stdout();
else if (is_stdin_readable())
read_from_stdin_and_write_to_pipe();
}

请注意,文件描述符在关闭后变为可读。因此,如果管道的写入端关闭,则读取端变为可读,read 返回零。

关于c中基于控制台的聊天应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15381840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com