/*----------------------------------------------------------------------- * * Program: echoserver * Purpose: wait for a connection from an echoclient and echo data * Usage: echoserver <appnum> * *----------------------------------------------------------------------- */ int main(int argc, char *argv[]) { connection conn; int len; char buff[BUFFSIZE]; if (argc != 2) { (void) fprintf(stderr, "usage: %s <appnum>\n", argv[0]); exit(1); } /* wait for a connection from an echo client */ conn = await_contact((appnum) atoi(argv[1])); if (conn < 0) exit(1); /* iterate, echoing all data received until end of file */ while((len = recv(conn, buff, BUFFSIZE, 0)) > 0) (void) send(conn, buff, len, 0); send_eof(conn); return 0; }
static void forward_query(struct worker_info *w) { struct query_list *q = oldest_query; if (q == NULL) { if (eof_from_pluto) send_eof(w); } else { enum helper_exit_status r = write_pipe(w->qfd, (const unsigned char *) &q->aq); if (r != HES_CONTINUE) exit(r); w->busy = TRUE; oldest_query = q->next; q->next = free_queries; free_queries = q; } }
/*----------------------------------------------------------------------- * * Program: echoserver * Purpose: wait for a connection from an echoclient and echo data * Usage: echoserver <appnum> * *----------------------------------------------------------------------- */ int main(int argc, char *argv[]) { connection conn; int len; char buff[BUFFSIZE]; char fp; while(1){ if (argc != 2) { (void) fprintf(stderr, "usage: %s <appnum>\n", argv[0]); exit(1); } /* wait for a connection from an echo client */ conn = await_contact((appnum) atoi(argv[1])); if (conn < 0) exit(1); /* iterate, echoing all data received until end of file */ FILE *fp; fp=fopen("archivo.txt","w+"); while((len = recv(conn, buff, BUFFSIZE, 0)) > 0){ fputs(buff, fp); (void) send(conn, buff, len, 0); } } fclose(fp); send_eof(conn); return 0; }
void subprocess_stream:: wait() { if (!wait_called) { wait_called = true; send_eof(); std::ostringstream sout; sout << stderr.rdbuf(); try{check_for_matlab_ctrl_c();} catch(...) { kill(child_pid, SIGTERM); } int status; waitpid(child_pid, &status, 0); if (status) throw dlib::error("Child process terminated with an error.\n" + sout.str()); if (sout.str().size() != 0) throw dlib::error("Child process terminated with an error.\n" + sout.str()); } }
/*----------------------------------------------------------------------- * * Program: chatclient * Purpose: contact a chatserver and allow users to chat * Usage: chatclient <compname> <appnum> * *----------------------------------------------------------------------- */ int main(int argc, char *argv[]) { computer comp; connection conn; char buff[BUFFSIZE]; int len; if (argc != 3) { (void) fprintf(stderr, "usage: %s <compname> <appnum>\n", argv[0]); exit(1); } /* convert the compname to binary form comp */ comp = cname_to_comp(argv[1]); if (comp == -1) exit(1); /* make a connection to the chatserver */ conn = make_contact(comp, (appnum) atoi(argv[2])); if (conn < 0) exit(1); (void) printf("Chat Connection Established.\n"); (void) printf(INPUT_PROMPT); (void) fflush(stdout); /* iterate, reading from local user and then from chatserver */ while((len = readln(buff, BUFFSIZE)) > 0) { buff[len - 1] = '\n'; (void) send(conn, buff, len, 0); /* receive and print a line from the chatserver */ if ((len = recvln(conn, buff, BUFFSIZE)) < 1) break; (void) printf(RECEIVED_PROMPT); (void) fflush(stdout); (void) write(STDOUT_FILENO, buff, len); (void) printf(INPUT_PROMPT); (void) fflush(stdout); } /* iteration ends when stdin or the connection indicates EOF */ (void) printf("\nChat Connection Closed.\n"); (void) send_eof(conn); exit(0); }
/** send command and display result */ static int go_cmd(SSL* ssl, int quiet, int argc, char* argv[]) { char pre[10]; const char* space=" "; const char* newline="\n"; int was_error = 0, first_line = 1; int r, i; char buf[1024]; snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION); if(SSL_write(ssl, pre, (int)strlen(pre)) <= 0) ssl_err("could not SSL_write"); for(i=0; i<argc; i++) { if(SSL_write(ssl, space, (int)strlen(space)) <= 0) ssl_err("could not SSL_write"); if(SSL_write(ssl, argv[i], (int)strlen(argv[i])) <= 0) ssl_err("could not SSL_write"); } if(SSL_write(ssl, newline, (int)strlen(newline)) <= 0) ssl_err("could not SSL_write"); if(argc == 1 && strcmp(argv[0], "load_cache") == 0) { send_file(ssl, stdin, buf, sizeof(buf)); } else if(argc == 1 && (strcmp(argv[0], "local_zones") == 0 || strcmp(argv[0], "local_zones_remove") == 0 || strcmp(argv[0], "local_datas") == 0 || strcmp(argv[0], "local_datas_remove") == 0)) { send_file(ssl, stdin, buf, sizeof(buf)); send_eof(ssl); } while(1) { ERR_clear_error(); if((r = SSL_read(ssl, buf, (int)sizeof(buf)-1)) <= 0) { if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) { /* EOF */ break; } ssl_err("could not SSL_read"); } buf[r] = 0; if(first_line && strncmp(buf, "error", 5) == 0) { printf("%s", buf); was_error = 1; } else if (!quiet) printf("%s", buf); first_line = 0; } return was_error; }
static void op_fini (void *impl) { ctx_t *ctx = impl; assert (ctx->magic == MODHANDLE_MAGIC); if (ctx->sock) { send_eof (ctx->sock); zsocket_destroy (ctx->zctx, ctx->sock); } if (ctx->uuid) free (ctx->uuid); if (ctx->uri) free (ctx->uri); ctx->magic = ~MODHANDLE_MAGIC; free (ctx); }
/** send command and display result */ static int go_cmd(SSL* ssl, int fd, int quiet, int argc, char* argv[]) { char pre[10]; const char* space=" "; const char* newline="\n"; int was_error = 0, first_line = 1; int i; char buf[1024]; snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION); remote_write(ssl, fd, pre, strlen(pre)); for(i=0; i<argc; i++) { remote_write(ssl, fd, space, strlen(space)); remote_write(ssl, fd, argv[i], strlen(argv[i])); } remote_write(ssl, fd, newline, strlen(newline)); if(argc == 1 && strcmp(argv[0], "load_cache") == 0) { send_file(ssl, fd, stdin, buf, sizeof(buf)); } else if(argc == 1 && (strcmp(argv[0], "local_zones") == 0 || strcmp(argv[0], "local_zones_remove") == 0 || strcmp(argv[0], "local_datas") == 0 || strcmp(argv[0], "local_datas_remove") == 0)) { send_file(ssl, fd, stdin, buf, sizeof(buf)); send_eof(ssl, fd); } while(1) { if(remote_read(ssl, fd, buf, sizeof(buf)) == 0) { break; /* EOF */ } if(first_line && strncmp(buf, "error", 5) == 0) { printf("%s", buf); was_error = 1; } else if (!quiet) printf("%s", buf); first_line = 0; } return was_error; }
static void query(void) { struct query_list *q = free_queries; enum helper_exit_status r; /* find an unused queue entry */ if (q == NULL) { q = malloc(sizeof(*q)); if (q == NULL) { syslog(LOG_ERR, "malloc(3) failed"); exit(HES_MALLOC); } } else { free_queries = q->next; } r = read_pipe(PLUTO_QFD, (unsigned char *)&q->aq, sizeof(q->aq), sizeof(q->aq)); if (r == HES_OK) { /* EOF: we're done, except for unanswered queries */ struct worker_info *w; eof_from_pluto = TRUE; q->next = free_queries; free_queries = q; /* Send bye-bye to unbusy processes. * Note that if there are queued queries, there won't be * any non-busy workers. */ for (w = wi; w != wi_roof; w++) if (!w->busy) send_eof(w); } else if (r != HES_CONTINUE) { exit(r); } else if (q->aq.qmagic != ADNS_Q_MAGIC) { syslog(LOG_ERR, "error in query from Pluto: bad magic"); exit(HES_BAD_MAGIC); } else { struct worker_info *w; /* got a query */ /* add it to FIFO */ q->next = NULL; if (oldest_query == NULL) oldest_query = q; else newest_query->next = q; newest_query = q; /* See if any worker available */ for (w = wi;; w++) { if (w == wi_roof) { /* no free worker */ if (w == wi + MAX_WORKERS) break; /* no more to be created */ /* make a new one */ if (!spawn_worker()) break; /* cannot create one at this time */ } if (!w->busy) { /* assign first to free worker */ forward_query(w); break; } } } return; }
void Serial::packet_done() { send_eof(); _packet_in_process = false; _pending_write = true; }
/*----------------------------------------------------------------------- * * Program: echoclient * Purpose: contact echoserver, send user input and print server response * Usage: echoclient <compname> [appnum] * Note: Appnum is optional. If not specified the standard echo appnum * (7) is used. * *----------------------------------------------------------------------- */ int main(int argc, char *argv[]) { computer comp; appnum app; connection conn; char buff[BUFFSIZE]; int expect, received, len; if (argc < 2 || argc > 3) { (void) fprintf(stderr, "usage: %s <compname> [appnum]\n", argv[0]); exit(1); } /* convert the arguments to binary format comp and appnum */ comp = cname_to_comp(argv[1]); if (comp == -1) exit(1); if (argc == 3) app = (appnum) atoi(argv[2]); else if ((app = appname_to_appnum("echo")) == -1) exit(1); /* form a connection with the echoserver */ conn = make_contact(comp, app); if (conn < 0) exit(1); (void) printf(INPUT_PROMPT); (void) fflush(stdout); /* iterate: read input from the user, send to the server, */ /* receive reply from the server, and display for user */ while((len = readln(buff, BUFFSIZE)) > 0) { /* send the input to the echoserver */ (void) send(conn, buff, len, 0); (void) printf(RECEIVED_PROMPT); (void) fflush(stdout); /* read and print same no. of bytes from echo server */ expect = len; for (received = 0; received < expect;) { len = recv(conn, buff, (expect - received) < BUFFSIZE ? (expect - received) : BUFFSIZE, 0); if (len < 0) { send_eof(conn); return 1; } (void) write(STDOUT_FILENO, buff, len); received += len; } (void) printf("\n"); (void) printf(INPUT_PROMPT); (void) fflush(stdout); } /* iteration ends when EOF found on stdin */ (void) send_eof(conn); (void) printf("\n"); return 0; }
/*----------------------------------------------------------------------- * * Program: echoserver * Purpose: wait for a connection from an echoclient and echo data * Usage: echoserver <appnum> * *----------------------------------------------------------------------- */ int main(int argc, char *argv[]) { connection conn; int len; char size[4]; char buff[BUFFSIZE]; if (argc != 2) { (void) fprintf(stderr, "usage: %s <SERVER_APPLICATION_NUMBER>\n", argv[0]); exit(1); } /* wait for a connection from an echo client */ conn = await_contact((appnum) atoi(argv[1])); if (conn < 0) exit(1); // Waits for 4 bytes to come through because we know that will // be the size of the paragraph while((len = recv(conn, size, 4, 0)) > 0) { // Print that we've received a paragraph printf("%s", INITIAL_OUTPUT); // These 4 bytes are the size of the paragraph // So we are converting it from the Network Endianness // to our host Endianness uint32_t len = ntohl(*((unsigned long*) size)); // Keep track of the number bytes read from the paragraph int bytesRead = 0; // Keep concatenating on this char array for the whole paragraph char paragraph[BUFFSIZE]; // Keep reading until we have all the bytes while (bytesRead < len) { // Smaller buffer for the characters read on this recv() call char charactersRead[BUFFSIZE]; int lengthOfBytes; // Read some bytes from the sender if (lengthOfBytes = recv(conn, charactersRead, BUFFSIZE, 0) > 0) { // Concatenate our paragraph with the bytes that were read strcat(paragraph, charactersRead); // Increment the counter with the size of the bytes read bytesRead += strlen(charactersRead); // Clear the charactersRead buffer just for security memset(&charactersRead[0], 0, sizeof(charactersRead)); } } // Print the paragraph printf("%s", paragraph); fflush(stdout); // Clear the paragraph buffer memset(¶graph[0], 0, sizeof(paragraph)); } send_eof(conn); fflush(stdout); return 0; }