Window Programming/Networking.
Hi, Wrote a simple client/server in DOS.
Its ment to be really simple.
Anyways I wrote the client in C++ to display in DOS
this works fine. It connects and recvs the only packet its sent and everything.
Code:
// Standard Includes
#include <iostream>
#include "SocketObject.h"
using namespace std;
struct PacketHandeler
{
int stop;
SocketObject ClientSocketObject;
};
int toMilli(SYSTEMTIME st);
void thrReceiveThread(PacketHandeler *tPacketRecv);
void PacketDisplay(char *buf,int iBytesSent);
void StartMacro(char *buf);
//
// ----> Main Program Function (REQUIRED)
//
int main( )
{
SocketObject ClientSocketObject;
int iPort = 6223; // HTTP Port
char szSendBuffer[256];
int iBytesSent;
int iBytesReceived;
cout << "<-- ATTEMPTING CONNECTION -->" << endl;
// Attempt to connect
if ( ClientSocketObject.Connect( "127.0.0.1", iPort) ) {
cout << "<-- SOCKET CONNECTED -->" << endl;
PacketHandeler PacketRecv;
PacketRecv.ClientSocketObject = ClientSocketObject;
PacketRecv.stop = 1;
// Load the request buffer
/*sprintf(szSendBuffer,"GET / HTTP/1.0\n\n");
// Send the request
iBytesSent = ClientSocketObject.Send(szSendBuffer,256,0);
cout << "<-- SENT " << iBytesSent << " BYTES -->" << endl;*/
// Receive the response
CreateThread(
NULL,
NULL,
(LPTHREAD_START_ROUTINE ) &thrReceiveThread,
&PacketRecv,
NULL,
NULL
);
// Output what was received
system("pause");
while(true)
{
Sleep(5000);
}
}
else {
cout << "**ERROR** Could Not Connect" << endl;
}
system("pause");
WSACleanup();
return EXIT_SUCCESS;
}
void thrReceiveThread(PacketHandeler *tPacketRecv)
{
int iBytesReceived;
char szRecvBuffer[32768];
while(tPacketRecv->stop){
iBytesReceived = tPacketRecv->ClientSocketObject.Recv(szRecvBuffer,32768,0);
PacketDisplay(szRecvBuffer,iBytesReceived); //I took out this function since it was so big
StartMacro(szRecvBuffer);
cout << "<-- RECEIVED " << iBytesReceived << " BYTES -->" << endl;
if(iBytesReceived == -1)
tPacketRecv->stop = 0; //disconnected
}
}
void StartMacro(char *buf)
{
SYSTEMTIME st;
int value;
HWND hwnd = 0;
int sendtime;
char atime[4] = {buf[4],buf[5],buf[6],buf[7]};
//cout << (*((long *) atime)) << endl;
value = (*((long *) atime));
sendtime = value + 2000;
while((value - sendtime) < 0)
{
GetLocalTime(&st);
value = toMilli(st);
}
cout << "Casting Now" << endl;
while(hwnd==0)
hwnd = FindWindow("Notepad",NULL);
SetFocus(hwnd);
SendMessage(hwnd,WM_KEYDOWN,VK_F4,0);
}
int toMilli(SYSTEMTIME st)
{
int time;
time = (((st.wHour*60)*60)*1000)+((st.wMinute*60)*1000)+(st.wSecond*1000)+st.wMilliseconds;
return time;
}
So I decided to put it in some sort of windows GUI.
Everything compiles and works. It connects, but the problem is, it does NOT stay connected. Here is my code
For some reason the same code doesnt stay connect as the other one. Anyone know why?
Code:
#include <windows.h>
#include <stdio.h>
#include <dos.h>
#include "SocketObject.h"
#define IDC_hBU_Join 40001
#define IDC_hEB_Name 40002
#define IDC_hEB_Msg 40003
#define IDC_hBU_Connect 40004
HWND hBU_Join = NULL;
HWND hBU_Connect = NULL;
HWND hEB_Name = NULL;
HWND hEB_Msg = NULL;
LPCTSTR lpszApplicationName = "Notepad";
LPCTSTR lpszTitle = "Notepad";
SocketObject ClientSocketObject;
LRESULT CALLBACK fnMessageProcessor(HWND, UINT, WPARAM, LPARAM);
struct PacketHandeler
{
int stop;
SocketObject ClientSocketObject;
};
void thrReceiveThread(PacketHandeler *tPacketRecv);
void vSendText(char *szName, char *szMsg);
void StartMacro(char *buf);
void Connect();
int toMilli(SYSTEMTIME st);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int nCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = fnMessageProcessor;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = lpszApplicationName;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(RegisterClassEx(&wndclass) == 0) {
exit(1);
}
hWnd = CreateWindow (lpszApplicationName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
75,
200,
375,
200,
NULL,
NULL,
hInstance,
NULL);
//ADD ........ AFTER THIS
hBU_Join = CreateWindow(
"BUTTON",
"Send",
WS_CHILD | WS_VISIBLE| BS_PUSHBUTTON,
150,
17,
100,
28,
hWnd,(HMENU)IDC_hBU_Join,hInstance,NULL);
hBU_Connect = CreateWindow(
"BUTTON",
"Connect",
WS_CHILD | WS_VISIBLE| BS_PUSHBUTTON,
150,
46,
100,
28,
hWnd,(HMENU)IDC_hBU_Connect,hInstance,NULL);
hEB_Name = CreateWindow(
"EDIT",
"Notepad",
WS_CHILD | WS_VISIBLE|ES_LEFT| WS_BORDER,
4,
20,
135,
20,
hWnd,(HMENU)IDC_hEB_Name,hInstance,NULL);
hEB_Msg = CreateWindow(
"EDIT",
"Message to Send",
WS_CHILD | WS_VISIBLE|ES_LEFT| WS_BORDER,
4,
40,
135,
20,
hWnd,(HMENU)IDC_hEB_Msg,hInstance,NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while(TRUE) {
if(PeekMessage(&msg, NULL, 0 ,0, PM_NOREMOVE)){
if(!GetMessage(&msg,NULL,0,0))
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return(msg.wParam);
}
LRESULT CALLBACK fnMessageProcessor(HWND hWnd, UINT iMsg, WPARAM wParam,
LPARAM lParam)
{
char szName[256];
char szMsg[256];
switch(iMsg) {
case WM_COMMAND:
switch(LOWORD(wParam)){
case IDC_hBU_Join:
GetWindowText(hEB_Name,szName,256);
GetWindowText(hEB_Msg,szMsg,256);
vSendText(szName,szMsg);
break;
case IDC_hBU_Connect:
Connect();
break;
}
break;
case WM_CREATE:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hWnd, iMsg, wParam, lParam));
}
return(0);
}
void vSendText(char *szName, char *szMsg)
{
int nVirtKey;
int lParm;
int cCharCode;
int i;
int countcheck = 0;
//char SendText[256] = "";
// HWND hwndEdit = 0;
//HWND hwndEdit2 = 0;
//HWND hwndEdit3 = 0;
HWND hwnd = 0;
//Get main notepad handle
while(hwnd== 0 && countcheck != 100){
hwnd = FindWindow(szName,NULL);
countcheck++;
if (countcheck > 99)
printf("Error 0001: Cant find hwnd \n");
}
///hwndEdit3 = hwnd;
SetFocus(hwnd);
lParm = 0x001c0001;
nVirtKey = VK_RETURN;
cCharCode = 13;
SendMessage(hwnd,WM_KEYDOWN,VK_F4,0);
return;
}
void StartMacro(char *buf)
{
SYSTEMTIME st;
int value;
HWND hwnd = 0;
int sendtime;
char atime[4] = {buf[4],buf[5],buf[6],buf[7]};
//cout << (*((long *) atime)) << endl;
value = (*((long *) atime));
sendtime = value + 2000;
while((value - sendtime) < 0)
{
GetLocalTime(&st);
value = toMilli(st);
}
while(hwnd==0)
hwnd = FindWindow("Notepad",NULL);
SetFocus(hwnd);
SendMessage(hwnd,WM_KEYDOWN,VK_F4,0);
}
void thrReceiveThread(PacketHandeler *tPacketRecv)
{
int iBytesReceived;
char szRecvBuffer[32768];
while(tPacketRecv->stop){
iBytesReceived = tPacketRecv->ClientSocketObject.Recv(szRecvBuffer,32768,1);
StartMacro(szRecvBuffer);
if(iBytesReceived == -1)
tPacketRecv->stop = 0; //disconnected
}
}
void Connect()
{
int iPort = 9668; // HTTP Port
char szSendBuffer[256];
int iBytesSent;
int iBytesReceived;
PacketHandeler PacketRecv;
// Attempt to connect
if ( PacketRecv.ClientSocketObject.Connect( "127.0.0.1", iPort) )
{
PacketRecv.stop = 1;
CreateThread(
NULL,
NULL,
(LPTHREAD_START_ROUTINE ) &thrReceiveThread,
&PacketRecv,
NULL,
NULL
);
}
}
int toMilli(SYSTEMTIME st)
{
int time;
time = (((st.wHour*60)*60)*1000)+((st.wMinute*60)*1000)+(st.wSecond*1000)+st.wMilliseconds;
return time;
}