Object
|
+---DTelNetClient
The DTelNetClient class implements a telnet client. The class supports negotiation of options. Also subnegotiation is supported. There is not yet support for interrupt sending.
#include <stdio.h>
#include "ofc/DTelNetClient.h"
@interface MyClient : DTelNetClient // MyClient processes the special server responses
{
}
- (BOOL) processOpenNegotiation :(int) who :(int) option :(int) state;
- (BOOL) processResponseNegotiation :(int) who :(int) option :(BOOL) accepted;
- (BOOL) processRequestSubNegotiation :(int) option;
- (BOOL) processSpecialCommand :(unsigned char) command;
@end
@implementation MyClient
- (BOOL) processOpenNegotiation :(int) who :(int) option :(int) state;
{
printf("Opening negotiation option %s for %s state %d\n",
[DTelNetClient optionToString :option], (who == DTNC_SERVER ? "Server" : "Client"), state);
return (option == 24 ? YES : NO); // Accept option 24, refuse others
}
- (BOOL) processResponseNegotiation :(int) who :(int) option :(BOOL) accepted;
{
printf("Server accepted negotiation option %s for %s state %d\n",
[DTelNetClient optionToString :option], (who == DTNC_SERVER ? "Server" : "Client"), accepted);
return YES;
}
- (BOOL) processRequestSubNegotiation :(int) option
{
printf("Server request the value for the sub negotiation of option %s\n",
[DTelNetClient optionToString :option]);
if (option == 24)
[self respondSubNegotiation :option :"dec-vt100" :9]; // Send value subnegotiation
return YES;
}
- (BOOL) processSpecialCommand :(unsigned char) command;
{
printf("Special command: %s received\n", [DTelNetClient commandToString :command]);
return YES;
}
@end
int main(int argc, char *argv[])
{
MyClient *client = [MyClient new];
DInetSocketAddress *address = [DInetSocketAddress new];
DURL *url = [DURL new];
DData *response = nil;
[address host :"localhost" :DTNC_PORT];
[client open :address]; // Open connection to the server
if ([client isConnected])
{
char buffer[256];
buffer[0] = EOS;
// Open the negotiations
[client requestOpenNegotiation :DTNC_SERVER :DTNC_TM :YES];
do
{
BOOL input = NO;
BOOL empty = NO;
do
{ // Send user input to server
[client sendText :buffer];
if (!input) // Receive responses
response = [client receive];
buffer[0] = EOS;
if (response != nil)
{
long i;
long length = [response length];
printf("Response:"); // Print response
for (i = 0; i < length; i++)
printf("%c", [response get :i]);
if (length > 0)
input = ([response get :(length-1)] != '\n');
empty = (length == 0);
[response free]; response = nil;
}
}
while (([client pendingRequests]) || (empty) || (!input));
fflush(stdout);
// Ask input from user
fgets(buffer, sizeof(buffer), stdin);
} // Keep asking till 'exit'
while (strncmp(buffer, "exit", 4) != 0);
[client close];
}
else
printf("Could not connect to telnet server.\n");
[client free]; // Cleanup
[url free];
[address free];
}