2 AcessOS Test Network Application
\r
4 #include <acess/sys.h>
\r
8 #include <readline.h>
\r
11 // === PROTOTYPES ===
\r
12 int main(int argc, char *argv[], char *envp[]);
\r
13 int OpenTCP(const char *AddressString, short PortNumber);
\r
16 int main(int argc, char *argv[], char *envp[])
\r
19 int client_running = 1;
\r
20 int bUseReadline = !!argv[3]; // HACK: If third argument is present, use ReadLine
\r
21 tReadline *readline_info;
\r
24 if( argc < 2 || argc > 3 ) {
\r
25 fprintf(stderr, "Usage: telnet <host> [<port>]\n Port defaults to 23\n");
\r
30 port = atoi(argv[2]);
\r
34 // Connect to the remove server
\r
35 server_fd = OpenTCP( argv[1], port );
\r
36 if( server_fd == -1 ) {
\r
37 fprintf(stderr, "Unable to create socket\n");
\r
41 // Create a ReadLine instance with no history
\r
42 readline_info = Readline_Init(0);
\r
44 while( client_running )
\r
48 char buffer[BUFSIZ];
\r
52 FD_ZERO(&fds); FD_ZERO(&err_fds);
\r
53 FD_SET(0, &fds); FD_SET(0, &err_fds);
\r
54 FD_SET(server_fd, &fds); FD_SET(server_fd, &err_fds);
\r
56 // Wait for data (no timeout)
\r
57 rv = select(server_fd+1, &fds, NULL, &err_fds, NULL);
\r
60 // Check for remote data avaliable
\r
61 if( FD_ISSET(server_fd, &fds) )
\r
63 // Read from server, and write to stdout
\r
66 len = _SysRead(server_fd, buffer, BUFSIZ);
\r
67 _SysWrite(1, buffer, len);
\r
68 } while( len == BUFSIZ );
\r
71 // Check for local data input
\r
72 if( FD_ISSET(0, &fds) )
\r
74 // Read from stdin, and write to server
\r
77 char *line = Readline_NonBlock(readline_info);
\r
80 _SysWrite(server_fd, line, strlen(line));
\r
81 _SysWrite(server_fd, "\n", 1);
\r
88 len = _SysRead(0, buffer, BUFSIZ);
\r
89 _SysWrite(server_fd, buffer, len);
\r
90 _SysWrite(1, buffer, len);
\r
91 } while( len == BUFSIZ );
\r
95 // If there was an error, quit
\r
96 if( FD_ISSET(server_fd, &err_fds) )
\r
98 printf("\nRemote connection lost\n");
\r
103 _SysClose(server_fd);
\r
108 * \brief Initialise a TCP connection to \a AddressString on port \a PortNumber
\r
110 int OpenTCP(const char *AddressString, short PortNumber)
\r
113 uint8_t addrBuffer[16];
\r
115 // Parse IP Address
\r
116 addrType = Net_ParseAddress(AddressString, addrBuffer);
\r
117 if( addrType == 0 ) {
\r
118 fprintf(stderr, "Unable to parse '%s' as an IP address\n", AddressString);
\r
122 // Opens a R/W handle
\r
123 fd = Net_OpenSocket(addrType, addrBuffer, "tcpc");
\r
126 fprintf(stderr, "Unable to open TCP Client\n");
\r
130 // Set remote port and address
\r
131 _SysIOCtl(fd, 5, &PortNumber);
\r
132 _SysIOCtl(fd, 6, addrBuffer);
\r
135 if( _SysIOCtl(fd, 7, NULL) == 0 ) {
\r
136 fprintf(stderr, "Unable to start connection\n");
\r
140 printf("Connection opened\n");
\r
142 // Return descriptor
\r