void cliClientServer(char *server) { int length, i; uint8_t cmd[2], buf[256], *ptr, *devname; activeSocket=lxScriboSocketInit(server); if(activeSocket<0) { fprintf(stderr, "something wrong with client %s\n", server); exit(1); } while(1){ length = read(activeSocket, buf, 256); if (socket_verbose & (length>0)) { printf("recv: "); for(i=0;i<length;i++) printf("0x%02x ", buf[i]); printf("\n"); } if (length>0) CmdProcess(buf, length); else { close(activeSocket); tfaRun_Sleepus(10000); activeSocket=lxScriboSocketInit(server); } } }
//-------------------------------- int main(int argc, char* argv[]) { UNUSED(argc); UNUSED(argv); #ifdef TUNE_PARAMETERS for(int i = 0; i < NPARAMS; ++i) param.push_back(1); #endif InitEngine(); tt.clear(); max_search_depth = max_ply; time_remains = 300000000; time_base = 300000000; time_inc = 0; moves_per_session = 0; max_nodes_to_search = 0; time_command_sent = false; char in[0x4000]; while(!quit) { if(!std::cin.getline(in, sizeof(in), '\n')) std::cin.clear(); if(CmdProcess((std::string)in)) { // NiCheGoNeDeLaYem! } else if(!busy && LooksLikeMove((std::string)in)) { if(!MakeMoveFinaly(in)) std::cout << "Illegal move" << std::endl; else if(!force) { #ifdef USE_THREAD_FOR_INPUT if(t.joinable()) t.join(); t = std::thread(MainSearch); #else MainSearch(); #endif // USE_THREAD_FOR_INPUT } } else std::cout << "Unknown command: " << in << std::endl; } if(busy) StopEngine(); }
void FtpServe(){ if((cid = fork()) < 0) err_exit("fork failed", 1); if(cid > 0) msg("Server started"); if(cid == 0){ // 在这里完成子进程的任务 int client_fd, sin_size, opt; /*sock_fd:监听socket;client_fd:数据传输socket */ struct sockaddr_in my_addr; /* 本机地址信息 */ struct sockaddr_in remote_addr; /* 客户端地址信息 */ InitChldPidPool(); // 初始化子进程pid池 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { err_exit("socket create failed",1); } // 将socket设置为可以重复使用,这个很有必要,不然就会出现调试过程中无法绑定端口的问题 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,&opt,sizeof(&opt)); // 设置地址的参数 my_addr.sin_family=AF_INET; my_addr.sin_port=htons(SERVPORT); my_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(my_addr.sin_zero),8); // 将socket与地址绑定 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { err_exit("bind error",1); } // 监听端口 if (listen(sockfd, BACKLOG) == -1) { err_exit("listen error",1); } // 响应信号 if(signal(SIGUSR1, sig_close_server) == SIG_ERR) err_exit("signal stop register failed", 1); if(signal(SIGCHLD, sig_chld_exit) == SIG_ERR) err_exit("signal child exit register failed", 1); // 循环监听客户端的连接 int *p; while(1){ sin_size = sizeof(struct sockaddr_in); if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1) { printf("accept error"); continue; // 如果连接过多,在这里会直接跳过 } printf("received a connection from %s\n", inet_ntoa(remote_addr.sin_addr)); p = getAvilablePid(); CmdProcess(p, client_fd, remote_addr); } // while退出,服务进程作服务器的善后工作 msg("shuting down..."); DeInitChldPidPool(); exit(0); } }