From dea97174128a992c23df917ec8326343fdf9eef4 Mon Sep 17 00:00:00 2001 From: flykhan Date: Thu, 17 Aug 2023 18:18:20 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=A1=E9=81=93-=E8=BF=9B=E7=A8=8B=E6=A1=88?= =?UTF-8?q?=E4=BE=8B:=20=E5=8D=95=E6=9C=BA=E8=81=8A=E5=A4=A9=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day4/chat_tool/chat_tool | Bin 0 -> 13392 bytes day4/chat_tool/chat_tool.c | 130 +++++++++++++++++++++++++++++++++++++ day4/chat_tool/readme.md | 53 +++++++++++++++ 3 files changed, 183 insertions(+) create mode 100755 day4/chat_tool/chat_tool create mode 100644 day4/chat_tool/chat_tool.c create mode 100644 day4/chat_tool/readme.md diff --git a/day4/chat_tool/chat_tool b/day4/chat_tool/chat_tool new file mode 100755 index 0000000000000000000000000000000000000000..11d962b463c9bfd64409c60e94b07fb2c075bf15 GIT binary patch literal 13392 zcmeHOeQ;FO6~DWi@XaP5V)#&=7HmXVHVA3Ns=G-RUYPJ91jN$t*km_kCD~1P-$I~8 z7neBfgkZE#nU2mdBicGTqMcDwKLSh`Qm2lM)3J6cR&0Up8f@dxGHF$|=iGbG=Ivv5 zM*nI5$<4fX?)jaMd+)g)Z|{3I4|{5tTP+qw$;$3w$i>FGI3(_jbuZwo#46Z$=3;Z$ zY&HgvLbzO9f>h*)4olZW$4NaK&;qzBtOBU8x_E}@Yym?FB2f*IVtFIUC9{I=D-tYG z#uN^q9oDt@Ce8xg$}>b`vL5>-B|Bu%u2kBUN;{$hvSLK3eb_d<>ZHFqs|z^fT}W6p zzmHJV#kBTLCErDRPePMe=`NHxgj!4L{n714Kqwb2;8+b_+M+G>n^E9|{450kV$8%4g-2C+F z-`#Z2@4vhG)>mJeT=^rkr3u`O-t~F#iFt7Jk;@N*es08WEoW{1a5xlZO`-61rf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TEVgI&3ME8d^y;XygK|y~6G<+d# zTT7_H+oJjOP&lG_qwTCQ)Y{hK*Zn@n_mS919}_bQL(SzQga6ji2tmZ6Vn)eo$8ud1m6ErWy~Vgd+G zwrL(!6W-M7bxd1PyQb1ztF2kSe7&bmt8-V@dawk3^r$dtdmw# zw@MocUEQiG3|yVJp~VkJts4D-9sX*s?uDrPy}Exn9;mveS~emMyJchA${pG|IcTd|ypaeWvCs!JSg>3)a1vSy*~-6pwh!KkAl z8i4bcaIVgQT(dXQ%pAU5LGUN29u|@v{%{1(;&C;}XdnywTfAr>b8Rg;bMT-!AnVu` z0#^68!yV@qD24c_arm3%Y-;v_6XJxam{!7q@((tG8WiJg4KzX>LOQw!1c#sldWSh+ zg15q?&9Q8KYY2ZI;gSl=7f%&dm!zEV@4+G`?sECfK;MBJ?(N8)_6Ky1SIKCK3_Ujp zE`yBGp7s&CbU2q3B{kbu1AYbef>izytBY$MkpS(J3pqNnUzRLe4H?|~l6{ZW1st3k zlK?+C2R!bdvF+x4>qfv}DbLu`{zZ`$eFkL4WRBk!5aQmJ%BOwKX=#6xG^YBK9?|U} z$GtM~v>!@Jd#AKF*I&X6f4`zC+0(wML>5>g6{&o4`}axvN~uTteWx^__XEhHFSGqY zz%V9QKTNa_<0lM(r}rLQ2sha*Vp#%mMtjU` zFE@glnSa`3Prqwo(*6kwoOD3tl0O_D#`0;OI8DAFqW2=CZ*Kqd(w_Q{exE7Q{uCKX zYOcT7UySrWotvlg^tC2?bNON)GGb5rQ>R@R)?woe$z1+BAVXE0Lzrm4LFenu`eyzE zuqlFz>}kJSA}5Yn-^_mswpc~7r+u;V4ec+2Ee zZbfLA^<_AO5bXd!%$Y$%hV1!qb%TuTiB16-m2FUCdv6yUo*^sHVMt;{}&bkdD;K~ literal 0 HcmV?d00001 diff --git a/day4/chat_tool/chat_tool.c b/day4/chat_tool/chat_tool.c new file mode 100644 index 0000000..950bd50 --- /dev/null +++ b/day4/chat_tool/chat_tool.c @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +// 创建FIFO +int createFifo(const char *fifoName) +{ + if (mkfifo(fifoName, 0644) == -1) // 创建FIFO + { + if (errno != EEXIST) + { + perror(fifoName); // 输出错误信息 + return 1; + } + } + return 0; +} + +// 发送数据 +void sendData(const char *fifoName, const char *sender) +{ + int fd = open(fifoName, O_WRONLY); // 打开FIFO以进行写操作 + while (1) + { + char buf[128] = {}; + int len = read(STDIN_FILENO, buf, 128); // 从标准输入读取数据 + buf[len - 1] = '\0'; // 去除换行符 + write(fd, buf, len); // 写入FIFO + if (strcmp(buf, "bye") == 0) // 如果输入为"bye",则退出循环 + { + break; + } + } + close(fd); // 关闭FIFO + printf("%s 发送完毕\n", sender); // 输出发送方的信息 +} + +// 接收数据 +void receiveData(const char *fifoName, const char *receiver) +{ + int fd = open(fifoName, O_RDONLY); // 打开FIFO以进行读操作 + while (1) + { + char buf[128] = ""; + int len = read(fd, buf, 128); // 从FIFO读取数据 + if (len <= 0) + break; + printf("接收 %s: %s\n", receiver, buf); // 输出接收到的数据和接收方的信息 + } + close(fd); // 关闭FIFO + printf("%s 接收完毕\n", receiver); // 输出接收方的信息 +} + +int main(int argc, char const *argv[]) +{ + const char *fifo_bob_to_lucy = "fifo_bob_to_lucy"; // Bob到Lucy的FIFO名称 + const char *fifo_lucy_to_bob = "fifo_lucy_to_bob"; // Lucy到Bob的FIFO名称 + + // 创建FIFO + if (createFifo(fifo_bob_to_lucy) != 0) // 创建Bob到Lucy的FIFO + return 1; + + if (createFifo(fifo_lucy_to_bob) != 0) // 创建Lucy到Bob的FIFO + return 1; + + if (argc != 2) + { + printf("Useage: ./chat_tool [角色名: lucy | bob]"); + return 1; + } + + if (strcmp(argv[1], "lucy") != 0 && strcmp(argv[1], "bob") != 0) + { + printf("用户仅可选择 lucy 或 bob"); + return 1; + } + + int i = 0; + for (; i < 2; i++) + { + int pid = fork(); // 创建子进程 + if (pid == 0) + break; + } + + if (i == 0) + { + // 第一个子进程 + if (strcmp(argv[1], "lucy") == 0) // 如果是Lucy方 + { + sendData(fifo_lucy_to_bob, "Lucy"); // 发送数据给Bob + } + else if (strcmp(argv[1], "bob") == 0) // 如果是Bob方 + { + sendData(fifo_bob_to_lucy, "Bob"); // 发送数据给Lucy + } + } + else if (i == 1) + { + // 第二个进程,读数据 + if (strcmp(argv[1], "lucy") == 0) // 如果是Lucy方 + { + receiveData(fifo_bob_to_lucy, "Lucy"); // 接收来自Bob的数据 + } + else if (strcmp(argv[1], "bob") == 0) // 如果是Bob方 + { + receiveData(fifo_lucy_to_bob, "Bob"); // 接收来自Lucy的数据 + } + } + else + { + // 主进程 + while (1) + { + int pid_ = waitpid(0, NULL, WNOHANG); // 等待所有子进程退出 + if (pid_ == -1) + { + // 所有子进程都已经退出 + break; + } + } + } + + return 0; +} diff --git a/day4/chat_tool/readme.md b/day4/chat_tool/readme.md new file mode 100644 index 0000000..89372d0 --- /dev/null +++ b/day4/chat_tool/readme.md @@ -0,0 +1,53 @@ +使用文档:chat_tool.c + +该程序是一个聊天工具,允许两个角色("lucy" 和 "bob")之间进行消息的发送和接收。 + +用法: +./chat_tool [角色名: lucy | bob] + +参数说明: + +- 角色名:指定聊天的角色,可选的值为 "lucy" 或 "bob"。 + +注意事项: + +- 在运行程序之前,请确保已经创建了相应的 FIFO(命名管道)。 +- 创建 FIFO 的函数为 `createFifo(const char *fifoName)`,请确保正确调用该函数。 +- FIFO 的名称分别为 "fifo_bob_to_lucy" 和 "fifo_lucy_to_bob"。 + +示例用法: + +1. 创建 FIFO: + $ ./chat_tool bob + $ ./chat_tool lucy + +2. 运行程序: + + - Bob 方发送消息给 Lucy 方: + $ ./chat_tool bob + + - Lucy 方发送消息给 Bob 方: + $ ./chat_tool lucy + +3. 聊天操作: + - 在程序运行后,输入消息并按下回车键发送。 + - 输入 "bye" 退出聊天。 + +注意事项: + +- 请确保在两个不同的终端窗口或会话中分别运行 Bob 方和 Lucy 方。 +- Bob 方和 Lucy 方可以同时发送和接收消息。 + +示例: + +- Bob 方发送消息给 Lucy 方: + $ ./chat_tool bob + Hello Lucy! + How are you? + bye + +- Lucy 方发送消息给 Bob 方: + $ ./chat_tool lucy + Hi Bob! + I'm good. How about you? + bye