您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > Linux网络编程代码示例
SOCKET网络编程:Linux下实现聊天室程序介绍:本聊天室程序在Ubuntu下,采用C语言实现,结构为Client/Server结构;服务端程序通过共享存储区存储聊天数据,并发送给每个连接的客户端;服务端程序和客户端程序都是通过父子进程分别负责发送和接收数据的,以避免数据冲撞;需按以下格式调用客户端程序:client.exe服务端主机IP端口号(本程序设定为:3490)用户名(在聊天室中显示的用户名)。程序截图://--------------------------------服务端----------------------------------------------//--------------------------------客户端1:真水无香--------------------------------------//--------------------------------客户端2:蜡笔小新--------------------------------------程序代码如下://--------------------------------server.c--------------------------------------------------//包含工程所需的头文件#includestdio.h#includestdlib.h#includesys/types.h//数据类型定义#includesys/stat.h#includenetinet/in.h//定义数据结构sockaddr_in#includesys/socket.h//提供socket函数及数据结构#includestring.h#includeunistd.h#includesignal.h#includesys/ipc.h#includeerrno.h#includesys/shm.h#includetime.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(;power0;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;}//端口绑定函数,创建套接字,并绑定到指定端口intbindPort(unsignedshortintport){intsockfd;structsockaddr_inmy_addr;sockfd=socket(AF_INET,SOCK_STREAM,0);//创建基于流套接字my_addr.sin_family=AF_INET;//IPv4协议族my_addr.sin_port=htons(port);//端口转换my_addr.sin_addr.s_addr=INADDR_ANY;bzero(&(my_addr.sin_zero),0);if(bind(sockfd,(structsockaddr*)&my_addr,sizeof(structsockaddr))==-1){perror(bind);exit(1);}printf(bingsuccess!\n);returnsockfd;}intmain(intargc,char*argv[]){intsockfd,clientfd,sin_size,recvbytes;//定义监听套接字、客户套接字pid_tpid,ppid;//定义父子线程标记变量char*buf,*r_addr,*w_addr,*temp,*time_str;//=\0;//定义临时存储区structsockaddr_intheir_addr;//定义地址结构key_tshmid;shmid=shm_create();//创建共享存储区temp=(char*)malloc(255);time_str=(char*)malloc(20);sockfd=bindPort(MYPORT);//绑定端口while(1){if(listen(sockfd,BACKLOG)==-1)//在指定端口上监听{perror(listen);exit(1);}printf(listening......\n);if((clientfd=accept(sockfd,(structsockaddr*)&their_addr,&sin_size))==-1)//接收客户端连接{perror(accept);exit(1);}printf(acceptfrom:%d\n,inet_ntoa(their_addr.sin_addr));send(clientfd,WELCOME,strlen(WELCOME),0);//发送问候信息buf=(char*)malloc(255);ppid=fork();//创建子进程if(ppid==0){//printf(ppid=0\n);pid=fork();//创建子进程while(1){if(pid0){//父进程用于接收信息memset(buf,0,255);//printf(recv\n);//sleep(1);if((recvbytes=recv(clientfd,buf,255,0))=0){perror(recv1);close(clientfd);raise(SIGKILL);exit(1);}//writebuf'sdatatosharememoryw_addr=shmat(shmid,0,0);memset(w_addr,'\0',1024);strncpy(w_addr,buf,1024);get_cur_time(time_str);strcat(buf,time_str);printf(%s\n,buf);}elseif(pid==0){//子进程用于发送信息//scanf(%s,buf);sleep(1);r_addr=shmat(shmid,0,0);//printf(---%s\n,r_addr);//printf(cmp:%d\n,strcmp(temp,r_addr));if(strcmp(temp,r_addr)!=0){strcpy(temp,r_addr);get_cur_time(time_str);strcat(r_addr,time_str);//printf(discriptor:%d\n,clientfd);//if(send(clientfd,buf,strlen(buf),0)==-1)if(send(clientfd,r_addr,strlen(r_addr),0)==-1){perror(send);}memset(r_addr,'\0',1024);strcpy(r_addr,temp);}}elseperror(fork);}}}printf(------------------------------\n);free(buf);close(sockfd);close(clientfd);return0;}//-----------------------------client.c-------------------------------------------------//包含工程所需的头文件#includestdio.h#includenetinet/in.h//定义数据结构sockaddr_in#includesys/socket.h//提供socket函数及数据结构#includesys/types.h//数据类型定义#includestring.h#includestdlib.h#includenetdb.h#includeunistd.h#includesignal.h#includetime.hintmain(intargc,char*argv[]){structsockaddr_inclientaddr;//定义地址结构pid_tpid;intclientfd,sendbytes,recvbytes;//定义客户端套接字structhostent*host;char*buf,*buf_r;if(argc4){printf(usage:\n);printf(%shostportname\n,argv[0]);exit(1);}host=gethostbyname(argv[1]);if((clientfd=socket(AF_INET,SOCK_STREAM,0))==-1)//创建客户端套接字{perror(socket\n);exit(1);}//绑定客户端套接字clientaddr.sin_family=AF_INET;clientaddr.sin_port=htons((uint16_t)atoi(argv[2]));clientaddr.sin_addr=*((structin_addr*)host-h_addr);bzero(&(clientaddr.sin_zero),0);if(connect(clientfd,(structsockaddr*)&clientaddr,sizeof(structsockaddr))==-1)//连接服务端{perror(connect\n);exit(1);}buf=(char*)malloc(120);memset(buf,0,120);buf_r=(char*)malloc(100);if(recv(clientfd,buf,100,0)==-1){perror(recv:);exit(1);}printf(\n%s\n,buf);pid=fork();//创建子进程while(1){if(pid0){//父进
本文标题:Linux网络编程代码示例
链接地址:https://www.777doc.com/doc-7294721 .html