telnet.cpp
资源名称:telc10.zip [点击查看]
上传用户:kunlunxyl
上传日期:2007-01-07
资源大小:45k
文件大小:3k
源码类别:
Telnet客户端
开发平台:
Visual C++
- ///////////////////////////////////////////////////////////////////////////////
- //
- // Module: telnet.cpp
- //
- // Contents: telnet main program
- //
- // Product: telnet
- //
- // Revisions: 02.Apr.1995 igor.milavec@uni-lj.si
- // Original code
- //
- ///////////////////////////////////////////////////////////////////////////////
- #include <stdio.h>
- #include "telnet.h"
- #include "tnnet.h"
- #include "tncon.h"
- #include "tnerror.h"
- // Structure to hold parsed command line parameters.
- struct TelParams {
- char* szHost;
- short sPort;
- };
- void telSetConsoleTitle(TelParams& Parameters)
- {
- char szTitle[128];
- wsprintf(szTitle, "Telnet - %s", Parameters.szHost);
- SetConsoleTitle(szTitle);
- }
- SOCKET telConnect(TelParams& Parameters)
- {
- printm(0, FALSE, MSG_TRYING);
- SOCKET Socket = socket(AF_INET, SOCK_STREAM, 0);
- SOCKADDR_IN SockAddr;
- SockAddr.sin_port = htons(Parameters.sPort);
- SockAddr.sin_family = AF_INET;
- SockAddr.sin_addr.s_addr = inet_addr(Parameters.szHost);
- // Were we given host name?
- if (SockAddr.sin_addr.s_addr == INADDR_NONE) {
- // Resolve host name to IP address.
- hostent* pHostEnt = gethostbyname(Parameters.szHost);
- if (!pHostEnt)
- return INVALID_SOCKET;
- SockAddr.sin_addr.s_addr = *(DWORD*)pHostEnt->h_addr;
- }
- if (connect(Socket, (sockaddr*)&SockAddr, sizeof(SockAddr)))
- return INVALID_SOCKET;
- printm(0, FALSE, MSG_CONNECTED, Parameters.szHost);
- return Socket;
- }
- void telDisconnect(SOCKET Socket)
- {
- closesocket(Socket);
- }
- BOOL telProcessParameters(TelParams& Parameters, int ArgC, char* ArgV[])
- {
- if (ArgC < 2)
- return FALSE;
- Parameters.szHost = ArgV[1];
- Parameters.sPort = 23;
- return TRUE;
- }
- int main(int ArgC, char* ArgV[])
- {
- TelParams Parameters;
- if (!telProcessParameters(Parameters, ArgC, ArgV)) {
- printm(0, FALSE, MSG_USAGE);
- return 1;
- }
- telSetConsoleTitle(Parameters);
- WSADATA WsaData;
- if (WSAStartup(MAKEWORD(1, 1), &WsaData)) {
- DWORD dwLastError = GetLastError();
- printm(0, FALSE, MSG_ERROR, "WSAStartup()");
- printm(0, TRUE, dwLastError);
- return 1;
- }
- SOCKET Socket = telConnect(Parameters);
- if (Socket == INVALID_SOCKET) {
- printm(0, FALSE, 0xC0000000 + GetLastError());
- return 2;
- }
- TelThreadParams ThreadParams = {Socket, CreateEvent(0, TRUE, FALSE, 0)};
- DWORD idThread;
- HANDLE hThread = CreateThread(0, 0,
- (LPTHREAD_START_ROUTINE)telProcessNetwork,
- (LPVOID)&ThreadParams, 0, &idThread);
- if (hThread) {
- CloseHandle(hThread);
- }
- telProcessConsole(&ThreadParams);
- telDisconnect(Socket);
- WSACleanup();
- return 0;
- }