📄 telnet.cpp
字号:
#include <windows.h>
#include <winsock.h>
#include "telnet.h"
//
// sock_loop is the thread dedicatd to reading socket input.
// It waits for data from the socket, and then gives it one byte at a time
// to the telnet vm to process.
//
DWORD sock_loop(SOCKET server)
{
char buf[256];
unsigned long read;
char* scan;
while( (read = recv(server,buf,sizeof(buf),0)) && read != SOCKET_ERROR )
{
scan = buf;
while(read--)
vm(server,*scan++);
}
int x = WSAGetLastError();
return 0;
}
DWORD input_loop(SOCKET server)
{
char buf[256];
unsigned long read;
do
{
WaitForSingleObject(stdin,INFINITE);
ReadFile(stdin,buf,255,&read,NULL);
}
while(SOCKET_ERROR != send(server,buf,read,0));
return 0;
}
void telnet(SOCKET server)
{
DWORD dwThreadIdsock;
DWORD dwThreadIdinput;
HANDLE threads[2];
threads[0] = CreateThread(
NULL, /* no security attributes */
0, /* use default stack size */
(LPTHREAD_START_ROUTINE) sock_loop, /* thread function */
(LPVOID)server, /* argument to thread function */
0, /* use default creation flags */
&dwThreadIdsock); /* returns the thread identifier */
//wait for the other thread to complete any setup negotiation...
//Sleep(500); //- this is not the problem - its just bloody stuffing up!
threads[1] = CreateThread(
NULL, /* no security attributes */
0, /* use default stack size */
(LPTHREAD_START_ROUTINE) input_loop, /* thread function */
(LPVOID)server, /* argument to thread function */
0, /* use default creation flags */
&dwThreadIdinput); /* returns the thread identifier */
WaitForMultipleObjects(2,threads,FALSE,INFINITE);
}
//
// connect to the hostname,port
//
void telnet(
char const* pszHostName,
const int nPort)
{
unsigned long ip;
if((*pszHostName <= '9') && (*pszHostName >= '0'))
{
if((ip = inet_addr(pszHostName)) == INADDR_NONE)
err("invalid host ip given");
}
else
{
hostent* ent = gethostbyname(pszHostName);
if(!ent)
err(sockmsg(WSAGetLastError()));
ip = *(unsigned long*)(ent->h_addr);
}
sockaddr_in name;
name.sin_family = AF_INET;
name.sin_port = htons(nPort);
name.sin_addr = *(in_addr*)&ip;
SOCKET server;
if((server = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)
err(sockmsg(WSAGetLastError()));
if(SOCKET_ERROR == connect(server,(sockaddr*)&name,sizeof(sockaddr)))
err(sockmsg(WSAGetLastError()));
telnet(server);
closesocket(server);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -