2 AcessOS Test Network Application
\r
4 #include <acess/sys.h>
\r
7 #include <readline.h>
\r
10 // === PROTOTYPES ===
\r
11 int main(int argc, char *argv[], char *envp[]);
\r
12 int OpenTCP(const char *AddressString, short PortNumber);
\r
15 int main(int argc, char *argv[], char *envp[])
\r
18 int client_running = 1;
\r
19 int bUseReadline = !!argv[3]; // HACK: If third argument is present, use ReadLine
\r
20 tReadline *readline_info;
\r
23 if( argc < 2 || argc > 3 ) {
\r
24 fprintf(stderr, "Usage: telnet <host> [<port>]\n Port defaults to 23\n");
\r
29 port = atoi(argv[2]);
\r
33 // Connect to the remove server
\r
34 server_fd = OpenTCP( argv[1], port );
\r
35 if( server_fd == -1 ) {
\r
36 fprintf(stderr, "Unable to create socket\n");
\r
40 // Create a ReadLine instance with no history
\r
41 readline_info = Readline_Init(0);
\r
43 while( client_running )
\r
47 char buffer[BUFSIZ];
\r
51 FD_ZERO(&fds); FD_ZERO(&err_fds);
\r
52 FD_SET(0, &fds); FD_SET(0, &err_fds);
\r
53 FD_SET(server_fd, &fds); FD_SET(server_fd, &err_fds);
\r
55 // Wait for data (no timeout)
\r
56 rv = select(server_fd+1, &fds, NULL, &err_fds, NULL);
\r
59 // Check for remote data avaliable
\r
60 if( FD_ISSET(server_fd, &fds) )
\r
62 // Read from server, and write to stdout
\r
65 len = read(server_fd, buffer, BUFSIZ);
\r
66 write(1, buffer, len);
\r
67 } while( len == BUFSIZ );
\r
70 // Check for local data input
\r
71 if( FD_ISSET(0, &fds) )
\r
73 // Read from stdin, and write to server
\r
76 char *line = Readline_NonBlock(readline_info);
\r
79 write(server_fd, line, strlen(line));
\r
80 write(server_fd, "\n", 1);
\r
87 len = read(0, buffer, BUFSIZ);
\r
88 write(server_fd, buffer, len);
\r
89 write(1, buffer, len);
\r
90 } while( len == BUFSIZ );
\r
94 // If there was an error, quit
\r
95 if( FD_ISSET(server_fd, &err_fds) )
\r
97 printf("\nRemote connection lost\n");
\r
107 * \brief Initialise a TCP connection to \a AddressString on port \a PortNumber
\r
109 int OpenTCP(const char *AddressString, short PortNumber)
\r
112 uint8_t addrBuffer[16];
\r
114 // Parse IP Address
\r
115 addrType = Net_ParseAddress(AddressString, addrBuffer);
\r
116 if( addrType == 0 ) {
\r
117 fprintf(stderr, "Unable to parse '%s' as an IP address\n", AddressString);
\r
121 // Opens a R/W handle
\r
122 fd = Net_OpenSocket(addrType, addrBuffer, "tcpc");
\r
125 fprintf(stderr, "Unable to open TCP Client\n");
\r
129 // Set remote port and address
\r
130 ioctl(fd, 5, &PortNumber);
\r
131 ioctl(fd, 6, addrBuffer);
\r
134 if( ioctl(fd, 7, NULL) == 0 ) {
\r
135 fprintf(stderr, "Unable to start connection\n");
\r
139 printf("Connection opened\n");
\r
141 // Return descriptor
\r