//-------------client.c------------------------------------------------------ #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <unistd.h> #define TRUE 1 #define PORT 6555 static int sockfd; void recvfromserver() //接受服务器消息线程入口函数 { char mes[1024]; int nbytes=0; while(1) { memset(mes,0,sizeof(mes)); nbytes=read(sockfd,mes,sizeof(mes)); if(nbytes>0) { mes[nbytes]='\0'; printf("%s\n",mes); } } pthread_exit(NULL); } int main(int argc, char *argv[]) { // int sockfd; char buffer[1024]; struct sockaddr_in server_addr; struct hostent *host; int portnumber,nbytes; char strhost[16]; char clientname[20]; char mes[1024]; int thr_id; /* thread ID for the newly created thread */ pthread_t p_thread; /* thread's structure */ if(argc!=1) { fprintf(stderr,"Usage:%s \a\n",argv[0]); exit(1); } printf("请输入服务器ip地址\n"); //scanf("%s",strhost); if((host=gethostbyname("127.0.0.1"))==NULL) { fprintf(stderr,"Gethostname error\n"); exit(1); } /* 客户程序开始建立 sockfd 描述符 */ printf("正在建立套接口...\n"); if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"Socket Error:%s\a\n",strerror(errno)); exit(1); } /* 客户程序填充服务端的资料 */ bzero(&server_addr,sizeof(server_addr)); server_addr.sin_family=AF_INET; server_addr.sin_port=htons(PORT); server_addr.sin_addr=*((struct in_addr *)host->h_addr); printf("套接口创建成功,正在链接服务器...\n"); /* 客户程序发起连接请求 */ if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) { fprintf(stderr,"Connect Error:%s\a\n",strerror(errno)); exit(1); } /* 连接成功了 */ printf("链接服务器成功\n欢迎来到聊天室\n"); printf("请输入你的用户昵称\n"); scanf("%s",clientname); // write(sockfd,clientname,sizeof(clientname)); printf("\n\n开始聊天吧(\"Quit\"断开连接)\n\n"); thr_id = pthread_create(&p_thread, NULL, recvfromserver, NULL); while(1) { memset(buffer,0,sizeof(buffer)); memset(mes,0,sizeof(mes)); gets(buffer); strcat(mes,clientname); strcat(mes,":"); strcat(mes,buffer); // printf("main thread %s\n",mes); if((write(sockfd,mes,sizeof(mes)))==-1) { fprintf(stderr,"Write Error:%s\n",strerror(errno)); exit(1); } if(strcmp(buffer,"Quit")==0) { break; } } /* 结束通讯 */ close(sockfd); exit(0); }
//-----------------server.c-------------- #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #define MAXLINE 1000 //在一条消息中最大的输出字符数 #define LISTENQ 20 //最大监听队列 #define PORT 6555 //监听端口 #define MAXFD 20 //最大的在线用户数量 void *get_client(void *); int sockfd,i; static int maxi=0;//maxi表示当前client数组中最大的用户的i值 static int client[MAXFD]; void recvandsend(void * arg) //监听转发线程入口函数 { printf("hello new thread\n"); int index=(int)arg; int nbytes=0; char buffer[1024]; int len; int outindex=0; printf("Listen Client[%d]", index); while(1) { memset(buffer,0,sizeof(buffer)); nbytes=0; //index++; nbytes=read(client[index],buffer,sizeof(buffer)); // printf("%d,%d\n",index,client[index]); if(nbytes>0) { buffer[nbytes]='\0'; printf(" %s\n",buffer); outindex=0; while(outindex<maxi) if(write(client[outindex++],buffer,sizeof(buffer))==-1) { fprintf(stderr,"Write Error:%s\n",strerror(errno)); exit(1); } } } pthread_exit(NULL); } int main(int argc, char *argv[]) { // int client_fd[LISTENQ],clientnum=0;; printf("server port has been open\a\n"); struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size,portnumber; char hello[]="Hello! Are You Fine?\n"; int thr_id[10]; /* thread ID for the newly created thread */ pthread_t p_thread[10]; /* thread's structure */ int new_fd=0; memset(client,0,sizeof(client)); if(argc!=1) { fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]); exit(1); } /* 服务器端开始建立 socket 描述符 */ if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"Socket error:%s\n\a",strerror(errno)); exit(1); } /* 服务器端填充 sockaddr 结构 */ bzero(&server_addr,sizeof(struct sockaddr_in)); server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htonl(INADDR_ANY); server_addr.sin_port=htons(PORT); /* 捆绑 sockfd 描述符 */ if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) { fprintf(stderr,"Bind error:%s\n\a",strerror(errno)); exit(1); } printf("The server port has been opened%d...\n",PORT); /* 监听 sockfd 描述符 */ if(listen(sockfd,LISTENQ)==-1) { fprintf(stderr,"Listen error:%s\n\a",strerror(errno)); exit(1); } printf("Welcome to this chatroom\n"); while(1) { /* 服务器阻塞,直到客户程序建立连接 */ if(maxi>=20) { printf("The number of the people was too much,Please wait while !以达到人数上线\a\n"); continue; } sin_size=sizeof(struct sockaddr_in); if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1) { fprintf(stderr,"Accept error:%s\n\a",strerror(errno)); exit(1); } /*fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));*/ client[maxi]=new_fd; thr_id[maxi] = pthread_create(&p_thread[maxi], NULL, recvandsend, maxi); maxi++; printf("\n新用户进入聊天室%d\n",new_fd); } close(sockfd); exit(0); }
if you want running the coding,please according to the next guide:
->gcc -o server ../server.c -lpthread
->./server
->ggc -o client ../client.c -lpthread
->./client
相关推荐
As in earlier Addison-Wesley books on the UNIX-based BSD operating system, Kirk McKusick and George Neville-Neil deliver here the most comprehensive, up-to-date, and authoritative technical ...
Architectural Styles and the Design of Network-based Software Architectures,对基于网络的软件架构设计以及架构风格进行了拓展,并主要揭示了软件架构的一些原理性的东西
The text begins with a bare machine and proceeds step-by-step through the design and implementation of Xinu, which is a small, elegant operating system that supports dynamic process creation, dynamic ...
the LAND System Cellular Automata model for Potential Effects (LANDSCAPE), with a hierarchical allocation strategy and resistances, to simulate changes in multiple land uses. Furthermore, we ...
《Model Predictive Control System Design and Implementation Using MATLAB》这本书全面地介绍了MPC的基本概念、最新进展以及如何利用MATLAB进行模拟和实现。 #### 二、MPC的特点与优势 1. **设计公式化**:MPC...
### 基于PLC的工业控制系统设计与实施 #### 1. 现代工业控制技术 ##### 1.1 工业控制与PLC技术 工业控制技术是计算机科学、控制理论、仪器仪表及其他信息技术在工业生产过程中的应用,以实现检测、控制、优化、...
基于RBAC角色访问模型的权限系统研究与实现,梁勇超,刘培值,RBAC (Role-Based Access Control, RBAC) role-based access technology is the study of R & D results funded by the U.S. Department of Defense ...
与ASP.NET相关的外文翻译,总体介绍了基于ASP.NET的网络发展
ebtables-iptables interaction on a Linux-based bridge.mht
# IEEE 802.11 OFDM-based transceiver system This repos contains the implementation of IEEE 802.11 (i.e. Wifi) OFDM-based transceiver system. This is stored in 2 separate parts, i.e. transmitter (TX) ...
Real-Time BCI System Design to Control Arduino Based Speed Controllable Robot Using EEG (SpringerBriefs in Applied Sciences and Technology) By 作者: Swagata Das ISBN-10 书号: 9811330972 ISBN-13 书号: ...
Design and Implementation of RISC I Design and Implementation of RISC I
Model Predictive Control System Design and Implementation Using MATLAB(R) proposes methods for design and implementation of MPC systems using basis functions that confer the following advantages: - ...
Eclipse IDE for RCP and RAP Developers(eclipse-rcp-2022-06-R-linux-gtk-x86_64.tar.gz) 适用于Linux x86_64: A complete set of tools for developers who want to create Eclipse plug-ins, Rich Client ...
The Art of Linux Kernel Design Illustrating the Operating System Design Principle and Implementation Uses the Running Operation as the Main Thread Difficulty in understanding an operating system &#...
Steven X. Ding Model-based Fault Diagnosis Techniques- Design Schemes Algorithms and Tools 2008