实际是用socket方法即可实现这个功能,具体代码看如下服务端:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <<a href="https://www.baidu.com/s?wd=sys%2Ftypes.h&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3mHuWrAmkuHmsnynYrjbk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPWfvPjm1rj0knHbvnjn3PW6d" target="_blank" class="baidu-highlight">sys/types.h</a>> #include <unistd.h> #include <sys/time.h> #define BUFLEN 1024 #define PORT 6666#define LISTNUM 20 int main() { int sockfd, newfd; struct sockaddr_in s_addr, c_addr; char buf[BUFLEN]; socklen_t len; unsigned int port, listnum; fd_set rfds; struct timeval tv; int retval,maxfd; /*建立socket*/ if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1){ perror("socket"); exit(errno); }else printf("socket create success!
"); memset(&s_addr,0,sizeof(s_addr)); s_addr.sin_family = AF_INET; s_addr.sin_port = htons(PORT); s_addr.sin_addr.s_addr = htons(INADDR_ANY); /*把地址和端口帮定到<a href="https://www.baidu.com/s?wd=%E5%A5%97%E6%8E%A5%E5%AD%97&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3mHuWrAmkuHmsnynYrjbk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPWfvPjm1rj0knHbvnjn3PW6d" target="_blank" class="baidu-highlight">套接字</a>上*/ if((bind(sockfd, (struct sockaddr*) &s_addr,sizeof(struct sockaddr))) == -1){ perror("bind"); exit(errno); }else printf("bind success!
"); /*侦听本地端口*/ if(listen(sockfd,listnum) == -1){ perror("listen"); exit(errno); }else printf("the server is listening!
"); while(1){ printf("*****************聊天开始***************
"); len = sizeof(struct sockaddr); if((newfd = accept(sockfd,(struct sockaddr*) &c_addr, &len)) == -1){ perror("accept"); exit(errno); }else printf("正在与您聊天的客户端是:%s: %d
",inet_ntoa(c_addr.sin_addr),ntohs(c_addr.sin_port)); while(1){ FD_ZERO(&rfds); FD_SET(0, &rfds); maxfd = 0; FD_SET(newfd, &rfds); /*找出<a href="https://www.baidu.com/s?wd=%E6%96%87%E4%BB%B6%E6%8F%8F%E8%BF%B0%E7%AC%A6&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3mHuWrAmkuHmsnynYrjbk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPWfvPjm1rj0knHbvnjn3PW6d" target="_blank" class="baidu-highlight">文件描述符</a>集合中最大的<a href="https://www.baidu.com/s?wd=%E6%96%87%E4%BB%B6%E6%8F%8F%E8%BF%B0%E7%AC%A6&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3mHuWrAmkuHmsnynYrjbk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPWfvPjm1rj0knHbvnjn3PW6d" target="_blank" class="baidu-highlight">文件描述符</a>*/ if(maxfd < newfd) maxfd = newfd; /*设置超时时间*/ tv.tv_sec = 6; tv.tv_usec = 0; /*等待聊天*/ retval = select(maxfd+1, &rfds, NULL, NULL, &tv); if(retval == -1){ printf("select出错,与该客户端连接的程序将退出
"); break; }else if(retval == 0){ printf("waiting...
"); continue; }else{ /*用户输入信息了*/ if(FD_ISSET(0, &rfds)){ /******发送消息*******/ memset(buf,0,sizeof(buf)); /*fgets函数:从流中读取BUFLEN-1个字符*/ fgets(buf,BUFLEN,stdin); /*打印发送的消息*/ //fputs(buf,stdout); if(!strncasecmp(buf,"quit",4)){ printf("server 请求终止聊天!
"); break; } len = send(newfd,buf,strlen(buf),0); if(len > 0) printf(" 消息发送成功:%s
",buf); else{ printf("消息发送失败!
"); break; } } /*客户端发来了消息*/ if(FD_ISSET(newfd, &rfds)){ /******接收消息*******/ memset(buf,0,sizeof(buf)); /*fgets函数:从流中读取BUFLEN-1个字符*/ len = recv(newfd,buf,BUFLEN,0); if(len > 0) printf("客户端发来的信息是:%s
",buf); else{ if(len < 0 ) printf("接受消息失败!
"); else printf("客户端退出了,聊天终止!
"); break; } }