Usermode - Working on AxWin3 SubWin widget (also API change for SysSpawn)
[tpg/acess2.git] / Usermode / Applications / telnet_src / main.c
1 /*\r
2  AcessOS Test Network Application\r
3 */\r
4 #include <acess/sys.h>\r
5 #include <stdio.h>\r
6 #include <net.h>\r
7 #include <readline.h>\r
8 #include <string.h>\r
9 \r
10 // === PROTOTYPES ===\r
11  int    main(int argc, char *argv[], char *envp[]);\r
12  int    OpenTCP(const char *AddressString, short PortNumber);\r
13 \r
14 // ==== CODE ====\r
15 int main(int argc, char *argv[], char *envp[])\r
16 {\r
17          int    server_fd, rv;\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
21          int    port;\r
22         \r
23         if( argc < 2 || argc > 3 ) {\r
24                 fprintf(stderr, "Usage: telnet <host> [<port>]\n Port defaults to 23\n");\r
25                 return 0;\r
26         }\r
27         \r
28         if(argc == 3)\r
29                 port = atoi(argv[2]);\r
30         else\r
31                 port = 23;\r
32         \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
37                 return -1;\r
38         }\r
39         \r
40         // Create a ReadLine instance with no history\r
41         readline_info = Readline_Init(0);\r
42         \r
43         while( client_running )\r
44         {\r
45                 fd_set  fds;\r
46                 fd_set  err_fds;\r
47                 char    buffer[BUFSIZ];\r
48                  int    len;\r
49                 \r
50                 // Prepare FD sets\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
54                 \r
55                 // Wait for data (no timeout)\r
56                 rv = select(server_fd+1, &fds, NULL, &err_fds, NULL);\r
57                 if( rv < 0 )    break;\r
58                 \r
59                 // Check for remote data avaliable\r
60                 if( FD_ISSET(server_fd, &fds) )\r
61                 {\r
62                         // Read from server, and write to stdout\r
63                         do\r
64                         {\r
65                                 len = read(server_fd, buffer, BUFSIZ);\r
66                                 write(1, buffer, len);\r
67                         } while( len == BUFSIZ );\r
68                 }\r
69                 \r
70                 // Check for local data input\r
71                 if( FD_ISSET(0, &fds) )\r
72                 {\r
73                         // Read from stdin, and write to server\r
74                         if( bUseReadline )\r
75                         {\r
76                                 char    *line = Readline_NonBlock(readline_info);\r
77                                 if( line )\r
78                                 {\r
79                                         write(server_fd, line, strlen(line));\r
80                                         write(server_fd, "\n", 1);\r
81                                 }\r
82                         }\r
83                         else\r
84                         {\r
85                                 do\r
86                                 {\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
91                         }\r
92                 }\r
93                 \r
94                 // If there was an error, quit\r
95                 if( FD_ISSET(server_fd, &err_fds) )\r
96                 {\r
97                         printf("\nRemote connection lost\n");\r
98                         break ;\r
99                 }\r
100         }\r
101         \r
102         close(server_fd);\r
103         return 0;\r
104 }\r
105 \r
106 /**\r
107  * \brief Initialise a TCP connection to \a AddressString on port \a PortNumber\r
108  */\r
109 int OpenTCP(const char *AddressString, short PortNumber)\r
110 {\r
111          int    fd, addrType;\r
112         uint8_t addrBuffer[16];\r
113         \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
118                 return -1;\r
119         }\r
120 \r
121         // Opens a R/W handle\r
122         fd = Net_OpenSocket(addrType, addrBuffer, "tcpc");\r
123         if( fd == -1 )\r
124         {\r
125                 fprintf(stderr, "Unable to open TCP Client\n");\r
126                 return -1;\r
127         }\r
128         \r
129         // Set remote port and address\r
130         ioctl(fd, 5, &PortNumber);\r
131         ioctl(fd, 6, addrBuffer);\r
132         \r
133         // Connect\r
134         if( ioctl(fd, 7, NULL) == 0 ) {\r
135                 fprintf(stderr, "Unable to start connection\n");\r
136                 return -1;\r
137         }\r
138         \r
139         printf("Connection opened\n");\r
140         \r
141         // Return descriptor\r
142         return fd;\r
143 }\r
144 \r

UCC git Repository :: git.ucc.asn.au