2018.9.24
PracticalSocket.hpp
#include
#include
#pragma comment(lib, "ws2_32.lib")
int ContractHost(const char* sendData,char* recData,int recLen,const char* HostIp, int portnum){
WORD sockVersion = MAKEWORD(2, 2);
WSADATA data;
if(WSAStartup(sockVersion, &data)!=0)return -2;
SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sclient == INVALID_SOCKET)return -1;
sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(portnum);
serAddr.sin_addr.S_un.S_addr = inet_addr(HostIp);
if(connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{
closesocket(sclient);
return -3;
#define HOST_NOT_EXIST (-3)
}
send(sclient, sendData, strlen(sendData), 0);
int ret = recv(sclient, recData, recLen, 0);
recData[ret] = 0x00;
closesocket(sclient);
WSACleanup();
return 0;
#define CONTRACT_SUC (0)
}
class ServerSocket{
WORD sockVersion;
WSADATA wsaData;
SOCKET slisten;
sockaddr_in sin;
SOCKET sClient;
sockaddr_in remoteAddr;
int nAddrlen;
public:
int InitServer(const char* ipname, int portnum){
sockVersion = MAKEWORD(2,2);
if(WSAStartup(sockVersion, &wsaData)!=0)
return -1;
slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(slisten == INVALID_SOCKET)
return -2;
sin.sin_family = AF_INET;
sin.sin_port = htons(portnum);
sin.sin_addr.S_un.S_addr = inet_addr(ipname);
if(bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
return -3;
if(listen(slisten, 5) == SOCKET_ERROR)
return -4;
nAddrlen = sizeof(remoteAddr);
return 0;
}
const char* WaitMessage(char* revData,int recLen){
sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
if(sClient == INVALID_SOCKET) return (const char *)NULL;
int ret = recv(sClient, revData, recLen, 0);
revData[ret] = 0x00;
return inet_ntoa(remoteAddr.sin_addr);
}
void Feedback(const char* sendData){
send(sClient, sendData, strlen(sendData), 0);
closesocket(sClient);
}
void DestructHost(){
closesocket(slisten);
WSACleanup();
}
};
Server.cpp
#include"PracticalSocket.hpp"
#include
ServerSocket ss;
#define DataLen (255)
#include
#include
using namespace std;
char recData[DataLen];
int react(){
stringstream path;
if(recData[0] == '$') {
printf("[Server] send system operation ... ");
int ret = system(&recData[1]);
printf(" [OK]\n");
return ret;
}
path<<recData; string label; path>>label;
if(label == "exit") return -0x7fffffff;
if(label == "cursor"){
printf("[Server] cursor set... ");
int x,y; path>>x>>y; SetCursorPos(x,y);
printf(" [OK]\n");
}else if(label == "keyboard"){
printf("[Server] keyboard ... ");
int ascii = 0;
while((path>>ascii) && ascii) {
keybd_event(ascii,0,0,0);
keybd_event(ascii,0,KEYEVENTF_KEYUP,0);
ascii = 0;
}
printf(" [OK]\n");
}else if(label == "beep"){
int fqc = 0, tim = 0;
while((path>>fqc>>tim) && tim) {
if(fqc > 0) Beep(fqc, tim);
else Sleep(tim);
tim = 0;
}
}else if(label == "hide"){
}else {
}
}
int main(){
char HostIp[18] = {};
cout<<"[Server] input host ip: "; cin>>HostIp;
cout<<"[Server] InitServer... "; ss.InitServer(HostIp, 8888); cout<<" [OK]"<<endl;
while(1){
recData[0] = 0; cout<<"[Server] WaitMessage ... ";
ss.WaitMessage(recData,DataLen); cout<<" [OK]"<<endl;
if(recData[0]) {
int t = react(); if(t == -0x7fffffff) break;
cout<<"[Server] Message: "<<recData<<" "<<endl;
ss.Feedback("[Server] Message Get Successfully!");
}
cout<<endl;
}
cout<<"[Server] DestructHost... "; ss.DestructHost(); cout<<" [OK]"<<endl;
return 0;
}
Client.cpp
#include"PracticalSocket.hpp"
#include
#define cin std::cin
#define cout std::cout
#define cerr std:cerr
#define endl std::endl
#define DataLen (255)
#include
#define clear(A) memset(A,0,sizeof(A))
char recData[DataLen],msg[DataLen];
int main(){
cout<<"[Client] input HostIp: ";
char HostIp[18] = {}; cin>>HostIp;
while(1){
clear(recData);
clear(msg);
cout<<"[Client] InputMessage: ";
gets(msg);
if(msg[0]==0)continue;
cout<<"[Client] ContractHost... ";
int t=ContractHost(msg,recData,DataLen,HostIp, 8888);
cout<<" [OK] return with "<< t <<endl;
cout<<"Message: "<<recData<<endl<<endl;
}
return 0;
}