Авторизация на 23-м порту с использованием сокетов

Поделиться:
code: #cpp
#include <iostream>
#include <string>
 
#include <conio.h>
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
 
#pragma comment(lib, "ws2_32")
#define SERVERPORT 23
#define SERVERADDR "192.168.1.1"
#define DEFAULT_BUFLEN 512
 
using namespace std;
 
string LastCommand;
 
bool ConnectToIp(WSADATA &wsaData, SOCKET &Socket, char *ModemIp, u_short ModemPort)
{
        WORD wVersionRequested;
    int err;
 
        //init WinSockets
        wVersionRequested = MAKEWORD(2, 2);
        err = WSAStartup(wVersionRequested, &wsaData);
    if (err != 0)
        {
                cout<<"WSAStartup failed with error: "<<err<<endl;
                return false;
        }
        if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
        {
                WSACleanup();
                cout<<"Could not find a usable version of Winsock.dll"<<endl;
        return false;
        }
 
        //connect to ip
    Socket = socket(AF_INET, SOCK_STREAM, 0);
    if(Socket < 0)
    {
                cout<<"Socket error: "<<WSAGetLastError()<<endl;
        return false;
    }
    sockaddr_in dest_addr;
    dest_addr.sin_family = AF_INET;
    dest_addr.sin_port = htons(ModemPort);
        if(inet_addr(SERVERADDR) != INADDR_NONE) dest_addr.sin_addr.s_addr = inet_addr(SERVERADDR);
        else
        {
                cout<<"Invalid address"<<endl;
        closesocket(Socket);
        WSACleanup();
        return false;
        }
        if (connect(Socket,( sockaddr*)&dest_addr,sizeof(dest_addr)))
    {
                cout<<"Connect error: "<<WSAGetLastError()<<endl;
        return false;
    }
 
        return true;
}
 
bool SendTextToServer(SOCKET Socket, char *Text)
{
        //send text
        int nSize = send(Socket, Text, strlen(Text), 0);
        if(nSize == SOCKET_ERROR)
        {
                cout<<"Connect error: "<<WSAGetLastError()<<endl;
                return false;
        }
        LastCommand = Text;
        //send enter
        nSize = send(Socket, "\r\n", 2, 0);
        if(nSize == SOCKET_ERROR)
        {
                cout<<"Connect error: "<<WSAGetLastError()<<endl;
                return false;
        }
        Sleep(500);
        return true;
}
 
bool RecvTextFromServer(SOCKET Socket)
{
        char Text[DEFAULT_BUFLEN] = {0};
        int nSize = recv(Socket, Text, DEFAULT_BUFLEN, 0);//0
        if(nSize == SOCKET_ERROR)
        {
                cout<<"Connect error: "<<WSAGetLastError()<<endl;
                return false;
        }
        if(nSize == 0)
        {
                cout<<"Recieve null"<<endl;
                return false;
        }
        string PreTextOut = Text;
        int pos = PreTextOut.find(LastCommand, 0);
        if(pos != -1) PreTextOut.erase(pos, LastCommand.size() + 2);
        cout<<PreTextOut.c_str();
 
        return true;
}
 
bool LoginStage(SOCKET Socket)
{
        //Hello
        if(!RecvTextFromServer(Socket)) return 0;
        //Login text
        if(!RecvTextFromServer(Socket)) return 0;
        //send
        char Buffer[DEFAULT_BUFLEN] = {0};
        cin>>Buffer;
        if(!SendTextToServer(Socket, Buffer)) return 0;
        //Password text
        if(!RecvTextFromServer(Socket)) return 0;
        //Password
        cin>>Buffer;
        if(!SendTextToServer(Socket, Buffer)) return 0;
        //Password result
        if(!RecvTextFromServer(Socket)) return 0;
}
 
bool MainLoop(SOCKET Socket)
{
        char Buffer[DEFAULT_BUFLEN] = {0};
        while(1)
        {
                cin>>Buffer;
                if(_stricmp(Buffer, "exit") == 0) break;
                if(!SendTextToServer(Socket, Buffer)) break;
                if(!RecvTextFromServer(Socket)) break;
        }
        return true;
}
 
int main(int argc, char**argv)
{
        WSADATA wsaData;
        SOCKET mySocket;
        if(!ConnectToIp(wsaData, mySocket, SERVERADDR, SERVERPORT)) return 0;
        cout<<"========================================"<<endl;
        cout<<"Modem ["<<SERVERADDR<<":"<<SERVERPORT<<"] Connected"<<endl;
        cout<<"========================================"<<endl;
        if(!LoginStage(mySocket)) return 0;
        //if(!MainLoop(mySocket)) return 0;
        closesocket(mySocket);
        WSACleanup();
 
        cout<<endl<<"Connection refused by user"<<endl;
        cin.get();
        cin.get();
        return 0;
}

автор: Bumerang

Похожие статьи: