SOCKET 网络编程:Linu x 下实现聊天室 程序介绍:本聊天室程序在Ubu ntu 下,采用C 语言实现,结构为Client/Serv er 结构; 服务端程序通过共享存储区存储聊天数据,并发送给每个连接的客户端; 服务端程序和客户端程序都是通过父子进程分别负责发送和接收数据的,以避免数据冲撞; 需按以下格式调用客户端程序:client.ex e 服务端主机IP 端口号(本程序设定为:3490) 用户名(在聊天室中显示的用户名)。 程序截图: //--------------------------------服务端---------------------------------------------- //--------------------------------客户端1:真水无香-------------------------------------- //--------------------------------客户端2:蜡笔小新-------------------------------------- 程序代码如下: //--------------------------------server.c-------------------------------------------------- //包含工程所需的头文件 #include #include #include //数据类型定义 #include #include //定义数据结构sockaddr_in #include //提供socket函数及数据结构 #include #include #include #include #include #include #include #define PERM S_IRUSR|S_IWUSR #define MYPORT 3490 //宏定义定义通信端口 #define BACKLOG 10 //宏定义,定义服务程序可以连接的最大客户数量 #define WELCOME "|----------Welcome to the chat room! ----------|" //宏定义,当客户端连接服务端时,想客户发送此欢迎字符串 //转换函数,将int类型转换成char *类型 void itoa(int i,char*string) { int power,j; j=i; for(power=1;j>=10;j/=10) power*=10; for(;power>0;power/=10) { *string++='0'+i/power; i%=power; } *string='\0'; } //得到当前系统时间 void get_cur_time(char * time_str) { time_t timep; struct tm *p_curtime; char *time_tmp; time_tmp=(char *)malloc(2); memset(time_tmp,0,2); memset(time_str,0,20); time(&timep); p_curtime = localtime(&timep); strcat(time_str," ("); itoa(p_curtime->tm_hour,time_tmp); strcat(time_str,time_tmp); strcat(time_str,":...