C // QB2C TCP routines (c) 2003 by Mike Ramos of US Internet Technologies, Inc. C // C // 04/25/2003 C // C // routines: tcp_connect, tcp_input, tcp_flush, tcp_send, tcp_close C // C // variables: tcp_maxdatasize, tcp_buf_S[maxdatasize], tcp_count, tcp_error, tcp_string$ C // tcp_dest_ip$, tcp_dest_port%, tcp_sockfd C // C // C //************************************************************************************** C // start of config for TCP C //************************************************************************************** CH #include CH #include CH #include CH #include CH #include CH #define MAXDATASIZE 10000 CM char tcp_buf_S[MAXDATASIZE]; //TCP flush buffer and working buffer CM struct sockaddr_in dest_addr; C //************************************************************************************** C //end of config for TCP C //************************************************************************************** C // YOUR CODE GOES HERE tcp_maxdatasize=MAXDATASIZE :rem max buffer above tcp_dest_ip$="207.203.36.254" :rem IP to connect to tcp_dest_port%=110 :rem port to connect to tcp_sockfd%=0 :rem sock field descriptor only one right now. gosub tcp_connect tcp_count=0 gosub tcp_input : if tcp_error then print "Error in tcp_input";tcp_count%;tcp_error print tcp_string$ tcp_string$="user junk" gosub tcp_send gosub tcp_input : if tcp_error then print "Error in tcp_input";tcp_count%;tcp_error print tcp_string$ gosub tcp_input print tcp_string$ tcp_string$="pass seeya1" gosub tcp_send : if tcp_error then print "Error in tcp_input";tcp_count%;tcp_error gosub tcp_input : print tcp_string$ tcp_string$="list" gosub tcp_send gosub tcp_flush print tcp_buf$ gosub tcp_close end C // YOUR CODE STOPS HERE C //************************************************************************************** C // START SUBS FOR TCP C //************************************************************************************** tcp_flush: tcp_error=0 C if ((tcp_count_int=recv(tcp_sockfd_int, tcp_buf_S, tcp_maxdatasize-1, 0)) == -1){ tcp_error=-1 C } return C //************************************************************************************** tcp_send: tcp_string$=tcp_string$+chr$(10) tcp_l%=len(tcp_string$) print tcp_l% C send(tcp_sockfd_int, tcp_string_S, tcp_l_int, 0); return C //************************************************************************************** tcp_connect: C tcp_sockfd_int = socket(AF_INET, SOCK_STREAM, 0); // do some error checking! C dest_addr.sin_family = AF_INET; // host byte order C dest_addr.sin_port = htons(tcp_dest_port_int); // short, network byte order C dest_addr.sin_addr.s_addr = inet_addr(tcp_dest_ip_S); C memset(&(dest_addr.sin_zero), '\0', 8); // zero the rest of the struct C // don't forget to error check the connect()! C connect(tcp_sockfd_int, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)); return C //************************************************************************************** tcp_close: C close(tcp_sockfd_int); return C //************************************************************************************** tcp_input: REM int recvtimeout(int s, char *buf, int len, int timeout) REM { CM fd_set fds; CM int tcp_n; CM struct timeval tv; C // set up the file descriptor set C FD_ZERO(&fds); C FD_SET(tcp_sockfd_int, &fds); C // set up the struct timeval for the timeout C tv.tv_sec = 2; C tv.tv_usec = 0; C // wait until timeout or data received tcp_error=-1 tcp_string$="" tcp_count%=0 C tcp_n = select(tcp_sockfd_int+1, &fds, NULL, NULL, &tv); C if (tcp_n == 0) { return C } // timeout! C if (tcp_n == -1) { return C } // error