Mouse driver and AxWin
[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 #define BUFSIZ  128\r
11 \r
12 // === PROTOTYPES ===\r
13  int    main(int argc, char *argv[], char *envp[]);\r
14  int    OpenTCP(const char *AddressString, short PortNumber);\r
15 \r
16 // ==== CODE ====\r
17 int main(int argc, char *argv[], char *envp[])\r
18 {\r
19          int    server_fd, rv;\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
23         \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
28                 return -1;\r
29         }\r
30         \r
31         // Create a ReadLine instance with no history\r
32         readline_info = Readline_Init(0);\r
33         \r
34         while( client_running )\r
35         {\r
36                 fd_set  fds;\r
37                 fd_set  err_fds;\r
38                 char    buffer[BUFSIZ];\r
39                  int    len;\r
40                 \r
41                 // Prepare FD sets\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
45                 \r
46                 // Wait for data (no timeout)\r
47                 rv = select(server_fd+1, &fds, NULL, &err_fds, NULL);\r
48                 if( rv < 0 )    break;\r
49                 \r
50                 // Check for remote data avaliable\r
51                 if( FD_ISSET(server_fd, &fds) )\r
52                 {\r
53                         // Read from server, and write to stdout\r
54                         do\r
55                         {\r
56                                 len = read(server_fd, BUFSIZ, buffer);\r
57                                 write(1, len, buffer);\r
58                         } while( len == BUFSIZ );\r
59                 }\r
60                 \r
61                 // Check for local data input\r
62                 if( FD_ISSET(0, &fds) )\r
63                 {\r
64                         // Read from stdin, and write to server\r
65                         if( bUseReadline )\r
66                         {\r
67                                 char    *line = Readline_NonBlock(readline_info);\r
68                                 if( line )\r
69                                 {\r
70                                         write(server_fd, strlen(line), line);\r
71                                         write(server_fd, 1, "\n");\r
72                                 }\r
73                         }\r
74                         else\r
75                         {\r
76                                 do\r
77                                 {\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
82                         }\r
83                 }\r
84                 \r
85                 // If there was an error, quit\r
86                 if( FD_ISSET(server_fd, &err_fds) )\r
87                 {\r
88                         printf("\nRemote connection lost\n");\r
89                         break ;\r
90                 }\r
91         }\r
92         \r
93         close(server_fd);\r
94         return 0;\r
95 }\r
96 \r
97 /**\r
98  * \brief Initialise a TCP connection to \a AddressString on port \a PortNumber\r
99  */\r
100 int OpenTCP(const char *AddressString, short PortNumber)\r
101 {\r
102          int    fd, addrType;\r
103         uint8_t addrBuffer[16];\r
104         \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
109                 return -1;\r
110         }\r
111 \r
112         // Opens a R/W handle\r
113         fd = Net_OpenSocket(addrType, addrBuffer, "tcpc");\r
114         if( fd == -1 )\r
115         {\r
116                 fprintf(stderr, "Unable to open TCP Client\n");\r
117                 return -1;\r
118         }\r
119         \r
120         // Set remote port and address\r
121         ioctl(fd, 5, &PortNumber);\r
122         ioctl(fd, 6, addrBuffer);\r
123         \r
124         // Connect\r
125         if( ioctl(fd, 7, NULL) == 0 ) {\r
126                 fprintf(stderr, "Unable to start connection\n");\r
127                 return -1;\r
128         }\r
129         \r
130         printf("Connection opened\n");\r
131         \r
132         // Return descriptor\r
133         return fd;\r
134 }\r
135 \r

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