这篇教程C++ FD_ZERO函数代码示例写得很实用,希望能帮到您。
本文整理汇总了C++中FD_ZERO函数的典型用法代码示例。如果您正苦于以下问题:C++ FD_ZERO函数的具体用法?C++ FD_ZERO怎么用?C++ FD_ZERO使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。 在下文中一共展示了FD_ZERO函数的30个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。 示例1: main//.........这里部分代码省略......... printf(" watchtower 'echo $F' /home/user # prints any filenames modified under " "/home/user/n"); printf("/n"); printf("Available options:/n"); printf(" -h, --help print this message/n"); printf(" -v, --version print program version/n"); printf(" -r, --recursive set program to watch entire subtree/n"); ret = EXIT_SUCCESS; goto done; } if (recursive_flag) fprintf(stderr, "WARNING: recursive mode is not yet implemented/n"); command = argv[optind++]; path = argv[optind]; memset(&hints, 0, sizeof(struct fileinfo)); hints.fi_type = DT_DIR; if ((res = getfileinfo(path, &hints, &info)) != 0) { fprintf(stderr, "getfileinfo: %s/n", gfi_strerror(res)); goto done; } infd = inotify_init(); for (p = info; p; p = p->fi_next) { if (strcmp(p->fi_name, "..") == 0) continue; if (strcmp(p->fi_name, ".") == 0) inwd = inotify_add_watch(infd, p->fi_path, IN_MODIFY); } freefileinfo(info); sigemptyset(&sigmask); sigaddset(&sigmask, SIGINT); sigprocmask(SIG_BLOCK, &sigmask, NULL); sa.sa_flags = 0; sa.sa_handler = interrupt_handler; sigemptyset(&sa.sa_mask); sigaction(SIGINT, &sa, NULL); sigemptyset(&empty_mask); for (;;) { FD_ZERO(&fds); FD_SET(infd, &fds); pselect(infd + 1, &fds, NULL, NULL, NULL, &empty_mask); if (got_SIGINT) { printf("/ncleaning up.../n"); break; } len = read(infd, buf, BUFFER_SIZE); for (iter = buf; iter < buf + len; iter += sizeof(struct inotify_event) + event->len) { event = (const struct inotify_event *)iter; if (event->mask & IN_MODIFY) { pid = fork(); switch (pid) { case -1: fprintf(stderr, "forking failed/n"); break; case 0: env[0] = malloc(strlen(event->name) + 3); sprintf(env[0], "F=%s", event->name); env[1] = NULL; execle("/bin/sh", "sh", "-c", command, (char *)NULL, env); free(env[0]); ret = EXIT_SUCCESS; goto done; default: fflush(NULL); if (waitpid(pid, &status, 0) == -1) fprintf(stderr, "child process failed/n"); break; } } } } inotify_rm_watch(infd, inwd); close(infd); ret = EXIT_SUCCESS; done: return ret;}
开发者ID:dutch,项目名称:watchtower,代码行数:101,
示例2: gwrl_bkd_gathervoid gwrl_bkd_gather(gwrl * rl) { int res = 0; gwrlevt_flags_t newflags = 0; fd_set fds[3]; gwrlsrc * src = NULL; gwrlsrc_file * fsrc = NULL; gwrlevt * evt = NULL; gwrlbkd * bkd = rl->backend; gwrlbkd_select * sbkd = _gwrlbkds(bkd); //setup timeout for select struct timeval timeout; struct timeval * timeoutp = &timeout; if(bkd->timeout.tv_sec == sec_min && bkd->timeout.tv_nsec == nsec_min) { timeoutp = NULL; } else { gwtm_timespec_to_timeval(&bkd->timeout,&timeout); } //if sleep isn't allowed set timeout to 0 if(!timeoutp && flisset(rl->flags,GWRL_NOSLEEP)) { timeoutp = &timeout; timeout.tv_sec = 0; timeout.tv_usec = 0; #ifdef GWRL_COVERAGE_INTERNAL_ASSERT_VARS if(asserts_var1 == gwrlbkd_no_sleep_assert_true) { asserts_var2 = true; } #endif } //initialize fds FD_ZERO(&(fds[0])); FD_ZERO(&(fds[1])); FD_ZERO(&(fds[2])); //reset fds memcpy(&(fds[0]),&(sbkd->src[0]),sizeof(fd_set)); memcpy(&(fds[1]),&(sbkd->src[1]),sizeof(fd_set)); memcpy(&(fds[2]),&(sbkd->src[2]),sizeof(fd_set)); //call select, wrapped in sleeping flags so other threads can wake us flset(rl->flags,GWRL_SLEEPING); res = select(sbkd->maxfd+1,&(fds[0]),&(fds[1]),&(fds[2]),timeoutp); flclr(rl->flags,GWRL_SLEEPING); if(res == 0) return; //timeout //break and let the event loop continue or start over. //if a signal did happen, the event loop may have an //event that needs processing. if(res < 0 && (errno == EINTR || errno == EAGAIN)) return; //bad fd, unforunately select doesn't tell us which //one it was so we have to search for it. if(res < 0 && errno == EBADF) { gwrl_src_file_find_badfd_post_evt(rl); return; } //invalid timeout or invalid number of fds. if(res < 0 && errno == EINVAL) { //invalid timeout, break and let process events recalculate timeouts. if(timeout.tv_sec < 0 || timeout.tv_usec < 0) return; //nfds parameter to select() is too large, not sure how to handle fprintf(stderr,"select: file descriptor limit reached. exiting. /n"); exit(1); } //valid events are ready if(res > 0) { fsrc = _gwrlsrcf(rl->sources[GWRL_SRC_TYPE_FILE]); while(fsrc) { src = _gwrlsrc(fsrc); newflags = 0; if(!flisset(src->flags,GWRL_ENABLED)) { fsrc = _gwrlsrcf(src->next); continue; } if(FD_ISSET(fsrc->fd,&fds[0])) flset(newflags,GWRL_RD); if(FD_ISSET(fsrc->fd,&fds[1])) flset(newflags,GWRL_WR); if(FD_ISSET(fsrc->fd,&fds[2])) flset(newflags,GWRL_RD); if(newflags > 0) { evt = gwrl_evt_createp(rl,src,src->callback,src->userdata,fsrc->fd,newflags); gwrl_post_evt(rl,evt); } fsrc = _gwrlsrcf(src->next); } }}
开发者ID:ChinaXing,项目名称:libgwrl,代码行数:96,
示例3: read//.........这里部分代码省略......... // cursor doesn't reflect actual length of the string that's sent back tty_con.cursor = strlen( tty_con.buffer ); if ( tty_con.cursor > 0 ) { if ( tty_con.buffer[0] == '//' ) { for ( i = 0; i <= tty_con.cursor; i++ ) { tty_con.buffer[i] = tty_con.buffer[i + 1]; } tty_con.cursor--; } } tty_Show(); return NULL; } avail = read( 0, &key, 1 ); if ( avail != -1 ) { // VT 100 keys if ( key == '[' || key == 'O' ) { avail = read( 0, &key, 1 ); if ( avail != -1 ) { switch ( key ) { case 'A': history = Hist_Prev(); if ( history ) { tty_Hide(); tty_con = *history; tty_Show(); } tty_FlushIn(); return NULL; break; case 'B': history = Hist_Next(); tty_Hide(); if ( history ) { tty_con = *history; } else { Field_Clear( &tty_con ); } tty_Show(); tty_FlushIn(); return NULL; break; case 'C': return NULL; case 'D': return NULL; } } } } Com_DPrintf( "droping ISCTL sequence: %d, tty_erase: %d/n", key, tty_erase ); tty_FlushIn(); return NULL; } // push regular character tty_con.buffer[tty_con.cursor] = key; tty_con.cursor++; // print the current line (this is differential) write( 1, &key, 1 ); } return NULL; } else { int len; fd_set fdset; struct timeval timeout; if ( !com_dedicated || !com_dedicated->value ) { return NULL; } if ( !stdin_active ) { return NULL; } FD_ZERO( &fdset ); FD_SET( 0, &fdset ); // stdin timeout.tv_sec = 0; timeout.tv_usec = 0; if ( select( 1, &fdset, NULL, NULL, &timeout ) == -1 || !FD_ISSET( 0, &fdset ) ) { return NULL; } len = read( 0, text, sizeof( text ) ); if ( len == 0 ) { // eof! stdin_active = qfalse; return NULL; } if ( len < 1 ) { return NULL; } text[len - 1] = 0; // rip off the /n and terminate return text; }}
开发者ID:chegestar,项目名称:omni-bot,代码行数:101,
示例4: PAUDIO_WaitDevice/* This function waits until it is possible to write a full sound buffer */static voidPAUDIO_WaitDevice(_THIS){ fd_set fdset; /* See if we need to use timed audio synchronization */ if (this->hidden->frame_ticks) { /* Use timer for general audio synchronization */ Sint32 ticks; ticks = ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS; if (ticks > 0) { SDL_Delay(ticks); } } else { audio_buffer paud_bufinfo; /* Use select() for audio synchronization */ struct timeval timeout; FD_ZERO(&fdset); FD_SET(this->hidden->audio_fd, &fdset); if (ioctl(this->hidden->audio_fd, AUDIO_BUFFER, &paud_bufinfo) < 0) {#ifdef DEBUG_AUDIO fprintf(stderr, "Couldn't get audio buffer information/n");#endif timeout.tv_sec = 10; timeout.tv_usec = 0; } else { long ms_in_buf = paud_bufinfo.write_buf_time; timeout.tv_sec = ms_in_buf / 1000; ms_in_buf = ms_in_buf - timeout.tv_sec * 1000; timeout.tv_usec = ms_in_buf * 1000;#ifdef DEBUG_AUDIO fprintf(stderr, "Waiting for write_buf_time=%ld,%ld/n", timeout.tv_sec, timeout.tv_usec);#endif }#ifdef DEBUG_AUDIO fprintf(stderr, "Waiting for audio to get ready/n");#endif if (select(this->hidden->audio_fd + 1, NULL, &fdset, NULL, &timeout) <= 0) { const char *message = "Audio timeout - buggy audio driver? (disabled)"; /* * In general we should never print to the screen, * but in this case we have no other way of letting * the user know what happened. */ fprintf(stderr, "SDL: %s - %s/n", strerror(errno), message); this->enabled = 0; /* Don't try to close - may hang */ this->hidden->audio_fd = -1;#ifdef DEBUG_AUDIO fprintf(stderr, "Done disabling audio/n");#endif }#ifdef DEBUG_AUDIO fprintf(stderr, "Ready!/n");#endif }}
开发者ID:KSLcom,项目名称:caesaria-game,代码行数:68,
示例5: wi_socket_wait_multiplewi_socket_t * wi_socket_wait_multiple(wi_array_t *array, wi_time_interval_t timeout) { wi_enumerator_t *enumerator; wi_socket_t *socket, *waiting_socket = NULL; struct timeval tv; fd_set rfds, wfds; int state, max_sd; tv = wi_dtotv(timeout); max_sd = -1; FD_ZERO(&rfds); FD_ZERO(&wfds); wi_array_rdlock(array); enumerator = wi_array_data_enumerator(array); while((socket = wi_enumerator_next_data(enumerator))) { if(wi_string_length(socket->buffer) > 0) { waiting_socket = socket; break; } if(socket->direction & WI_SOCKET_READ) FD_SET(socket->sd, &rfds); if(socket->direction & WI_SOCKET_WRITE) FD_SET(socket->sd, &wfds); if(socket->sd > max_sd) max_sd = socket->sd; } wi_array_unlock(array); if(waiting_socket) return waiting_socket; state = select(max_sd + 1, &rfds, &wfds, NULL, (timeout > 0.0) ? &tv : NULL); if(state < 0) { wi_error_set_errno(errno); return NULL; } wi_array_rdlock(array); enumerator = wi_array_data_enumerator(array); while((socket = wi_enumerator_next_data(enumerator))) { if(FD_ISSET(socket->sd, &rfds) || FD_ISSET(socket->sd, &wfds)) { waiting_socket = socket; break; } } wi_array_unlock(array); return waiting_socket;}
开发者ID:asvitkine,项目名称:phxd,代码行数:63,
示例6: easylink_threadvoid easylink_thread(void *inContext){ OSStatus err = kNoErr; mico_Context_t *Context = inContext; fd_set readfds; int reConnCount = 0; int clientFdIsSet; easylink_log_trace(); require_action(easylink_sem, threadexit, err = kNotPreparedErr); if(Context->flashContentInRam.micoSystemConfig.easyLinkByPass == EASYLINK_BYPASS){ Context->flashContentInRam.micoSystemConfig.easyLinkByPass = EASYLINK_BYPASS_NO; MICOUpdateConfiguration(Context); _easylinkConnectWiFi_fast(Context); }else if(Context->flashContentInRam.micoSystemConfig.easyLinkByPass == EASYLINK_SOFT_AP_BYPASS){ ConfigWillStop( Context ); _easylinkStartSoftAp(Context); mico_rtos_delete_thread(NULL); return; }else{#ifdef EasyLink_Plus easylink_log("Start easylink plus mode"); micoWlanStartEasyLinkPlus(EasyLink_TimeOut/1000);#else easylink_log("Start easylink V2"); micoWlanStartEasyLink(EasyLink_TimeOut/1000);#endif mico_rtos_get_semaphore(&easylink_sem, MICO_WAIT_FOREVER); if(EasylinkFailed == false) _easylinkConnectWiFi(Context); else{ msleep(20); _cleanEasyLinkResource( Context ); ConfigWillStop( Context ); _easylinkStartSoftAp(Context); mico_rtos_delete_thread(NULL); return; } } err = mico_rtos_get_semaphore(&easylink_sem, EasyLink_ConnectWlan_Timeout); require_noerr(err, reboot); httpHeader = HTTPHeaderCreate(); require_action( httpHeader, threadexit, err = kNoMemoryErr ); HTTPHeaderClear( httpHeader ); while(1){ if(easylinkClient_fd == -1){ err = _connectFTCServer(inContext, &easylinkClient_fd); require_noerr(err, Reconn); }else{ FD_ZERO(&readfds); FD_SET(easylinkClient_fd, &readfds); if(httpHeader->len == 0){ err = select(1, &readfds, NULL, NULL, NULL); require(err>=1, threadexit); clientFdIsSet = FD_ISSET(easylinkClient_fd, &readfds); } if(clientFdIsSet||httpHeader->len){ err = SocketReadHTTPHeader( easylinkClient_fd, httpHeader ); switch ( err ) { case kNoErr: // Read the rest of the HTTP body if necessary do{ err = SocketReadHTTPBody( easylinkClient_fd, httpHeader ); require_noerr(err, Reconn); PrintHTTPHeader(httpHeader); // Call the HTTPServer owner back with the acquired HTTP header err = _FTCRespondInComingMessage( easylinkClient_fd, httpHeader, Context ); require_noerr( err, Reconn ); if(httpHeader->contentLength == 0) break; } while( httpHeader->chunkedData == true || httpHeader->dataEndedbyClose == true); // Reuse HTTPHeader HTTPHeaderClear( httpHeader ); break; case EWOULDBLOCK: // NO-OP, keep reading break; case kNoSpaceErr: easylink_log("ERROR: Cannot fit HTTPHeader."); goto Reconn; break; case kConnectionErr: // NOTE: kConnectionErr from SocketReadHTTPHeader means it's closed easylink_log("ERROR: Connection closed."); /*Roll back to previous settings (if it has) and reboot*/ if(Context->flashContentInRam.micoSystemConfig.configured == wLanUnConfigured ){ Context->flashContentInRam.micoSystemConfig.configured = allConfigured; MICOUpdateConfiguration( Context );//.........这里部分代码省略.........
开发者ID:yantrabuddhi,项目名称:bootlaoder_EMW3165,代码行数:101,
示例7: p2p_log_msg//// function: tunnel_server_handler// opne a service port and process the service request from devices// parameters// service port number// return// 0 success// other fail//void *tunnel_server_handler(void *pdata){ fd_set readfds, writefds, rfds, wfds; int nfds=-1,servfd=-1, sd=-1, ret=-1; struct timeval tv; struct sockaddr_in clientaddr; /* client addr */ int optval; /* service socket */ unsigned int next_time, current_time;#ifdef _OPEN_SECURED_CONN SSL *ssl=NULL; int securefd = -1;#endif // // tunnel control block init // if (tunnel_ctrl_init()<0) { p2p_log_msg( "initial tunnel control block fail!"); goto end_tunnel_server_handler; } FD_ZERO(&readfds); FD_ZERO(&writefds); // wait mone secure connection servfd = do_bind_service_port(service_port); if (servfd>=0) { FD_SET(servfd, &readfds); nfds = (servfd>nfds?servfd:nfds); } else goto end_tunnel_server_handler; #ifdef _OPEN_SECURED_CONN // wait mone secure connection securefd = do_bind_service_port(secure_service_port); if (securefd>=0) { FD_SET(securefd, &readfds); nfds = (securefd>nfds?securefd:nfds); } else goto end_tunnel_server_handler;#endif // // report to SC that service start // do_service_start("tunnel server start"); // // main loop: wait for a connection request // current_time = get_time_milisec(); next_time = current_time + 10000; // next seconds while (tunnel_server_on) { current_time = get_time_milisec(); if (current_time > next_time) { // if timer expired, do report to SC do_service_report(); next_time = current_time + 10000; // 10 seconds } memcpy(&rfds,&readfds,sizeof(fd_set)); memcpy(&wfds,&writefds,sizeof(fd_set)); // reset timeout value tv.tv_sec = 1; // 1 seconds tv.tv_usec = 0; if((ret=select(nfds+1, &rfds, &wfds, NULL, &tv)) == -1) { p2p_log_msg( "Server-select() error lol!"); goto end_tunnel_server_handler; }#ifdef _OPEN_SECURED_CONN if (FD_ISSET(securefd, &rfds)) { socklen_t clientlen = sizeof(clientaddr); sd = ssl_server_accept(securefd,&ssl); if (sd >= 0) { getpeername(sd, (struct sockaddr*)&clientaddr, &clientlen); p2p_log_msg("new secure connection from %s/n",inet_ntoa (clientaddr.sin_addr)); ret = new_device_tunnel(sd, &clientaddr, ssl); if (ret<0) { p2p_log_msg("reject secure connection from %s/n",inet_ntoa (clientaddr.sin_addr)); ssl_server_close(ssl); } }//.........这里部分代码省略.........
开发者ID:kennywj,项目名称:tunnel_server,代码行数:101,
示例8: ARGS1/* Bind to a TCP port** ------------------**** On entry,** tsap is a string explaining where to take data from.** "" means data is taken from stdin.** "*:1729" means "listen to anyone on port 1729"**** On exit,** returns Negative value if error.*/int do_bind ARGS1(CONST char *, tsap){ FD_ZERO(&open_sockets); /* Clear our record of open sockets */ num_sockets = 0; /* Deal with PASSIVE socket:**** A passive TSAP is one which has been created by the inet daemon.** It is indicated by a void TSAP name. In this case, the inet** daemon has started this process and given it, as stdin, the connection** which it is to use.*/ if (*tsap == 0) { /* void tsap => passive */ dynamic_allocation = FALSE; /* not dynamically allocated */ role = passive; /* Passive: started by daemon */#ifdef vms { unsigned short channel; /* VMS I/O channel */ struct string_descriptor { /* This is NOT a proper descriptor*/ int size; /* but it will work. */ char *ptr; /* Should be word,byte,byte,long */ } sys_input = {10, "SYS$INPUT:"}; int status; /* Returned status of assign */ extern int sys$assign(); status = sys$assign(&sys_input, &channel, 0, 0); com_soc = channel; /* The channel is stdin */ CTRACE(tfp, "IP: Opened PASSIVE socket %d/n", channel); return 1 - (status&1); } #else com_soc = 0; /* The channel is stdin */ CTRACE(tfp, "IP: PASSIVE socket 0 assumed from inet daemon/n"); return 0; /* Good */#endif/* Parse the name (if not PASSIVE)*/ } else { /* Non-void TSAP */ char *p; /* pointer to string */ char *q; struct hostent *phost; /* Pointer to host - See netdb.h */ char buffer[256]; /* One we can play with */ register struct sockaddr_in* sin = &soc_address; strcpy(buffer, tsap); p = buffer;/* Set up defaults:*/ sin->sin_family = AF_INET; /* Family = internet, host order */ sin->sin_port = 0; /* Default: new port, */ dynamic_allocation = TRUE; /* dynamically allocated */ role = passive; /* by default *//* Check for special characters:*/ if (*p == WILDCARD) { /* Any node */ role = master; p++; }/* Strip off trailing port number if any:*/ for(q=p; *q; q++) if (*q==':') { int status; *q++ = 0; /* Terminate node string */ sin->sin_port = htons((unsigned short)HTCardinal( &status, &q, (unsigned int)65535)); if (status<0) return status; if (*q) return -2; /* Junk follows port number */ dynamic_allocation = FALSE; break; /* Exit for loop before we skip the zero */ } /*if*//* Get node name:*/ if (*p == 0) { sin->sin_addr.s_addr = INADDR_ANY; /* Default: any address */ } else if (*p>='0' && *p<='9') { /* Numeric node address: */ sin->sin_addr.s_addr = inet_addr(p); /* See arpa/inet.h */ } else { /* Alphanumeric node name: *///.........这里部分代码省略.........
开发者ID:NotTheRealTimBL,项目名称:WWWDaemon,代码行数:101,
示例9: server_loopPRIVATE int server_loop()#endif{ int tcp_status; /* <0 if error, in general */ int timeout = -1; /* No timeout required but code exists */ for(;;) {/* If it's a master socket, then find a slave:*/ if (role == master) {#ifdef SELECT fd_set read_chans; fd_set write_chans; fd_set except_chans; int nfound; /* Number of ready channels */ struct timeval max_wait; /* timeout in form for select() */ FD_ZERO(&write_chans); /* Clear the write mask */ FD_ZERO(&except_chans); /* Clear the exception mask *//* If timeout is required, the timeout structure is set up. Otherwise** (timeout<0) a zero is passed instead of a pointer to the struct timeval.*/ if (timeout>=0) { max_wait.tv_sec = timeout/100; max_wait.tv_usec = (timeout%100)*10000; } for (com_soc=(-1); com_soc<0;) { /* Loop while connections keep coming */ /* The read mask expresses interest in the master channel for incoming** connections) or any slave channel (for incoming messages).*//* Wait for incoming connection or message*/ read_chans = open_sockets; /* Read on all active channels */ if (TRACE) printf("Daemon: Waiting for connection or message. (Mask=%x hex, max=%x hex)./n", *(int *)(&read_chans), num_sockets); nfound=select(num_sockets, &read_chans, &write_chans, &except_chans, timeout >= 0 ? &max_wait : 0); if (nfound<0) return HTInetStatus("select()"); if (nfound==0) return 0; /* Timeout *//* We give priority to existing connected customers. When there are** no outstanding commands from them, we look for new customers.*//* If a message has arrived on one of the channels, take that channel:*/ { int i; for(i=0; i<num_sockets; i++) if (i != master_soc) if (FD_ISSET(i, &read_chans)) { if (TRACE) printf( "Message waiting on socket %d/n", i); com_soc = i; /* Got one! */ break; } if (com_soc>=0) break; /* Found input socket */ } /* block */ /* If an incoming connection has arrived, accept the new socket:*/ if (FD_ISSET(master_soc, &read_chans)) { CTRACE(tfp, "Daemon: New incoming connection:/n"); tcp_status = accept(master_soc, (struct sockaddr *)&soc_address, &soc_addrlen); if (tcp_status<0) return HTInetStatus("accept"); CTRACE(tfp, "Daemon: Accepted new socket %d/n", tcp_status); FD_SET(tcp_status, &open_sockets); if ((tcp_status+1) > num_sockets) num_sockets=tcp_status+1; } /* end if new connection */ } /* loop on event */ #else /* SELECT not supported */ if (com_soc<0) { /* No slaves: must accept */ CTRACE(tfp, "Daemon: Waiting for incoming connection.../n"); tcp_status = accept(master_soc, &rsoc->mdp.soc_tcp.soc_address, &rsoc->mdp.soc_tcp.soc_addrlen); if (tcp_status<0) return HTInetStatus("accept"); com_soc = tcp_status; /* socket number */ CTRACE(tfp, "Daemon: Accepted socket %d/n", tcp_status);//.........这里部分代码省略.........
开发者ID:NotTheRealTimBL,项目名称:WWWDaemon,代码行数:101,
示例10: cmyth_file_read/* * cmyth_file_read() * * Scope: PUBLIC * * Description * * Request and read a block of data from backend * * Return Value: * * Sucess: number of bytes transfered * * Failure: an int containing -errno */int32_t cmyth_file_read(cmyth_file_t file, char *buf, int32_t len){ int err, count; int32_t ret; int req, nfds, rec; char *end, *cur; char msg[256]; int64_t len64; struct timeval tv; fd_set fds; if (!file || !file->file_data) { cmyth_dbg (CMYTH_DBG_ERROR, "%s: no connection/n", __FUNCTION__); return -EINVAL; } if (len == 0) return 0; if(len > file->file_data->conn_tcp_rcvbuf) len = file->file_data->conn_tcp_rcvbuf; pthread_mutex_lock (&file->file_control->conn_mutex); /* make sure we have outstanding requests that fill the buffer that was called with */ /* this way we should be able to saturate the network connection better */ if (file->file_req < file->file_pos + len) { snprintf (msg, sizeof (msg), "QUERY_FILETRANSFER %"PRIu32"[]:[]REQUEST_BLOCK[]:[]%"PRId32, file->file_id, (int32_t)(file->file_pos + len - file->file_req)); if ( (err = cmyth_send_message (file->file_control, msg) ) < 0) { cmyth_dbg (CMYTH_DBG_ERROR, "%s: cmyth_send_message() failed (%d)/n", __FUNCTION__, err); ret = err; goto out; } req = 1; } else { req = 0; } rec = 0; cur = buf; end = buf+len; while (cur == buf || req || rec) { if(rec) { tv.tv_sec = 0; tv.tv_usec = 0; } else { tv.tv_sec = 20; tv.tv_usec = 0; } nfds = 0; FD_ZERO (&fds); if (req) { if ((int)file->file_control->conn_fd > nfds) nfds = (int)file->file_control->conn_fd; FD_SET (file->file_control->conn_fd, &fds); } if ((int)file->file_data->conn_fd > nfds) nfds = (int)file->file_data->conn_fd; FD_SET (file->file_data->conn_fd, &fds); if ((ret = select (nfds+1, &fds, NULL, NULL,&tv)) < 0) { cmyth_dbg (CMYTH_DBG_ERROR, "%s: select(() failed (%d)/n", __FUNCTION__, ret); goto out; } if (ret == 0 && !rec) { file->file_control->conn_hang = 1; file->file_data->conn_hang = 1; ret = -ETIMEDOUT; goto out; } /* check control connection */ if (FD_ISSET(file->file_control->conn_fd, &fds)) {//.........这里部分代码省略.........
开发者ID:Alpha13s,项目名称:xbmc-pvr-addons,代码行数:101,
示例11: main_loop/* * main loop: listen on stdin (for user input) and master pty (for command output), * and try to write output_queue to master_pty (if it is not empty) * This function never returns. */voidmain_loop(){ int nfds; fd_set readfds; fd_set writefds; int nread; char buf[BUFFSIZE], *timeoutstr, *old_raw_prompt, *new_output_minus_prompt; int promptlen = 0; int leave_prompt_alone; sigset_t no_signals_blocked; int seen_EOF = FALSE; struct timespec select_timeout, *select_timeoutptr; struct timespec immediately = { 0, 0 }; /* zero timeout when child is dead */ struct timespec wait_a_little = {0, 0xBadf00d }; /* tv_usec field will be filled in when initialising */ struct timespec *forever = NULL; wait_a_little.tv_nsec = 1000 * 1000 * wait_before_prompt; sigemptyset(&no_signals_blocked); init_readline(""); last_minute_checks(); pass_through_filter(TAG_OUTPUT,""); /* If something is wrong with filter, get the error NOW */ set_echo(FALSE); /* This will also put the terminal in CBREAK mode */ test_main(); /* ------------------------------ main loop -------------------------------*/ while (TRUE) { /* listen on both stdin and pty_fd */ FD_ZERO(&readfds); FD_SET(STDIN_FILENO, &readfds); FD_SET(master_pty_fd, &readfds); /* try to write output_queue to master_pty (but only if it is nonempty) */ FD_ZERO(&writefds); if (output_queue_is_nonempty()) FD_SET(master_pty_fd, &writefds); DPRINTF1(DEBUG_AD_HOC, "prompt_is_still_uncooked = %d", prompt_is_still_uncooked); if (command_is_dead || ignore_queued_input) { select_timeout = immediately; select_timeoutptr = &select_timeout; timeoutstr = "immediately"; } else if (prompt_is_still_uncooked || polling) { select_timeout = wait_a_little; select_timeoutptr = &select_timeout; timeoutstr = "wait_a_little"; } else { select_timeoutptr = forever; /* NULL */ timeoutstr = "forever"; } DPRINTF1(DEBUG_TERMIO, "calling select() with timeout %s", timeoutstr); nfds = my_pselect(1 + master_pty_fd, &readfds, &writefds, NULL, select_timeoutptr, &no_signals_blocked); DPRINTF3(DEBUG_TERMIO, "select() returned %d (stdin|pty in|pty out = %03d), within_line_edit=%d", nfds, 100*(FD_ISSET(STDIN_FILENO, &readfds)?1:0) + 10*(FD_ISSET(master_pty_fd, &readfds)?1:0) + (FD_ISSET(master_pty_fd, &writefds)?1:0), within_line_edit); assert(!filter_pid || filter_is_dead || kill(filter_pid,0) == 0); assert(command_is_dead || kill(command_pid,0) == 0); /* check flags that may have been set by signal handlers */ if (filter_is_dead) filters_last_words(); /* will call myerror with last words */ if (received_WINCH) { /* received_WINCH flag means we've had a WINCH while within_line_edit was FALSE */ DPRINTF0(DEBUG_READLINE, "Starting line edit as a result of WINCH "); within_line_edit = TRUE; restore_rl_state(); received_WINCH = FALSE; continue; } if (nfds < 0) { /* exception */ if (errno == EINTR || errno == 0) { /* interrupted by signal, or by a cygwin bug (errno == 0) :-( */ continue; } else myerror(FATAL|USE_ERRNO, "select received exception"); } else if (nfds == 0) { /* timeout, which can only happen when .. */ if (ignore_queued_input) { /* ... we have read all the input keystrokes that should be ignored (i.e. those that accumulated on stdin while we were calling an external editor) */ ignore_queued_input = FALSE; continue;//.........这里部分代码省略.........
开发者ID:v2e4lisp,项目名称:rlwrap,代码行数:101,
示例12: mainmain(int argc,char *argv[]){ CoolImage *image; int x,y; int i,j; PtWidget_t *win; PtArg_t args[3]; PhDim_t dim={m_W,m_H}; PhPoint_t pos={50,250}; int fd; //Bt878 driver file descriptor int fd_temp; int size_read; struct timeval tv; fd_set rfd; int n; int error; int counter = 0; int counter_mean = 0; int file = 0; //Timing calculation uint64_t cps, cycle1, cycle2, ncycles; float sec; float msec; // if a paramater was passed, grab it as the blit type if (argc>1) blittype=atoi(argv[1]); // initialize our connection to Photon, and create/realize a window //PtInit("/net/irene2/dev/photon"); PtInit("/dev/photon"); PtSetArg(&args[0],Pt_ARG_POS,&pos,0); PtSetArg(&args[1],Pt_ARG_DIM,&dim,0); win=PtCreateWidget(PtWindow,Pt_NO_PARENT,2,args); PtRealizeWidget(win); // Allocate and fill a series of NUMIMAGES images with a little // fading type animation. Put your own animation in here if you like. /* * Set a 5 second timeout. */ tv.tv_sec = 5; tv.tv_usec = 0; image = AllocBuffer(m_W,m_H,fd); assert(image!=0); if (file != 2) { init_bttvx(2,0, m_W,m_H,0,0); open_bttvx(); BttvxSetImageBuffer(0, image->buffer); } fd_temp = fd; FD_ZERO( &rfd ); FD_SET( fd, &rfd ); while(1) { //fd = open("/net/europa/dev/bttvx0",O_RDWR); //if ( fd > 0 ) //{ ///switch ( n = select( 1 + max( fd,0 ), /// &rfd, 0, 0, &tv ) ) ///{ /// case -1: /// perror( "select" ); /// return EXIT_FAILURE; /// case 0: /// puts( "select timed out" ); /// break; /// default: //printf( "descriptor ready .../n"); //if( FD_ISSET( console, &rfd ) ) // puts( " -- console descriptor has data pending" ); //if( FD_ISSET( serial, &rfd ) ) // puts( " -- serial descriptor has data pending" ); /* Read the text */ cycle1=ClockCycles( ); //lseek(fd,0L,SEEK_SET); /// size_read = read( fd, image->buffer, W*H*deep ); if (file != 2) { BttvxWaitEvent(); BttvxAcquireBuffer(image->buffer); } switch(file) { case 0: BlitBuffer(win,image); break;//.........这里部分代码省略.........
开发者ID:robotology-legacy,项目名称:yarp1,代码行数:101,
示例13: main/* * Simply download a HTTP file. */int main(int argc, char **argv){ CURL *http_handle; CURLM *multi_handle; int still_running; /* keep number of running handles */ http_handle = curl_easy_init(); /* set the options (I left out a few, you'll get the point anyway) */ curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/"); curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace); curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L); /* init a multi stack */ multi_handle = curl_multi_init(); /* add the individual transfers */ curl_multi_add_handle(multi_handle, http_handle); /* we start some action by calling perform right away */ while(CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &still_running)); while(still_running) { struct timeval timeout; int rc; /* select() return code */ fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd = -1; FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); /* set a suitable timeout to play around with */ timeout.tv_sec = 1; timeout.tv_usec = 0; /* get file descriptors from the transfers */ curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); /* In a real-world program you OF COURSE check the return code of the function calls. On success, the value of maxfd is guaranteed to be greater or equal than -1. We call select(maxfd + 1, ...), specially in case of (maxfd == -1), we call select(0, ...), which is basically equal to sleep. */ rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); switch(rc) { case -1: /* select error */ still_running = 0; printf("select() returns error, this is badness/n"); break; case 0: default: /* timeout or readable/writable sockets */ while(CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &still_running)); break; } } curl_multi_cleanup(multi_handle); curl_easy_cleanup(http_handle); return 0;}
开发者ID:AndroidAppList,项目名称:Android-Supertux,代码行数:77,
示例14: test//.........这里部分代码省略......... curl_easy_setopt(c, CURLOPT_PROXY, arg2); curl_easy_setopt(c, CURLOPT_URL, URL); curl_easy_setopt(c, CURLOPT_VERBOSE, 1); if ((m = curl_multi_init()) == NULL) { fprintf(stderr, "curl_multi_init() failed/n"); curl_easy_cleanup(c); curl_global_cleanup(); return TEST_ERR_MAJOR_BAD; } if ((res = curl_multi_add_handle(m, c)) != CURLM_OK) { fprintf(stderr, "curl_multi_add_handle() failed, " "with code %d/n", res); curl_multi_cleanup(m); curl_easy_cleanup(c); curl_global_cleanup(); return TEST_ERR_MAJOR_BAD; } ml_timedout = FALSE; ml_start = tutil_tvnow(); while (!done) { struct timeval interval; interval.tv_sec = 1; interval.tv_usec = 0; if (tutil_tvdiff(tutil_tvnow(), ml_start) > MAIN_LOOP_HANG_TIMEOUT) { ml_timedout = TRUE; break; } mp_timedout = FALSE; mp_start = tutil_tvnow(); fprintf(stderr, "curl_multi_perform()/n"); res = CURLM_CALL_MULTI_PERFORM; while (res == CURLM_CALL_MULTI_PERFORM) { res = curl_multi_perform(m, &running); if (tutil_tvdiff(tutil_tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) { mp_timedout = TRUE; break; } } if (mp_timedout) break; if(!running) { /* This is where this code is expected to reach */ int numleft; CURLMsg *msg = curl_multi_info_read(m, &numleft); fprintf(stderr, "Expected: not running/n"); if(msg && !numleft) ret = 100; /* this is where we should be */ else ret = 99; /* not correct */ break; } fprintf(stderr, "running == %d, res == %d/n", running, res); if (res != CURLM_OK) { ret = 2; break; } FD_ZERO(&rd); FD_ZERO(&wr); FD_ZERO(&exc); max_fd = 0; fprintf(stderr, "curl_multi_fdset()/n"); if (curl_multi_fdset(m, &rd, &wr, &exc, &max_fd) != CURLM_OK) { fprintf(stderr, "unexpected failured of fdset./n"); ret = 3; break; } rc = select_test(max_fd+1, &rd, &wr, &exc, &interval); fprintf(stderr, "select returned %d/n", rc); } if (ml_timedout || mp_timedout) { if (ml_timedout) fprintf(stderr, "ml_timedout/n"); if (mp_timedout) fprintf(stderr, "mp_timedout/n"); fprintf(stderr, "ABORTING TEST, since it seems " "that it would have run forever./n"); ret = TEST_ERR_RUNS_FOREVER; } curl_multi_remove_handle(m, c); curl_easy_cleanup(c); curl_multi_cleanup(m); curl_global_cleanup(); return ret;}
开发者ID:irmametra,项目名称:EiffelStudio,代码行数:101,
示例15: redisContextWaitReadystatic int redisContextWaitReady(redisContext *c, const struct timeval *timeout) {#ifdef FASTO #ifdef OS_WIN fd_set master_set; FD_ZERO(&master_set); int max_sd = c->fd; FD_SET(c->fd, &master_set); struct timeval tm; tm.tv_sec = 60; tm.tv_usec = 0; /* Only use timeout when not NULL. */ if (timeout != NULL) { if (timeout->tv_usec > 1000000 || timeout->tv_sec > __MAX_MSEC) { redisContextCloseFd(c); return REDIS_ERR; } tm = *timeout; } if (errno == EINPROGRESS) { int res; if ((res = select(max_sd + 1, &master_set, NULL, NULL, &tm)) == -1) { __redisSetErrorFromErrno(c, REDIS_ERR_IO, "select(2)"); redisContextCloseFd(c); return REDIS_ERR; } else if (res == 0) { errno = ETIMEDOUT; __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL); redisContextCloseFd(c); return REDIS_ERR; } if (redisCheckSocketError(c) != REDIS_OK) return REDIS_ERR; return REDIS_OK; } __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL); redisContextCloseFd(c); return REDIS_ERR; #else struct pollfd wfd[1]; long msec; msec = -1; wfd[0].fd = c->fd; wfd[0].events = POLLOUT; /* Only use timeout when not NULL. */ if (timeout != NULL) { if (timeout->tv_usec > 1000000 || timeout->tv_sec > __MAX_MSEC) { __redisSetErrorFromErrno(c, REDIS_ERR_IO, NULL); redisContextCloseFd(c); return REDIS_ERR; } msec = (timeout->tv_sec * 1000) + ((timeout->tv_usec + 999) / 1000); if (msec < 0 || msec > INT_MAX) { msec = INT_MAX; } } if (errno == EINPROGRESS) { int res; if ((res = poll(wfd, 1, msec)) == -1) { __redisSetErrorFromErrno(c, REDIS_ERR_IO, "poll(2)"); redisContextCloseFd(c); return REDIS_ERR; } else if (res == 0) { errno = ETIMEDOUT; __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL); redisContextCloseFd(c); return REDIS_ERR; } if (redisCheckSocketError(c) != REDIS_OK) return REDIS_ERR; return REDIS_OK; } __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL); redisContextCloseFd(c); return REDIS_ERR; #endif#else struct pollfd wfd[1]; long msec; msec = -1; wfd[0].fd = c->fd; wfd[0].events = POLLOUT; /* Only use timeout when not NULL. *///.........这里部分代码省略.........
开发者ID:topilski,项目名称:fastonosql,代码行数:101,
示例16: FD_ZEROvoid BaseSocketManager::DoSelect(int pauseMicroSecs, int handleInput) { timeval tv; tv.tv_sec = 0; tv.tv_usec = pauseMicroSecs; fd_set inp_set, out_set, exc_set; int maxDesc; FD_ZERO(&inp_set); FD_ZERO(&out_set); FD_ZERO(&exc_set); maxDesc = 0; for (SocketList::iterator i = m_SockList.begin(); i != m_SockList.end(); ++i) { NetSocket* pSock = *i; if ((pSock->m_deleteFlag & 1) || pSock->m_sock == INVALID_SOCKET) continue; if (handleInput) FD_SET(pSock->m_sock, &inp_set); FD_SET(pSock->m_sock, &exc_set); if (pSock->VHasOutput()) FD_SET(pSock->m_sock, &out_set); if ((int)pSock->m_sock > maxDesc) maxDesc = (int)pSock->m_sock; } int selRet = 0; selRet = select(maxDesc + 1, &inp_set, &out_set, &exc_set, &tv); if (selRet == SOCKET_ERROR) { GCC_ERROR("Error in DoSelect!"); return; } if (selRet) { for (SocketList::iterator i = m_SockList.begin(); i != m_SockList.end(); ++i) { NetSocket* pSock = *i; if ((pSock->m_deleteFlag & 1) || pSock->m_sock == INVALID_SOCKET) continue; if (FD_ISSET(pSock->m_sock, &exc_set)) pSock->HandleException(); if (!(pSock->m_deleteFlag & 1) && FD_ISSET(pSock->m_sock, &out_set)) pSock->VHandleOutput(); if (handleInput && !(pSock->m_deleteFlag & 1) && FD_ISSET(pSock->m_sock, &inp_set)) { pSock->VHandleInput(); } } } unsigned int timeNow = timeGetTime(); SocketList::iterator i = m_SockList.begin(); while (i != m_SockList.end()) { NetSocket* pSock = *i; if (pSock->m_timeOut && pSock->m_timeOut < timeNow) pSock->VTimeOut(); if (pSock->m_deleteFlag & 1) { switch (pSock->m_deleteFlag) { case 1: g_pSocketManager->RemoveSocket(pSock); i = m_SockList.begin(); break; case 3: pSock->m_deleteFlag = 2; if (pSock->m_sock != INVALID_SOCKET) { closesocket(pSock->m_sock); pSock->m_sock = INVALID_SOCKET; } break; } } i++; }}
开发者ID:snikk,项目名称:Junk,代码行数:86,
示例17: Curl_SSLConnect//.........这里部分代码省略......... /* Lets make an SSL structure */ conn->ssl.handle = SSL_new (conn->ssl.ctx); SSL_set_connect_state (conn->ssl.handle); conn->ssl.server_cert = 0x0; if(!conn->bits.reuse) { /* We're not re-using a connection, check if there's a cached ID we can/should use here! */ if(!Get_SSL_Session(conn, &ssl_sessionid)) { /* we got a session id, use it! */ SSL_set_session(conn->ssl.handle, ssl_sessionid); /* Informational message */ infof (data, "SSL re-using session ID/n"); } } /* pass the raw socket into the SSL layers */ SSL_set_fd(conn->ssl.handle, conn->firstsocket); do { int what; fd_set writefd; fd_set readfd; struct timeval interval; long timeout_ms; err = SSL_connect(conn->ssl.handle); what = SSL_get_error(conn->ssl.handle, err); FD_ZERO(&writefd); FD_ZERO(&readfd); if(SSL_ERROR_WANT_READ == what) FD_SET(conn->firstsocket, &readfd); else if(SSL_ERROR_WANT_WRITE == what) FD_SET(conn->firstsocket, &writefd); else break; /* untreated error */ /* Find out if any timeout is set. If not, use 300 seconds. Otherwise, figure out the most strict timeout of the two possible one and then how much time that has elapsed to know how much time we allow for the connect call */ if(data->set.timeout || data->set.connecttimeout) { double has_passed; /* Evaluate in milliseconds how much time that has passed */ has_passed = Curl_tvdiff(Curl_tvnow(), data->progress.start);#ifndef min#define min(a, b) ((a) < (b) ? (a) : (b))#endif /* get the most strict timeout of the ones converted to milliseconds */ if(data->set.timeout && (data->set.timeout>data->set.connecttimeout)) timeout_ms = data->set.timeout*1000; else timeout_ms = data->set.connecttimeout*1000; /* subtract the passed time */ timeout_ms -= (long)has_passed;
开发者ID:BackupTheBerlios,项目名称:texlive,代码行数:67,
示例18: revnameint revname(int *lookup, struct in_addr *saddr, struct in6_addr *s6addr, char *target, int rvnfd){ struct hostent *he; struct rvn rpkt; int br; struct sockaddr_un su; socklen_t fl; fd_set sockset; struct timeval tv; int sstat = 0; memset(target, 0, 45); if (*lookup) { if (rvnfd > 0) { su.sun_family = AF_UNIX; strcpy(su.sun_path, IPTSOCKNAME); rpkt.type = RVN_REQUEST; rpkt.saddr.s_addr = saddr->s_addr; if (s6addr != NULL) memcpy(rpkt.s6addr.s6_addr, s6addr->s6_addr, 16); else memset(rpkt.s6addr.s6_addr, 0, 4); sendto(rvnfd, &rpkt, sizeof(struct rvn), 0, (struct sockaddr *) &su, sizeof(su.sun_family) + strlen(su.sun_path)); fl = sizeof(su.sun_family) + strlen(su.sun_path); do { tv.tv_sec = 10; tv.tv_usec = 0; FD_ZERO(&sockset); FD_SET(rvnfd, &sockset); do { sstat = select(rvnfd + 1, &sockset, NULL, NULL, &tv); } while ((sstat < 0) && (errno == EINTR)); if (FD_ISSET(rvnfd, &sockset)) br = recvfrom(rvnfd, &rpkt, sizeof(struct rvn), 0, (struct sockaddr *) &su, &fl); else br = -1; } while ((br < 0) && (errno == EINTR)); if (br < 0) { if (saddr->s_addr != 0) strcpy(target, inet_ntoa(*saddr)); else inet_ntop(AF_INET6, s6addr, target, 44); printipcerr(); *lookup = 0; return RESOLVED; } strncpy(target, rpkt.fqdn, 44); return (rpkt.ready); } else { if (saddr->s_addr != 0) he = gethostbyaddr((char *) saddr, sizeof(struct in_addr), AF_INET); else he = gethostbyaddr((char *) s6addr, sizeof(struct in6_addr), AF_INET6); if (he == NULL) { if (saddr->s_addr != 0) strcpy(target, inet_ntoa(*saddr)); else inet_ntop(AF_INET6, s6addr, target, 44); } else { strncpy(target, he->h_name, 44); } return RESOLVED; } } else { if (saddr->s_addr != 0 || s6addr == NULL) strcpy(target, inet_ntoa(*saddr)); else inet_ntop(AF_INET6, s6addr, target, 44); return RESOLVED; } return NOTRESOLVED;}
开发者ID:ebichu,项目名称:dd-wrt,代码行数:96,
示例19: mainint main (int argc, char *argv[]){ int i, len, rc, on = 1; int listen_sd, max_sd, new_sd; int desc_ready, end_server = FALSE; int close_conn; char buffer[8]; struct sockaddr_in addr; struct timeval timeout;#if _ORI struct fd_set master_set, working_set;#else fd_set master_set, working_set;#endif /*************************************************************/ /* Create an AF_INET stream socket to receive incoming */ /* connections on */ /*************************************************************/ listen_sd = socket(AF_INET, SOCK_STREAM, 0); if (listen_sd < 0) { perror("socket() failed"); exit(-1); } /*************************************************************/ /* Allow socket descriptor to be reuseable */ /*************************************************************/ rc = setsockopt(listen_sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); if (rc < 0) { perror("setsockopt() failed"); close(listen_sd); exit(-1); } /*************************************************************/ /* Set socket to be non-blocking. All of the sockets for */ /* the incoming connections will also be non-blocking since */ /* they will inherit that state from the listening socket. */ /*************************************************************/#ifdef _use_setnonblocking setnonblocking(listen_sd);#else rc = ioctl(listen_sd, FIONBIO, (char *)&on); if (rc < 0) { perror("ioctl() failed"); close(listen_sd); exit(-1); }#endif /*************************************************************/ /* Bind the socket */ /*************************************************************/ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(SERVER_PORT); rc = bind(listen_sd, (struct sockaddr *)&addr, sizeof(addr)); if (rc < 0) { perror("bind() failed"); close(listen_sd); exit(-1); } /*************************************************************/ /* Set the listen back log */ /*************************************************************/ rc = listen(listen_sd, BACKLOG); if (rc < 0) { perror("listen() failed"); close(listen_sd); exit(-1); } /*************************************************************/ /* Initialize the master fd_set */ /*************************************************************/ FD_ZERO(&master_set); max_sd = listen_sd; FD_SET(listen_sd, &master_set); /*************************************************************/ /* Initialize the timeval struct to 3 minutes. If no */ /* activity after 3 minutes this program will end. */ /*************************************************************/ timeout.tv_sec = 3 * 60; timeout.tv_usec = 0; /*************************************************************/ /* Loop waiting for incoming connects or for incoming data */ /* on any of the connected sockets. *///.........这里部分代码省略.........
开发者ID:Masshat,项目名称:C_and_CPP,代码行数:101,
示例20: MAKEWORDSelectServer *net_serverA(tagitem *tags){ SelectServer *server; int true, rc;/* Win32 wants to be weird */#ifdef Win32_Winsock WORD version_wanted = MAKEWORD(1,1); WSADATA wsaData; if(WSAStartup(version_wanted, &wsaData)) { fprintf(stderr, "Couldn't initialize Winsock 1.1/n"); return 0; }#endif server = calloc(1, sizeof(SelectServer)); if(!server) return NULL; server->queue = 5; /* Get descriptor for listening socket */ server->sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(server->sock < 0) { perror("socket"); free(server); return NULL; } /* We want to bind without TIME_WAIT problems */ true = 1; rc = setsockopt(server->sock, SOL_SOCKET, SO_REUSEADDR, (char *)&true, sizeof(true)); if(rc < 0) { perror("setsockopt"); free(server); return NULL; } net_setnonblock(server->sock); server->connectlist.destructor = (void *)&destroy_socket; server->available.destructor = (void *)&destroy_socket; server->address.sin_family = AF_INET; server->address.sin_addr.s_addr = _SwapBE32(INADDR_ANY); net_tag_serverA(server, tags); rc =bind(server->sock, (struct sockaddr *) &server->address, sizeof(server->address)); if(rc < 0) { perror("bind"); net_server_free(server); return NULL; } rc = listen(server->sock, server->queue); if(rc == -1) { perror("listen"); net_server_free(server); return NULL; } FD_ZERO(&server->set); FD_SET(server->sock, &server->set); return server;}
开发者ID:BackupTheBerlios,项目名称:xiqual-svn,代码行数:63,
示例21: ldap_pvt_connect//.........这里部分代码省略......... msg.msg_iov = &iov; msg.msg_iovlen = 1;# ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL msg.msg_control = control_un.control; msg.msg_controllen = sizeof( control_un.control ); msg.msg_flags = 0; cmsg = CMSG_FIRSTHDR( &msg ); cmsg->cmsg_len = CMSG_LEN( sizeof(int) ); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; *((int *)CMSG_DATA(cmsg)) = fds[0];# else msg.msg_accrights = (char *)fds; msg.msg_accrightslen = sizeof(int);# endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */ getpeername( s, sa, &salen ); fchmod( fds[0], S_ISUID|S_IRWXU ); write( fds[1], sa, salen ); sendmsg( s, &msg, 0 ); close(fds[0]); close(fds[1]); } }#endif return 0; } if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) return -1; #ifdef notyet if ( async ) return -2;#endif#ifdef HAVE_POLL { struct pollfd fd; int timeout = INFTIM; if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv ); fd.fd = s; fd.events = POLL_WRITE; do { fd.revents = 0; rc = poll( &fd, 1, timeout ); } while( rc == AC_SOCKET_ERROR && errno == EINTR && LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART )); if( rc == AC_SOCKET_ERROR ) return rc; if( fd.revents & POLL_WRITE ) { if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1; if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;#ifdef LDAP_PF_LOCAL_SENDMSG goto sendcred;#else return ( 0 );#endif } }#else { fd_set wfds, *z=NULL;#ifdef FD_SETSIZE if ( s >= FD_SETSIZE ) { rc = AC_SOCKET_ERROR; tcp_close( s ); ldap_pvt_set_errno( EMFILE ); return rc; }#endif do { FD_ZERO(&wfds); FD_SET(s, &wfds ); rc = select( ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL ); } while( rc == AC_SOCKET_ERROR && errno == EINTR && LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART )); if( rc == AC_SOCKET_ERROR ) return rc; if ( FD_ISSET(s, &wfds) ) { if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1; if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;#ifdef LDAP_PF_LOCAL_SENDMSG goto sendcred;#else return ( 0 );#endif } }#endif oslocal_debug(ld, "ldap_connect_timeout: timed out/n",0,0,0); ldap_pvt_set_errno( ETIMEDOUT ); return ( -1 );}
开发者ID:FarazShaikh,项目名称:LikewiseSMB2,代码行数:101,
示例22: testExternalGetstatic inttestExternalGet (){ struct MHD_Daemon *d; CURL *c; char buf[2048]; struct CBC cbc; CURLM *multi; CURLMcode mret; fd_set rs; fd_set ws; fd_set es; MHD_socket maxsock;#ifdef MHD_WINSOCK_SOCKETS int maxposixs; /* Max socket number unused on W32 */#else /* MHD_POSIX_SOCKETS */#define maxposixs maxsock#endif /* MHD_POSIX_SOCKETS */ int running; struct CURLMsg *msg; time_t start; struct timeval tv; multi = NULL; cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; d = MHD_start_daemon (MHD_USE_DEBUG, 21080, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END); if (d == NULL) return 256; c = curl_easy_init (); curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1:21080/hello+world?k=v+x&hash=%23foo&space=%A0bar"); curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); if (oneone) curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); /* NOTE: use of CONNECTTIMEOUT without also setting NOSIGNAL results in really weird crashes on my system! */ curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); multi = curl_multi_init (); if (multi == NULL) { curl_easy_cleanup (c); MHD_stop_daemon (d); return 512; } mret = curl_multi_add_handle (multi, c); if (mret != CURLM_OK) { curl_multi_cleanup (multi); curl_easy_cleanup (c); MHD_stop_daemon (d); return 1024; } start = time (NULL); while ((time (NULL) - start < 5) && (multi != NULL)) { maxsock = MHD_INVALID_SOCKET; maxposixs = -1; FD_ZERO (&rs); FD_ZERO (&ws); FD_ZERO (&es); curl_multi_perform (multi, &running); mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs); if (mret != CURLM_OK) { curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); MHD_stop_daemon (d); return 2048; } if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock)) { curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); MHD_stop_daemon (d); return 4096; } tv.tv_sec = 0; tv.tv_usec = 1000; if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv)) { if (EINTR != errno) abort (); } curl_multi_perform (multi, &running); if (running == 0) {//.........这里部分代码省略.........
开发者ID:svn2github,项目名称:libmicrohttpd,代码行数:101,
示例23: ident_checkvoid ident_check(struct descriptor_data *d, int pulse){ fd_set fd, efd; int rc, rmt_port, our_port, len; char user[256], *p; extern struct timeval null_time; extern int port; /* * Each pulse, this checks if the ident is ready to proceed to the * next state, by calling select to see if the socket is writeable * (connected) or readable (response waiting). */ switch (STATE(d)) { case CON_IDCONING: /* waiting for connect() to finish */ if (d->ident_sock != INVALID_SOCKET) { FD_ZERO(&fd); FD_ZERO(&efd); FD_SET(d->ident_sock, &fd); FD_SET(d->ident_sock, &efd); } if ((rc = select(d->ident_sock + 1, (fd_set *) 0, &fd, &efd, &null_time)) == 0) break; else if (rc < 0) { logerror("ident check select (conning)"); STATE(d) = CON_ASKNAME; break; } if (FD_ISSET(d->ident_sock, &efd)) { /* exception, such as failure to connect */ STATE(d) = CON_ASKNAME; break; } STATE(d) = CON_IDCONED; break; case CON_IDCONED: /* connected, write request */ sprintf(buf, "%d, %d/n/r", ntohs(d->peer_port), port); len = strlen(buf);#ifdef CIRCLE_WINDOWS if (send(d->ident_sock, buf, len, 0) < 0) {#else if (write(d->ident_sock, buf, len) != len) { if (errno != EPIPE) /* read end closed (no remote identd) */#endif logerror("ident check write (conned)"); STATE(d) = CON_ASKNAME; break; } STATE(d) = CON_IDREADING; break; case CON_IDREADING: /* waiting to read */ if (d->ident_sock != INVALID_SOCKET) { FD_ZERO(&fd); FD_ZERO(&efd); FD_SET(d->ident_sock, &fd); FD_SET(d->ident_sock, &efd); } if ((rc = select(d->ident_sock + 1, &fd, (fd_set *) 0, &efd, &null_time)) == 0) break; else if (rc < 0) { logerror("ident check select (reading)"); STATE(d) = CON_ASKNAME; break; } if (FD_ISSET(d->ident_sock, &efd)) { STATE(d) = CON_ASKNAME; break; } STATE(d) = CON_IDREAD; break; case CON_IDREAD: /* read ready, get the info */#ifdef CIRCLE_WINDOWS if ((len = recv(d->ident_sock, buf, sizeof(buf) - 1, 0)) < 0)#else if ((len = read(d->ident_sock, buf, sizeof(buf) - 1)) < 0)#endif//.........这里部分代码省略.........
开发者ID:nawglan,项目名称:ShadowWind,代码行数:101,
示例24: mainint main(int argc, char *argv[]){ //Game vars int playerfd[2]; char playerid = '1'; char board[BSIZE][BSIZE]; char bufrack[8]; int x, y, num; char bufx[3], bufy[3]; int bag[27] = {9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1, 2}; char **dict; char word[MAXWORDSIZE]; char bufout[MAXDATASIZE]; dict = opendict(DICT); srandom(time(NULL)); clearsqarray(BSIZE, board); int listens, newsock, sin_size, bigfd, numbytes, i, j; char buffer[MAXDATASIZE]; struct sockaddr_in localaddr, newaddr, getaddr; fd_set master, tempfd; int yes = 1; listens = socket(PF_INET, SOCK_STREAM, 0); setsockopt(listens, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); localaddr.sin_family = AF_INET; localaddr.sin_port = htons(PORT); localaddr.sin_addr.s_addr = INADDR_ANY; memset(&(localaddr.sin_zero), '/0', sizeof(localaddr.sin_zero)); bind(listens, (struct sockaddr *) &localaddr, sizeof(localaddr)); listen(listens, 10); FD_ZERO(&master); FD_ZERO(&tempfd); FD_SET(listens, &master); bigfd = listens; for(;;) { tempfd = master; select(bigfd + 1, &tempfd, NULL, NULL, NULL); for(i = 0; i <= bigfd; i++) { if(FD_ISSET(i, &tempfd)) { if(i == listens) { sin_size = sizeof(newaddr); newsock = accept(listens, (struct sockaddr *) &newaddr, &sin_size); FD_SET(newsock, &master); if(newsock > bigfd) bigfd = newsock; printf("Connection from: %s Socket: %i/n", inet_ntoa(newaddr.sin_addr), newsock); //playerid stuff if(atoi(&playerid) < 2) playerfd[atoi(&playerid) - 1] = newsock; send(newsock, &playerid, sizeof(char), 0); playerid++; //send rack randomrack(bufrack, bag, RACKSIZE); send(newsock, bufrack, strlen(bufrack), 0); } else { if((numbytes = recv(i, buffer, sizeof(buffer), 0)) == 0) { printf("Connection closed: socket %i/n", i); if(playerid == '3' || playerid == '1') playerid = '1'; else playerid--; close(i); FD_CLR(i, &master); } else if(numbytes < 0) { perror("recv"); } else { //Data handling getpeername(i, (struct sockaddr *) &getaddr, &sin_size); if(buffer[0] != 'c' && buffer [0] != 'r' && buffer[0] != 'p')//.........这里部分代码省略.........
开发者ID:WraithM,项目名称:cscrabble,代码行数:101,
示例25: run_commandstatic ssize_t run_command(int sockfd, char const *command, char *buffer, size_t bufsize){ char *p; ssize_t size, len; if (echo) { fprintf(outputfp, "%s/n", command); } /* * Write the text to the socket. */ if (write(sockfd, command, strlen(command)) < 0) return -1; if (write(sockfd, "/r/n", 2) < 0) return -1; /* * Read the response */ size = 0; buffer[0] = '/0'; memset(buffer, 0, bufsize); while (1) { int rcode; fd_set readfds; FD_ZERO(&readfds); FD_SET(sockfd, &readfds); rcode = select(sockfd + 1, &readfds, NULL, NULL, NULL); if (rcode < 0) { if (errno == EINTR) continue; fprintf(stderr, "%s: Failed selecting: %s/n", progname, strerror(errno)); exit(1); } if (rcode == 0) { fprintf(stderr, "%s: Server closed the connection./n", progname); exit(1); }#ifdef MSG_DONTWAIT len = recv(sockfd, buffer + size, bufsize - size - 1, MSG_DONTWAIT);#else /* * Read one byte at a time (ugh) */ len = recv(sockfd, buffer + size, 1, 0);#endif if (len < 0) { /* * No data: keep looping */ if ((errno == EAGAIN) || (errno == EINTR)) { continue; } fprintf(stderr, "%s: Error reading socket: %s/n", progname, strerror(errno)); exit(1); } if (len == 0) return 0; /* clean exit */ size += len; buffer[size] = '/0'; /* * There really is a better way of doing this. */ p = strstr(buffer, "radmin> "); if (p && ((p == buffer) || (p[-1] == '/n') || (p[-1] == '/r'))) { *p = '/0'; if (p[-1] == '/n') p[-1] = '/0'; break; } } /* * Blank prompt. Go get another command. */ if (!buffer[0]) return 1; buffer[size] = '/0'; /* this is at least right */ return 2;}
开发者ID:RockalotofPokadots,项目名称:freeradius-server,代码行数:96,
示例26: mainint main( int argc, char *argv[] ){ int ret; mbedtls_net_context listen_fd, client_fd, server_fd; int nb_fds; fd_set read_fds; mbedtls_net_init( &listen_fd ); mbedtls_net_init( &client_fd ); mbedtls_net_init( &server_fd ); get_options( argc, argv ); /* * Decisions to drop/delay/duplicate packets are pseudo-random: dropping * exactly 1 in N packets would lead to problems when a flight has exactly * N packets: the same packet would be dropped on every resend. * * In order to be able to reproduce problems reliably, the seed may be * specified explicitly. */ if( opt.seed == 0 ) { opt.seed = (unsigned int) time( NULL ); mbedtls_printf( " . Pseudo-random seed: %u/n", opt.seed ); } srand( opt.seed ); /* * 0. "Connect" to the server */ mbedtls_printf( " . Connect to server on UDP/%s/%s ...", opt.server_addr, opt.server_port ); fflush( stdout ); if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port, MBEDTLS_NET_PROTO_UDP ) ) != 0 ) { mbedtls_printf( " failed/n ! mbedtls_net_connect returned %d/n/n", ret ); goto exit; } mbedtls_printf( " ok/n" ); /* * 1. Setup the "listening" UDP socket */ mbedtls_printf( " . Bind on UDP/%s/%s ...", opt.listen_addr, opt.listen_port ); fflush( stdout ); if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port, MBEDTLS_NET_PROTO_UDP ) ) != 0 ) { mbedtls_printf( " failed/n ! mbedtls_net_bind returned %d/n/n", ret ); goto exit; } mbedtls_printf( " ok/n" ); /* * 2. Wait until a client connects */accept: mbedtls_net_free( &client_fd ); mbedtls_printf( " . Waiting for a remote connection ..." ); fflush( stdout ); if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd, NULL, 0, NULL ) ) != 0 ) { mbedtls_printf( " failed/n ! mbedtls_net_accept returned %d/n/n", ret ); goto exit; } mbedtls_printf( " ok/n" ); /* * 3. Forward packets forever (kill the process to terminate it) */ clear_pending(); memset( dropped, 0, sizeof( dropped ) ); nb_fds = client_fd.fd; if( nb_fds < server_fd.fd ) nb_fds = server_fd.fd; if( nb_fds < listen_fd.fd ) nb_fds = listen_fd.fd; ++nb_fds; while( 1 ) { FD_ZERO( &read_fds ); FD_SET( server_fd.fd, &read_fds ); FD_SET( client_fd.fd, &read_fds ); FD_SET( listen_fd.fd, &read_fds );//.........这里部分代码省略.........
开发者ID:1514louluo,项目名称:mbedtls,代码行数:101,
示例27: FD_ZERO void HttpServer::WaitMessage(uint32_t ms) { fd_set fdsr; struct timeval tv; int max_sock = m_sock; tv.tv_sec = ms / 1000; tv.tv_usec = (ms % 1000 ) / 1000; FD_ZERO(&fdsr);#ifdef _WIN32 /* on Windows, a socket is not an int but a SOCKET (unsigned int) */ FD_SET((SOCKET)m_sock, &fdsr);#else FD_SET(m_sock, &fdsr);#endif for(std::list<int>::iterator it = m_clients.begin() ; it != m_clients.end() ; ++it) {#ifdef _WIN32 FD_SET((SOCKET)(*it), &fdsr);#else FD_SET((*it), &fdsr);#endif if((*it) > max_sock) { max_sock = (*it); } } max_sock++; if(select(max_sock, &fdsr, NULL, NULL, ms ? &tv : NULL) > 0) { if(FD_ISSET(m_sock, &fdsr)) { Accept(); } for(std::list<int>::iterator it = m_clients.begin() ; it != m_clients.end() ; ++it) { if(FD_ISSET((*it), &fdsr)) { Recv((*it)); } } /* remove disconnect socket descriptor */ for(std::list<int>::iterator it = m_purge.begin() ; it != m_purge.end() ; ++it) { m_clients.remove((*it)); m_httpmsgs.erase((*it)); } /* purge disconnected list */ m_purge.erase(m_purge.begin(), m_purge.end()); } else { /* error */ } }
开发者ID:un44444444,项目名称:jsonrpc-cpp-mod-http,代码行数:64,
示例28: pollint zmq::signaler_t::wait (int timeout_){#ifdef HAVE_FORK if (unlikely(pid != getpid())) { // we have forked and the file descriptor is closed. Emulate an interupt // response. //printf("Child process %d signaler_t::wait returning simulating interrupt #1/n", getpid()); errno = EINTR; return -1; }#endif#ifdef ZMQ_SIGNALER_WAIT_BASED_ON_POLL struct pollfd pfd; pfd.fd = r; pfd.events = POLLIN; int rc = poll (&pfd, 1, timeout_); if (unlikely (rc < 0)) { errno_assert (errno == EINTR); return -1; } else if (unlikely (rc == 0)) { errno = EAGAIN; return -1; }#ifdef HAVE_FORK if (unlikely(pid != getpid())) { // we have forked and the file descriptor is closed. Emulate an interupt // response. //printf("Child process %d signaler_t::wait returning simulating interrupt #2/n", getpid()); errno = EINTR; return -1; }#endif zmq_assert (rc == 1); zmq_assert (pfd.revents & POLLIN); return 0;#elif defined ZMQ_SIGNALER_WAIT_BASED_ON_SELECT fd_set fds; FD_ZERO (&fds); FD_SET (r, &fds); struct timeval timeout; if (timeout_ >= 0) { timeout.tv_sec = timeout_ / 1000; timeout.tv_usec = timeout_ % 1000 * 1000; }#ifdef ZMQ_HAVE_WINDOWS int rc = select (0, &fds, NULL, NULL, timeout_ >= 0 ? &timeout : NULL); wsa_assert (rc != SOCKET_ERROR);#else int rc = select (r + 1, &fds, NULL, NULL, timeout_ >= 0 ? &timeout : NULL); if (unlikely (rc < 0)) { errno_assert (errno == EINTR); return -1; }#endif if (unlikely (rc == 0)) { errno = EAGAIN; return -1; } zmq_assert (rc == 1); return 0;#else#error#endif}
开发者ID:EricMCornelius,项目名称:libzmq,代码行数:74,
示例29: mainint main(int argc, char **argv){ int fd = socket(AF_INET, SOCK_STREAM, 0); // TCP int on = 1; setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on); struct sockaddr_in srvaddr; bzero(&srvaddr, sizeof srvaddr); srvaddr.sin_family = AF_INET; srvaddr.sin_addr.s_addr = inet_addr(HOST); srvaddr.sin_port = htons(DEFAULT_PORT); if(connect(fd, (struct sockaddr *)&srvaddr, sizeof srvaddr) == -1) { perror("connect() error"); exit(1); } struct sockaddr_in myaddr; socklen_t len = sizeof myaddr; bzero(&myaddr, len); getsockname(fd, (struct sockaddr *)&myaddr, &len); printf("my port: %hu/n", htons(myaddr.sin_port)); //pthread_t tid; //pthread_create(&tid, NULL, routine, (void *)fd); char buf[100]; fd_set rset, wset, eset; // 这些是描述符集合 struct timeval a; a.tv_sec = 0; a.tv_usec = 0; while(1) {#if 0 FD_ZERO(&rset); // 添加在这里面的描述符,我们关注他们的“读就绪”状态 FD_ZERO(&wset); // 添加在这里面的描述符,我们关注他们的“写就绪”状态 FD_ZERO(&eset); // 添加在这里面的描述符,我们关注他们的“异常就绪”状态 FD_SET(fd, &rset); FD_SET(STDIN_FILENO, &rset); // 将两个描述符添加到rset里面! select(fd+1, &rset, &wset, &eset, &a); bzero(&buf, 100); if(FD_ISSET(STDIN_FILENO, &rset)) // 用户输入数据了! { fgets(buf, 100, stdin); write(fd, buf, strlen(buf)); }#endif FD_ZERO(&rset); // 添加在这里面的描述符,我们关注他们的“读就绪”状态 FD_ZERO(&wset); // 添加在这里面的描述符,我们关注他们的“写就绪”状态 FD_ZERO(&eset); // 添加在这里面的描述符,我们关注他们的“异常就绪”状态 FD_SET(fd, &rset); FD_SET(STDIN_FILENO, &rset); // 将两个描述符添加到rset里面! select(fd+1, &rset, &wset, &eset, &a); char buf[100]; bzero(buf, 100); if(FD_ISSET(fd, &rset)) // 对方有消息来了! { if(read(fd, buf, 100) == 0) break; printf("broadcast: %s/n", buf); } if(FD_ISSET(STDIN_FILENO, &rset)) // 用户输入数据了! { fgets(buf, 100, stdin); write(fd, buf, strlen(buf)); } } return 0;}
开发者ID:lanselambor,项目名称:HomeServer,代码行数:80,
示例30: server_init//.........这里部分代码省略......... if (l2sock < 0) { log_crit("Could not create L2CAP socket. %s (%d)", strerror(errno), errno); close(unsock); return (-1); } size = sizeof(imtu); if (getsockopt(l2sock, SOL_L2CAP, SO_L2CAP_IMTU, &imtu, &size) < 0) { log_crit("Could not get L2CAP IMTU. %s (%d)", strerror(errno), errno); close(unsock); close(l2sock); return (-1); } memset(&l2, 0, sizeof(l2)); l2.l2cap_len = sizeof(l2); l2.l2cap_family = AF_BLUETOOTH; memcpy(&l2.l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(l2.l2cap_bdaddr)); l2.l2cap_psm = htole16(NG_L2CAP_PSM_SDP); if (bind(l2sock, (struct sockaddr *) &l2, sizeof(l2)) < 0) { log_crit("Could not bind L2CAP socket. %s (%d)", strerror(errno), errno); close(unsock); close(l2sock); return (-1); } if (listen(l2sock, 10) < 0) { log_crit("Could not listen on L2CAP socket. %s (%d)", strerror(errno), errno); close(unsock); close(l2sock); return (-1); } /* Allocate incoming buffer */ srv->imtu = (imtu > SDP_LOCAL_MTU)? imtu : SDP_LOCAL_MTU; srv->req = (uint8_t *) calloc(srv->imtu, sizeof(srv->req[0])); if (srv->req == NULL) { log_crit("Could not allocate request buffer"); close(unsock); close(l2sock); return (-1); } /* Allocate memory for descriptor index */ srv->fdidx = (fd_idx_p) calloc(FD_SETSIZE, sizeof(srv->fdidx[0])); if (srv->fdidx == NULL) { log_crit("Could not allocate fd index"); free(srv->req); close(unsock); close(l2sock); return (-1); } /* Register Service Discovery profile (attach it to control socket) */ if (provider_register_sd(unsock) < 0) { log_crit("Could not register Service Discovery profile"); free(srv->fdidx); free(srv->req); close(unsock); close(l2sock); return (-1); } /* * If we got here then everything is fine. Add both control sockets * to the index. */ FD_ZERO(&srv->fdset); srv->maxfd = (unsock > l2sock)? unsock : l2sock; FD_SET(unsock, &srv->fdset); srv->fdidx[unsock].valid = 1; srv->fdidx[unsock].server = 1; srv->fdidx[unsock].control = 1; srv->fdidx[unsock].priv = 0; srv->fdidx[unsock].rsp_cs = 0; srv->fdidx[unsock].rsp_size = 0; srv->fdidx[unsock].rsp_limit = 0; srv->fdidx[unsock].omtu = SDP_LOCAL_MTU; srv->fdidx[unsock].rsp = NULL; FD_SET(l2sock, &srv->fdset); srv->fdidx[l2sock].valid = 1; srv->fdidx[l2sock].server = 1; srv->fdidx[l2sock].control = 0; srv->fdidx[l2sock].priv = 0; srv->fdidx[l2sock].rsp_cs = 0; srv->fdidx[l2sock].rsp_size = 0; srv->fdidx[l2sock].rsp_limit = 0; srv->fdidx[l2sock].omtu = 0; /* unknown */ srv->fdidx[l2sock].rsp = NULL; return (0);}
开发者ID:ppaeps,项目名称:freebsd-head,代码行数:101,
注:本文中的FD_ZERO函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 C++ FEH_FILE函数代码示例 C++ FD_TO_SOCKET函数代码示例 |