[英]console based chat application in c
我創建了該應用程序,其中兩個進程進行了通信。 一切都很好,但是我希望當用戶按下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);
}
第二個過程(我稱為客戶端)
#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
返回零。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.