资源简介
/**************************************************************************/
//本程序是利用LINUX FIFO命名管道技术实现双向聊天的C语言源代码。
//优点:代码简洁明了。
//其中:
//chat.c: 聊天源代码。
//makefile: 利用宏定义,把一个源码生成两个不同的可执行程序。
//
//使用:
//make clean 清除上次编译生成的结果文件。
//make 重新编译生成两个可执行程序。./a_chat和./b_chat的执行顺序没有先后。
//作者:david.q@sz 2012-8-11 2263537@qq.com
/**************************************************************************/

代码片段和文件信息
/**************************************************************************/
//本程序是利用LINUX FIFO命名管道技术实现双向聊天的C语言源代码。
//优点:代码简洁明了。
//其中:
//chat.c: 聊天源代码。
//makefile: 利用宏定义,把一个源码生成两个不同的可执行程序。
//
//使用:
//make clean 清除上次编译生成的结果文件。
//make 重新编译生成两个可执行程序。./a_chat和./b_chat的执行顺序没有先后。
//作者:david.q@sz 2012-8-11 2263537@qq.com
/**************************************************************************/
#include
#include
#include
#include
#include
#include
#include
int main( int argc char *argv[] )
{
#ifdef A
char me[] = “a“;
char you[] = “b“;
#else
char me[] = “b“;
char you[] = “a“;
#endif
char buf[100];
char get[100];
printf( “Hello %s!\n“ me ); //print welcome message
pid_t pid;
pid = fork();
if ( pid == 0 ) //child process
{
int fifo_f;
#ifdef A
char fifo[] = “/tmp/b2a“;
#else
char fifo[] = “/tmp/a2b“;
#endif
mkfifo( fifo 0666 ); //creat fifo if exist or not
if ( errno == EEXIST )
{
fifo_f = open( fifo O_RDONLY ); //wait & open fifo
}
//read
while ( 1 )
{
bzero( get sizeof(get)); //set buf to ‘\0‘ before read buf
read( fifo_f get sizeof(get) ); //read buf from another side
if ( strlen(get) )
{
printf( “\r%s: %s“ you get );
printf( “%s: “ me );
fflush(stdout); //print buf to screen
usleep(100);
}
}
}
else //parent process
{
int fifo_f;
#ifdef A
char fifo[] = “/tmp/a2b“;
#else
char fifo[] = “/tmp/b2a“;
#endif
mkfifo( fifo 0666 ); //creat fifo if exist or not
if ( errno == EEXIST )
{
fifo_f = open( fifo O_WRONLY ); //wait & open fifo
}
//write
while ( 1 )
{
printf( “%s: “ me );
fflush(stdout); //print to screen
bzero( buf sizeof(buf) ); //set buf to ‘\0‘ before get from stdin
fgets( buf sizeof(buf) stdin ); //get buf from stdin
write( fifo_f buf sizeof(buf) ); //send buf to anoterh side
usleep(100);
}
}
return 0;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2187 2012-08-12 16:57 chat.c
文件 179 2012-08-12 12:00 Makefile
----------- --------- ---------- ----- ----
2366 2
- 上一篇:读取sgy卷头,道头及数据
- 下一篇:3DES,C语言版,ECB,PKCS7
相关资源
- LINUX下命令行界面的C语言细胞游戏
- 尚观培训linux许巍老师关于c语言的课
- VC++实现CMD命令执行与获得返回信息
- 《Linux程序设计》第四版pdf高清电子版
- linux 0.11内核代码
- linux ac108多麦方案驱动(ac108.c)
- 共享内存 读写
- 简易web服务器的设计与实现
- 《LINUX C编程从初学到精通》光盘源码
- Linux那些事儿之我是USB core
- Linux c语言 学生成绩管理系统
- Linux开发工具手册
- Linux操作系统下C语言编程从零开始
- 基于Linux下C语言开发的员工管理系统
- 超级玛丽c++源码win32Linux平台
- 页面置换算法(fifolruopt) C语言编写
- UNIX/LINUX下C语言中文短信UCS2编码和解
- 页式存储管理FIFO实现
- 嵌入式工程师必知必会 (完整高清中
- linux-2.6.24.rar
- Linux下C语言操作静态ARP表,包括增加
- c语言 linux 贪吃蛇.doc
- Linux多线程服务端编程:使用muduo C+
- libstdc++.so.6.0.23_linux7
- libstdc++-devel-4.1.2-48.el5.x86_64
- C++版仿Linux文件管理系统
- 基于SDL的贪吃蛇游戏
- 链表栈的基本操作(C语言
- 基于linux C/C++和Qt的聊天程序
- Linux+gladeGTK++C语言+mysql的模仿QQ聊天工
评论
共有 条评论