2 AcessOS Test Network Application
\r
4 #include <acess/sys.h>
\r
7 #include <readline.h>
\r
12 // === PROTOTYPES ===
\r
13 int main(int argc, char *argv[], char *envp[]);
\r
14 int OpenTCP(const char *AddressString, short PortNumber);
\r
17 int main(int argc, char *argv[], char *envp[])
\r
20 int client_running = 1;
\r
21 int bUseReadline = !!argv[3]; // HACK: If third argument is present, use ReadLine
\r
22 tReadline *readline_info;
\r
24 // Connect to the remove server
\r
25 server_fd = OpenTCP( argv[1], atoi(argv[2]) );
\r
26 if( server_fd == -1 ) {
\r
27 fprintf(stderr, "Unable to create socket\n");
\r
31 // Create a ReadLine instance with no history
\r
32 readline_info = Readline_Init(0);
\r
34 while( client_running )
\r
38 char buffer[BUFSIZ];
\r
42 FD_ZERO(&fds); FD_ZERO(&err_fds);
\r
43 FD_SET(0, &fds); FD_SET(0, &err_fds);
\r
44 FD_SET(server_fd, &fds); FD_SET(server_fd, &err_fds);
\r
46 // Wait for data (no timeout)
\r
47 rv = select(server_fd+1, &fds, NULL, &err_fds, NULL);
\r
50 // Check for remote data avaliable
\r
51 if( FD_ISSET(server_fd, &fds) )
\r
53 // Read from server, and write to stdout
\r
56 len = read(server_fd, BUFSIZ, buffer);
\r
57 write(1, len, buffer);
\r
58 } while( len == BUFSIZ );
\r
61 // Check for local data input
\r
62 if( FD_ISSET(0, &fds) )
\r
64 // Read from stdin, and write to server
\r
67 char *line = Readline_NonBlock(readline_info);
\r
70 write(server_fd, strlen(line), line);
\r
71 write(server_fd, 1, "\n");
\r
78 len = read(0, BUFSIZ, buffer);
\r
79 write(server_fd, len, buffer);
\r
80 write(1, len, buffer);
\r
81 } while( len == BUFSIZ );
\r
85 // If there was an error, quit
\r
86 if( FD_ISSET(server_fd, &err_fds) )
\r
88 printf("\nRemote connection lost\n");
\r
98 * \brief Initialise a TCP connection to \a AddressString on port \a PortNumber
\r
100 int OpenTCP(const char *AddressString, short PortNumber)
\r
103 uint8_t addrBuffer[16];
\r
105 // Parse IP Address
\r
106 addrType = Net_ParseAddress(AddressString, addrBuffer);
\r
107 if( addrType == 0 ) {
\r
108 fprintf(stderr, "Unable to parse '%s' as an IP address\n", AddressString);
\r
112 // Opens a R/W handle
\r
113 fd = Net_OpenSocket(addrType, addrBuffer, "tcpc");
\r
116 fprintf(stderr, "Unable to open TCP Client\n");
\r
120 // Set remote port and address
\r
121 ioctl(fd, 5, &PortNumber);
\r
122 ioctl(fd, 6, addrBuffer);
\r
125 if( ioctl(fd, 7, NULL) == 0 ) {
\r
126 fprintf(stderr, "Unable to start connection\n");
\r
130 printf("Connection opened\n");
\r
132 // Return descriptor
\r