vttelnet.cpp
上传用户:wsk323
上传日期:2007-01-05
资源大小:403k
文件大小:4k
- /*---------------------------------------------------------------------------
- Copyright: E. Brady Trexler
- Creation: March 1998
- Description: vttelnet -- a simple command line telnet client
- Legal issues: Copyright (C) 1998 by E. Brady Trexler
- This software is provided 'as-is', without any express or
- implied warranty. In no event will the author be held liable
- for any damages arising from the use of this software.
- Permission is granted to anyone to use this software for any
- purpose, excluding commercial applications, and to alter it
- and redistribute it freely, subject to the following
- restrictions:
- 1. The origin of this software must not be misrepresented,
- you must not claim that you wrote the original software.
- If you use this software in a product, an acknowledgment
- in the product documentation would be appreciated but is
- not required.
- 2. Altered source versions must be plainly marked as such, and
- must not be misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source
- distribution.
- Updates:
- ---------------------------------------------------------------------------*/
- #include <condefs.h>
- #pragma hdrstop
- #include <conio.h>
- #include <stdio.h>
- #include <process.h>
- #include "VTClientSession.h"
- HANDLE hSaveStdin, hSaveStdout, hSaveStderr;
- #define BUFSIZE 4096
- //---------------------------------------------------------------------------
- USEFORM("VTClientSession.cpp", VTClient);
- //---------------------------------------------------------------------------
- void GetData(void *arg)
- {
- DWORD dwRead;
- CHAR chBuf[BUFSIZE];
- while (VTClient->TnCnx->State != wsClosed){
- if (!ReadFile(hSaveStdin, chBuf, BUFSIZE, &dwRead, NULL) || dwRead == 0) break;
- chBuf[dwRead] = 0;
- VTClient->TnCnx->Send(chBuf, dwRead);
- }
- }
- #pragma argsused
- int main(int argc, char **argv)
- {
- if (argc < 2){
- printf("Usage: vttelnet <hostname>rn");
- return 0;
- }
- Application->Initialize();
- hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE);
- hSaveStderr = GetStdHandle(STD_ERROR_HANDLE);
- hSaveStdin = GetStdHandle(STD_INPUT_HANDLE);
- VTClient = new TVTClient(Application);
- VTClient->TnCnx->Host = argv[1];
- // printf("<<<Attempting connection to %s>>>", argv[1]);
- try{
- VTClient->TnCnx->Connect();
- }
- __except (EXCEPTION_EXECUTE_HANDLER) {
- printf("rn<<<Connection to %s refused>>>rn
- <<<or host unknown>>>rn", argv[1]);
- }
- VTClient->TimeOutTimer->Enabled = true;
- //start a thread to read input from keyboard and pass it through the socket
- SECURITY_ATTRIBUTES sa =
- {
- sizeof(SECURITY_ATTRIBUTES), // structure size
- 0, // No security descriptor
- TRUE, // Thread handle is inheritable
- };
- DWORD threadId;
- _beginthreadNT(GetData, // Thread starting address
- 8192, // Thread stack size
- 0, // Thread start argument
- &sa, // Thread security
- 0, // Create in running state
- &threadId); // Thread ID.
- while (VTClient->TnCnx->State != wsClosed){
- // these next 3 lines are crazy -- because we don't want it to terminate
- // but all we need is one message to be sent, which Terminate() does,
- // and in this context, handling that message does not close the app.
- Application->Terminate();
- Sleep(50);
- Application->HandleMessage();
- }
- return 0;
- }