int recv_msg(int userno,int from) { char buf[3000],*tmp; int len,i; len=read(from,buf,sizeof(buf)-1); if(len < 0) return -1; buf[len] = 0; if(len>0) { for(tmp=strtok(buf,"\n"); tmp; tmp=strtok(NULL,"\n")) { if ( contain_prompt(tmp) ) write_enable[userno] = 1 ; printf("%d| %s\n",userno,tmp); // echo input } } fflush(stdout); return len; }
void bash_serve() { int i, s, max_s, n; int activity; struct timeval timeout; // second, microsecs fd_set fds; timeout.tv_sec = 600; timeout.tv_usec = 0; while(1) { // FD_ZERO(&fds); max_s = 0; for( i=0 ; i<MAX_REQUEST ; i++ ) { s = req[i].socket; if(!s) continue; if(s>max_s) max_s = s; FD_SET(s, &fds); fprintf(stderr, "socket (%d)%d is set\n", i+1, s); } if(!max_s) { fprintf(stderr, "no more existing socket\n"); break; } // select activity = select(max_s+1, &fds, NULL, NULL, &timeout); if( (activity<0) && (errno!=EINTR) ) { error("select"); } else if( activity == 0 ) { fprintf(stderr, "timeout\n"); break; } for( i=0 ; i<MAX_REQUEST ; i++ ) { s = req[i].socket; if(!s) continue; if(!FD_ISSET(s, &fds)) continue; // read bzero(buf, BUFSIZE); //n = recv(s, buf, BUFSIZE, 0); n = read(s, buf, BUFSIZE); if(n<=0) { fprintf(stderr, "close socket (%d): %d\n", i+1, s); close(s); req[i].socket = 0; write_command_close(i); FD_CLR(s, &fds); } if(DEBUG) printf("<p>Read from server(%d):<br />%s***END***<br /></p>", i+1, wrap_html(buf)); write_content_at(i, wrap_html(buf), 0); // write next command //if(buf[0] == '%' || (buf[0] == '*' && buf[3] == '*')) { if(contain_prompt()) { fprintf(stderr, ">>> socket (%d) get:%s\nsend:\n<<<\n", i+1, buf); write_command_next(i); fprintf(stderr, "\n<<<\n"); } } } }