int main(int argc, char **argv) { int n, flags; char buff[NREAD+1]; /* +1 for null at end */ if (argc == 2) listenfd = Tcp_listen(NULL, argv[1], NULL); else if (argc == 3) listenfd = Tcp_listen(argv[1], argv[2], NULL); else err_quit("usage: tcprecv02 [ <host> ] <port#>"); connfd = Xti_accept(listenfd, NULL, NULL); Signal(SIGPOLL, sig_poll); Ioctl(connfd, I_SETSIG, S_RDNORM); for ( ; ; ) { flags = 0; if ( (n = t_rcv(connfd, buff, NREAD, &flags)) < 0) { if (t_errno == TLOOK) { if ( (n = T_look(connfd)) == T_ORDREL) { printf("received T_ORDREL\n"); exit(0); } else err_quit("unexpected event after t_rcv: %d", n); } err_xti("t_rcv error"); } buff[n] = 0; /* null terminate */ printf("read %d bytes: %s, flags = %s\n", n, buff, Xti_flags_str(flags)); } }
/* * Consume some data from the given endpoint of the given wait-based instance. */ void consume_wait_data(instance_t *inst, int fd) { int flag; char buf[50]; /* same arbitrary size as old inetd */ debug_msg("Entering consume_wait_data: inst: %s, fd: %d", inst->fmri, fd); if (inst->config->basic->istlx) { (void) t_rcv(fd, buf, sizeof (buf), &flag); } else { (void) recv(fd, buf, sizeof (buf), 0); } }
int main(int argc, char **argv) { int n, flags; char buff[NREAD+1]; /* +1 for null at end */ struct pollfd pollfd[1]; if (argc == 2) listenfd = Tcp_listen(NULL, argv[1], NULL); else if (argc == 3) listenfd = Tcp_listen(argv[1], argv[2], NULL); else err_quit("usage: tcprecv05 [ <host> ] <port#>"); connfd = Xti_accept(listenfd, NULL, NULL); sleep(5); pollfd[0].fd = connfd; pollfd[0].events = POLLIN; for ( ; ; ) { Poll(pollfd, 1, INFTIM); printf("revents = %x\n", pollfd[0].revents); if (pollfd[0].revents & POLLIN) { flags = 0; if ( (n = t_rcv(connfd, buff, NREAD, &flags)) < 0) { if (t_errno == TLOOK) { if ( (n = T_look(connfd)) == T_ORDREL) { printf("received T_ORDREL\n"); exit(0); } else err_quit("unexpected event after t_rcv: %d", n); } err_xti("t_rcv error"); } buff[n] = 0; /* null terminate */ printf("read %d bytes: %s, flags = %s\n", n, buff, Xti_flags_str(flags)); } } }
/* * Read the contents of the endbuffer, write each record to the * transport endpoint (to the server process), then read a ACKs back from * the transport endpoint and print it on the standard output. */ void process_client_service(int tfd, struct t_call *callptr, char *recbuffer, int length) { static SEQNO_dt current_seqnum, last_seqnum; int n, total_msg; int flags; struct gs_record_hdr hdr; char sendBuffer[4096]; int total_l, l, hl; l = length; hl = sizeof(struct gs_record_hdr); total_l = l + hl; /* make socket connection to server */ if (t_connect(tfd, callptr, (struct t_call *) 0) < 0) { printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); printf("!!! WORNING:Client can't connect to server... !!!\n"); printf("!!! Is the server running? or !!!\n"); printf("!!! Is the server host and port specified for client? !!!\n"); printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); return; } else { printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); printf("!!! Client: Connecting to server... !!!\n"); printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); if(t_rcv(tfd, (char*)&last_seqnum, sizeof(SEQNO_dt), &flags) > 0) { /* now ACKs completed.*/ current_seqnum = htonl(last_seqnum); current_seqnum++; printf( "**ACK: connected to server and rcvd seq nums %d\n", last_seqnum); } else { printf( "**ACK: server DID NOT sent back seq nums %d to %d\n", last_seqnum, current_seqnum); } } total_msg = 0; for(int i=0; i< MaxNumSend; i++) { /* build header with the seqno and size, and data buffer */ current_seqnum = htonl(last_seqnum)+1; hdr.length = htons(l); hdr.sequence_no = htonl(current_seqnum); memcpy(sendBuffer, &hdr, hl); memcpy(sendBuffer+hl, recbuffer, l); /* assume the connection is on, or we can check it if we want using t_look(fd) */ if( n = t_snd( tfd, sendBuffer, total_l, NULL) != total_l) { printf( "t_snd fails: record length is %d, but was sent %d\n", length, n); } else { printf( " ##Client sent data %d bytes for seqno = %d\n", length, hdr.sequence_no); if(t_rcv(tfd, (char*)&last_seqnum, sizeof(SEQNO_dt), &flags) > 0) { /* now ACKs completed.*/ printf( "**ACK: Server processed data SUCCESSFULLY for seqno = %d\n", last_seqnum); total_msg++; } else { continue; } } /* end if for send */ } /* end for-loop*/ printf("************************************************\n"); printf("* Result: # of msg sent and processed = %d\n", total_msg); printf("************************************************\n"); }
/* **************************************************************** * Leitura da Conexão * **************************************************************** */ int tcp_read (int fd, void *buf, int size) { return (t_rcv (fd, buf, size, NULL)); } /* end tcp_read */
int main(int argc, char **argv) { int tfd, n, flags; char recvline[MAXLINE + 1]; struct sockaddr_in servaddr; struct t_call tcall; struct t_discon tdiscon; if (argc != 2) err_quit("usage: daytimecli01 <IPaddress>"); tfd = T_open(XTI_TCP, O_RDWR, NULL); T_bind(tfd, NULL, NULL); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(13); /* daytime server */ Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); tcall.addr.maxlen = sizeof(servaddr); tcall.addr.len = sizeof(servaddr); tcall.addr.buf = &servaddr; tcall.opt.len = 0; /* no options with connect */ tcall.udata.len = 0; /* no user data with connect */ if (t_connect(tfd, &tcall, NULL) < 0) { if (t_errno == TLOOK) { if ( (n = T_look(tfd)) == T_DISCONNECT) { tdiscon.udata.maxlen = 0; T_rcvdis(tfd, &tdiscon); errno = tdiscon.reason; err_sys("t_connect error"); } else err_quit("unexpected event after t_connect: %d", n); } else err_xti("t_connect error"); } /* end daytimecli1 */ /* include daytimecli2 */ for ( ; ; ) { if ( (n = t_rcv(tfd, recvline, MAXLINE, &flags)) < 0) { if (t_errno == TLOOK) { if ( (n = T_look(tfd)) == T_ORDREL) { T_rcvrel(tfd); break; } else if (n == T_DISCONNECT) { tdiscon.udata.maxlen = 0; T_rcvdis(tfd, &tdiscon); errno = tdiscon.reason; /* probably ECONNRESET */ err_sys("server terminated prematurely"); } else err_quit("unexpected event after t_rcv: %d", n); } else err_xti("t_rcv error"); } recvline[n] = 0; /* null terminate */ fputs(recvline, stdout); } exit(0); }
int nlsrequest(int fd, char *svc_code) { int len, err, flags; char buf[256]; char *p; int ret; extern int t_errno; t_errno = 0; /* indicates a 'name' problem */ buf[0] = 0; /* * Validate service code */ if (!svc_code || !strlen(svc_code) || (strlen(svc_code) >= (size_t)SVC_CODE_SZ)) { if (_nlslog) { fprintf(stderr, "nlsrequest: invalid service code format\n"); } return (-1); } /* * send protocol message requesting the service */ len = sprintf(buf, nls_v2_msg, svc_code) + 1; /* inc trailing null */ if (t_snd(fd, buf, len, 0) < len) { if (_nlslog) t_error("t_snd of listener request message failed"); return (-1); } p = _nlsbuf; len = 0; do { if (++len > sizeof (_nlsbuf)) { if (_nlslog) { fprintf(stderr, "nlsrequest: _nlsbuf not large enough\n"); } return (-1); } if (t_rcv(fd, p, sizeof (char), &flags) != sizeof (char)) { if (_nlslog) { t_error("t_rcv of listener response msg " "failed"); } return (-1); } } while (*p++ != '\0'); if ((p = strtok(_nlsbuf, ":")) == NULL) goto parsefail; /* * We ignore the version number here as we do not have any use for it. * Previous versions of the code looked at it by calling atoi() on it, * which did not mutate the actual string and did not use it. */ if ((p = strtok(NULL, ":")) == NULL) goto parsefail; ret = atoi(p); _nlsrmsg = p + strlen(p) + 1; if (ret && _nlslog) fprintf(stderr, "%s\n", _nlsrmsg); /* debug only */ return (ret); parsefail: if (_nlslog) { fprintf(stderr, "nlsrequest: failed parse of response message\n"); } return (-1); }