- 论坛徽章:
- 1
|
请教select的用法
例如:
- # include <sys/types.h>;
- # include <sys/socket.h>;
- # include <netinet/in.h>;
- # include <netdb.h>;
- # include <stdio.h>;
- # include <unistd.h>;
- # include <time.h>;
- //define some general viriables
- //log file 50!
- //record file 100! message
- void gettime();
- int num=0;//num为客户端socket数组专用…
- int number;//number是特定客户端数组socket号…
- char history[20]; //询问历史信息记录文件名…
- int userfd[20]; //很有用的…
- char *id[20]; //标明该用户地址…
- char timenow[25]; //标明此时服务器时间…
- char today[8]; //例如:20010412…
- char todayinfo[13]; //信息记录文件名…
- char todaylog[12]; //操作日志文件名…
- char message[46]; //发送信息数组…
- int inspect[20];
- char infofromclient[50];// his,msg info comes here!
- int fd1,fd2,fd; //record文件…log文件…history文件…!
- main()
- {
- int sockfd,maxfd=1;//sockfd is the socket for listening,maxfd is the max number of socket
- fd_set readfds;//readfds is the collection of the client sockets
- char showin[29]; //showin!!!!!!!
- int k;
- int i;
- int port;
- struct sockaddr_in server;
- char request[3];
- int namelen;
- /******************* realcode here ***********************/
- for (k=1;k<=19;k++)
- inspect[k]=1; //to make the out off!!
- sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
- if(sockfd<0)
- {
- perror("socket");
- exit(1);
- }
- server.sin_family=AF_INET;
- server.sin_addr.s_addr=INADDR_ANY;
- server.sin_port=htons(3333);
- if(bind(sockfd,&server,sizeof(server))<0)
- {
- perror("bind");
- exit(2);
- }
- printf("bind ok\n"); //+++++++++++++++temp+++++++++++++++
- if(listen(sockfd,5)<0)
- {
- perror("listen");
- exit(3);
- }
- printf("listen ok\n"); //+++++++++++++++temp+++++++++++++++
- FD_ZERO( &readfds); //处理……
- FD_SET( sockfd, &readfds);
- for ( k=0;k<=num;k++)
- FD_SET( userfd[k], &readfds); //加入集中去…
- /*** 发现错误后增加如下语句
- maxfd=sockfd+1;
- /***
- //////////////////////////////////////////////////////////////////////
- //is the main loop while
- userfd[0]=0;// 0 is empty!
- while(1)
- {
- if (userfd[num]>;maxfd)
- maxfd=userfd[num]; //needn't
- select( maxfd, &readfds, NULL, NULL, NULL); //判断maxfd
- //case 1 :if some new client comes
- if ( FD_ISSET( sockfd, &readfds))
- {
- printf("get connect\n");//debug
- num++;
- namelen=sizeof(server);
- if((userfd[num]=accept(sockfd,&server,&namelen))<0)
- //建立新连接, 并将该连接描述符加入到 sockset 中去了,此为有新连接…
- {
- perror("accept");
- exit(4);
- } //如果退出,那…
- printf("server: got connection from %s\n",inet_ntoa(server.sin_addr));
- strcpy(id[num],"203.93.49.33");//inet_ntoa(server.sin_addr)); **@@##
- printf("new connection from %s/n",id[num]); //IP地址
- printf("a new user comes!!");
- gettime();
- strncpy(todaylog,today,8);
- strcat(todaylog,".log");
- if ((fd1=open(todaylog))==-1)
- {
- creat(todaylog,2);
- fd1=open(todaylog,2);
- }
- write(fd1,id[num],20); //保存
- write(fd1,":",2); //操作
- write(fd1,"IN",6); //日志
- write(fd1,":",2); //文件
- write(fd1,timenow,30); //时间
- strncpy(showin,id[num],20);
- strcat(showin,"has come!");
- for(k=1;k<num;k++)
- if (inspect[k])
- write(userfd[k],showin,29);
- FD_SET(userfd[num],&readfds); //加到集中…because of linked recently;
- }
- //end case 1:end some new client comes
- //case 2:if some connected clients send some message
- for ( k=1;k<=num;k++)
- {
- if (FD_ISSET(userfd[k], &readfds))
- {
- number=k;
- read(userfd[k],infofromclient,50);
- printf("%s\n",infofromclient);
- strncpy(request,infofromclient,3);
- gettime();
- strncpy(todaylog,today,8);
- strcat(todaylog,".log");
- if ((fd1=open(todaylog))==-1)
- {
- creat(todaylog,2);
- fd1=open(todaylog,2);
- }
- write(fd1,id[number],20); //保存
- write(fd1,":",2); //操作
- write(fd1,request,6); //日志
- write(fd1,":",2); //文件
- write(fd1,timenow,30); //时间
- //MMM SSSS GGG @@@
- if (strcmp(request,"MSG")==0)
- {
- gettime();
- strncpy(todayinfo,today,8);
- strcat(todayinfo,".info");
- if ((fd2=open(todayinfo,2))==-1)
- {
- creat(todayinfo,2);
- fd2=open(todayinfo,2);
- }
- for(i=0;i<=45;i++)
- message=infofromclient[i+4];
- msg();
- }
- //HHH IIII SSS @@@
- /* if (strcmp(request,"HIS")==0)
- {
- for(i=0;i<=14;i++)
- history=infofromclient[i+4];
- strcat(history,".info");
- if ((fd=open(history,2))==-1)
- {
- printf("the file dosn't exist!");
- exit(6);
- }
- do
- {
- his();
- sleep(1); //pause in order to let client read!!
- }
- while(!eof(fd1));
- }*/
- if (strcmp(request,"OUT")==0)
- out();
- }
- }
- //end case 2:end some connected clients send some message
- }
- //end of main loop while
- } //OK! end main
- ///////////////////////////////////////////////////////////////////////
- msg()
- {
- int k;
- char record[100];
- write(fd2,id[number],20); //保存
- write(fd2,":",2); //信息
- write(fd2,message,46); //记录
- write(fd2,":",2); //文件
- write(fd2,timenow,30); //时间
- lseek(fd2,-100L,2);
- read(fd2,record,100);
- for ( k=0;k<=num;k++)
- if (inspect[k])
- write(userfd[k],record,100);
- lseek(fd2,100L,1);
- }
- his()
- {
- char onerecord[100];
- read(fd,onerecord,100);
- write(userfd[number],onerecord,100);
- lseek(fd1,100L,1);
- }
- out()
- {
- int k;
- char showout[31];
- strncpy(showout,id[number],20);
- strcat(showout,"is out yet!",11);
- shutdown(userfd[number],2);
- for(k=1;k<=num;k++)
- if (inspect[k])
- write(userfd[k],showout,31);
- inspect[k]=0;
- }
- void gettime()
- {
- time_t timer;
- char mon[2];
- char year[4];
- char mday[2];
- char year1[4];
- char mday1[2];
- char mon1[2];
- struct tm *t;
- timer=time(NULL);
- t=localtime(&timer);
- strcpy(timenow,asctime(t));
- (t->;tm_mon)++;
- sprintf(mon1,"%d",t->;tm_mon);
- if (t->;tm_mon/10==0)
- {
- strcpy(mon,"0");
- strcat(mon,mon1);
- }
- else strcpy(mon,mon1);
- t->;tm_year+=1900;
- sprintf(year1,"%d",t->;tm_year);
- strcpy(year,year1);
- strcpy(today,year);
- sprintf(mday1,"%d",t->;tm_mday);
- if (t->;tm_mday/10==0)
- {
- strcpy(mday,"0");
- strcat(mday,mday1);
- }
- else strcpy(mday,mday1);
- strncat(today,mon,2);
- strncat(today,mday,2);
- }
复制代码 |
|