基于上次的改进版,实现了服务器的转发功能,基本实现了局域网聊天室的功能。
此版改进之处:
1、给client端开了一个线程,recv()函数在线程中单独处理,这样又可不必使用非阻塞的recv()了。
2、给服务器端成功accept()产生的confd加入一个链表中,当收到消息的时候,将转发标志flag置为1,转发的时候遍历链表,和接收到消息的confd不 同的就转发。
3、每当一个客户端退出时,收回线程id的同时,将此次连接的confd从链表中删除。
下面是代码:
//linklist.c
#include<stdio.h>
#include<stdlib.h>
typedef int elemType;
typedef struct listnode{
elemType info;
struct listnode * next;
}listnode;
listnode * linkListInit(listnode * head){
head = NULL;
return head;
}
listnode * addNode(listnode *head,elemType value){
listnode *ptr = (listnode *)malloc(sizeof(listnode));
ptr->info = value;
if(head == NULL){
ptr->next = NULL;
}
else{
ptr->next = head;
}
head = ptr;
return head;
}
listnode * findNode(listnode * head,elemType value){
listnode *ptr = head;
if(NULL == head)//0 elem
return NULL;
else if(NULL == ptr->next){ //1 elem
if(value == ptr->info)
return ptr;
else
return NULL;
}
while(ptr != NULL){
if(value == ptr->info)
return ptr;
ptr = ptr->next;
}
return NULL;
}
listnode * delNode(listnode *head,elemType value){
listnode *elemPtr = findNode(head,value);
listnode *ptr = head;
if(NULL == elemPtr)
printf("Cannot find value: %d in list.\n",value);
else{
if(elemPtr == head ){
if(head->next ==NULL)
head = NULL;
else{
head = head->next;
}
}
else if(elemPtr->next == NULL){
while(elemPtr != ptr->next){
ptr = ptr->next;
}
ptr->next = NULL;
}
else{
while(ptr->next != elemPtr){
ptr = ptr->next;
}
ptr->next = elemPtr->next;
}
free(elemPtr);
}
return head;
}
void printList(listnode *head){
if(head == NULL)
return ;
listnode *ptr = head;
while(ptr != NULL){
printf("%d -> ",ptr->info);
ptr = ptr->next;
}
printf("\n");
}
//server.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<error.h>
#include<errno.h>
#include<arpa/inet.h>
#include<pthread.h>
#include<time.h>
#include"linklist.c"
#define MAX_LEN 100
#define PORT_NUM 4321
#define EXIT_FAIL_NUM 1
#define MAX_NUM 3
void trans(void *p);
struct trans_data{
int fd;
char buff[MAX_LEN];
struct sockaddr_in info;
}p[MAX_NUM];
struct myTherad{
pthread_t tid;
int flag;
}myThread[MAX_NUM];
listnode *head;
int main(){
int sockfd,confd,len,err,i=0;
head=linkListInit(head);
for(i=0;i<MAX_NUM;i++){
memset(&myThread[i].flag,0,sizeof(int));
printf("thread[%d] flag %d\n",i,myThread[i].flag);
}
struct sockaddr_in ser,cli;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){
perror("socket");
exit(EXIT_FAIL_NUM);
}
printf("scoket_fd = %d\n",sockfd);
bzero(&ser,sizeof(ser));
ser.sin_family = AF_INET;
ser.sin_port = htons(PORT_NUM);
ser.sin_addr.s_addr = htonl(INADDR_ANY);
if(-1 == bind(sockfd,(struct sockaddr *)&ser,sizeof(ser))){
perror("bind");
exit(EXIT_FAIL_NUM);
}
if(0 != listen(sockfd,10)){
perror("listen");
exit(EXIT_FAIL_NUM);
}
printf("Receiving message......\n");
while(1){
len = sizeof(cli);
confd = accept(sockfd,(struct sockaddr *)&cli,&len);
if(-1 == confd){
perror("accept");
exit(EXIT_FAIL_NUM);
}
for(i=0;i<MAX_NUM;i++){
if(myThread[i].flag == 0){
p[i].fd = confd;
memcpy(&p[i].info,&cli,sizeof(p[i].info));
err = pthread_create(&myThread[i].tid,NULL,(void *)&trans,(void *)&p[i]);
if(0 != err){
printf("Can not create thread!\n");
exit(EXIT_FAIL_NUM);
}
myThread[i].flag = 1;
printf("confd:%d\n",confd);
head = addNode(head,confd);
// printList(head);
break;
}
}
if(MAX_NUM <= i){
char message[100];
memset(message,0,sizeof(message));
strcpy(message,"服务器没有空余线程,连接即将关闭......");
if( -1 ==send(confd,message,sizeof(message),0)){
perror("send");
exit(EXIT_FAIL_NUM);
}
}
}
return 0;
}
void trans(void *p){
int i;
int flag=0;
struct tm *t;
struct trans_data *ptr;
ptr = (struct trans_data *)p;
printf("线程创建成功,线程号:%lu\n",pthread_self());
send((*ptr).fd,"服务器线程创建成功...\n连接成功!!!",100,0);
while(1){
memset((*ptr).buff,0,sizeof((*ptr).buff));
int len;
if((len = recv((*ptr).fd,(*ptr).buff,sizeof((*ptr).buff),0))<0){
perror("recv");
exit(EXIT_FAIL_NUM);
}
if(len ==0){
for(i=0;i<MAX_NUM;i++){
if(myThread[i].tid == pthread_self()){
myThread[i].flag = 0;
printf("confd:%d\n",(*ptr).fd);
head = delNode(head,(*ptr).fd);
// printList(head);
printf("IP:%s\t端口号:%d 断开连接.\n",inet_ntoa((struct in_addr)(*ptr).info.sin_addr),(*ptr).info.sin_port);
printf("线程:%lu 被收回\n",myThread[i].tid);
break;
}
}
pthread_exit(NULL);
}
time_t timer;
timer = time(NULL);
t = localtime(&timer);
flag = 1;
printf("消息来自:\nIP:%s\t",inet_ntoa((struct in_addr )(*ptr).info.sin_addr));
printf("端口号:%d\t",(*ptr).info.sin_port);
printf("(%d:%d:%d)\n",(*t).tm_hour,(*t).tm_min,(*t).tm_sec);
printf("confd:%d\t线程号:%lu\n",(*ptr).fd,pthread_self());
puts((*ptr).buff);
char message[1024];
strcat(message,"\n来自IP:");
strcat(message,inet_ntoa((struct in_addr)(*ptr).info.sin_addr));
strcat(message,"\tPort:");
char m[10];
sprintf(m,"%d",(*ptr).info.sin_port);
strcat(message,m);
strcat(message,"\n消息:");
strcat(message,(*ptr).buff);
listnode *s = head;
int l;
// printList(head);
while(flag){
while(s !=NULL){
if(s->info !=(*ptr).fd){
if((l=send(s->info,message,sizeof(message),0))<0){
perror("send");
}
}
s = s->next;
}
flag = 0;
}
}
}
//client.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
#include<error.h>
#include<errno.h>
#include<fcntl.h>
#include<pthread.h>
#define MAX_LEN 100
#define EXIT_FAIL 1
#define SERV_IP "127.0.0.1"
#define PORT_NUM 4321
void doRecv(void *p){
char message[1024];
int len;
int *s = p;
while(1){
memset(message,0,sizeof(message));
// int io_block_var = fcntl(*s, F_GETFL, 0);
// fcntl(*s, F_SETFL, io_block_var|O_NONBLOCK);
if((len = recv(*s,message,sizeof(message),0))>0){
puts(message);
if(0 == strcmp(message,"服务器没有空余线程,连接即将关闭......")){
close(*s);
sleep(3);
exit(EXIT_FAIL);
}
}
}
}
int main(){
int sockfd,confd,len;
char buff[MAX_LEN];
int *p;
struct sockaddr_in cli,ser;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("socket");
exit(EXIT_FAIL);
}
printf("socket_fd = %d\n",sockfd);
bzero(&ser,sizeof(struct sockaddr_in));
ser.sin_family = AF_INET;
ser.sin_addr.s_addr = inet_addr(SERV_IP);
ser.sin_port = htons(PORT_NUM);
if((confd = connect(sockfd,(struct sockaddr *)&ser,sizeof(struct sockaddr))) == -1){
perror("connect");
exit(EXIT_FAIL);
}
printf("正在连接......\n");
sleep(2);
while(1){
pthread_t pid;
int err;
p = &sockfd;
err = pthread_create(&pid,NULL,(void *)&doRecv,(void *)p);
if(err != 0){
printf("Can not create thread!!!\n");
exit(EXIT_FAIL);
}
gets(buff);
// fgets(buff,sizeof(buff),stdin);
fflush(stdin);
len = send(sockfd,buff,strlen(buff),0);
if(len == -1){
perror("send");
exit(EXIT_FAIL);
}
memset(buff,0,sizeof(buff));
}
close(sockfd);
return 0;
}
运行截图
![](https://img-blog.csdn.net/20131031204721328?watermark/2/text/aHR0cDovL2Jsb 2cuY3Nkbi5uZXQvZ2VycmFyZDA2MTY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d issolve/70/gravity/SouthEast)
微信扫一扫,订阅我的博客动态^_^