如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
SOCKET网络编程:Linux下实现聊天室程序介绍:本聊天室程序在Ubuntu下,采用C语言实现,结构为Client/Server结构;服务端程序通过共享存储区存储聊天数据,并发送给每个连接的客户端;服务端程序和客户端程序都是通过父子进程分别负责发送和接收数据的,以避免数据冲撞;需按以下格式调用客户端程序:client.exe服务端主机IP端口号(本程序设定为:3490)用户名(在聊天室中显示的用户名)。程序截图://--------------------------------服务端----------------------------------------------//--------------------------------客户端1:真水无香--------------------------------------//--------------------------------客户端2:蜡笔小新--------------------------------------程序代码如下://--------------------------------server.c--------------------------------------------------//包含工程所需的头文件#include<stdio.h>#include<stdlib.h>#include<sys/types.h>//数据类型定义#include<sys/stat.h>#include<netinet/in.h>//定义数据结构sockaddr_in#include<sys/socket.h>//提供socket函数及数据结构#include<string.h>#include<unistd.h>#include<signal.h>#include<sys/ipc.h>#include<errno.h>#include<sys/shm.h>#include<time.h>#definePERMS_IRUSR|S_IWUSR#defineMYPORT3490//宏定义定义通信端口#defineBACKLOG10//宏定义,定义服务程序可以连接的最大客户数量#defineWELCOME"|----------Welcometothechatroom!----------|"//宏定义,当客户端连接服务端时,想客户发送此欢迎字符串//转换函数,将int类型转换成char*类型voiditoa(inti,char*string){intpower,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';}//得到当前系统时间voidget_cur_time(char*time_str){time_ttimep;structtm*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,":");itoa(p_curtime->tm_min,time_tmp);strcat(time_str,time_tmp);strcat(time_str,":");itoa(p_curtime->tm_sec,time_tmp);strcat(time_str,time_tmp);strcat(time_str,")");free(time_tmp);}//创建共享存储区key_tshm_create(){key_tshmid;//shmid=shmget(IPC_PRIVATE,1024,PERM);if((shmid=shmget(IPC_PRIVATE,1024,PERM))==-1){fprintf(stderr,"CreateShareMemoryError:%s\n\a",strerror(errno));exit(1);}returnshmid;}//端口绑定函数,创建套接字,并