Question Revision #2
I am trying to write my own remote desktop program using C++ and WinApi. I have written a multi class project that uses "BmpTools.h" to capture the screen, set the screen, get arrays of pixel data, compress arrays of pixel data, etc. It uses "nSocket.h" for creating a server or client and sending/recieving data through sockets. My problem right now is that my program seems to hang for no reason at all in the middle of sending. I would greatly appreciate if someone could tell me why it hangs for no reason. Here is all of my code.
I have never used a message board like this to find an answer to my questions, I usually find the answers on my own. Please excuse me if I entered too much information and/or made it difficult to answer my question.
My program seems to hang the line after
cout << "Sent Data Info!\nSending Main Data...\n";
in my Client and at the line after
cout << "Recieved Data Info!\n";
in my Server.
Here is the RELEVANT code to my problem.
Client.cpp
#include <windows.h>
#include <iostream>
#include "BmpTools.h"
#include "nSocket.h"
using std::cout;
int main(int argc, char *argv[])
{
HBITMAP bmp;
unsigned char *pixels, *cPixels;
int arraySize, dataSize, lastPacketSize, compBits, compBit1, compBit2, compBit3, compBit4;
unsigned char sendData[6144];
int bufferSize = 6144;
int run = 148;
cout << "Connecting...\n";
nSocket client;
client.nConnect("nyllshanson.selfip.com", 1000);
cout << "Connected!\n";
while(run!=SOCKET_ERROR)
{
bmp = getScreen();
delete [] pixels;
pixels = charGetPixels(bmp);
delete [] cPixels;
cPixels = charCompressPixels(pixels);
compBit1 = cPixels[0];
compBit2 = cPixels[1];
compBit3 = cPixels[2];
compBit4 = cPixels[3];
compBits = (compBit1*1000000) + (compBit2*10000) + (compBit3*100) + compBit4;
arraySize = compBits;
dataSize = compBits / bufferSize;
lastPacketSize = compBits % bufferSize;
cout << "Sending Data Info...\n";
client.nSend((char*)&arraySize, (int)sizeof(arraySize));
client.nSend((char*)&dataSize, (int)sizeof(dataSize));
client.nSend((char*)&lastPacketSize, (int)sizeof(lastPacketSize));
cout << "Sent Data Info!\nSending Main Data...\n";
for(int i=0;i<dataSize;i++)
{
for(int j=0;j<bufferSize;j++)
{
sendData[j] = cPixels[(j+(i*bufferSize))];
}
run = client.nSend((char*)sendData, lastPacketSize);
}
cout << "Sent Main Data!\nSending Last Packet...\n";
if(lastPacketSize!=0)
{
for(int i=0;i<lastPacketSize;i++)
{
sendData[i] = cPixels[(i+(dataSize*bufferSize))];
}
run = client.nSend((char*)sendData, bufferSize);
}
cout << "Sent Last Packet!\n";
}
client.nClose();
client.nCleanup();
}
Server.cpp
#include <windows.h>
#include <iostream>
#include "BmpTools.h"
#include "nSocket.h"
using std::cout;
int main(int argc, char *argv[])
{
HBITMAP bmp;
unsigned char *pixels, *uPixels;
int arraySize, dataSize, lastPacketSize;
unsigned char recvData[6144];
int bufferSize = 6144;
int run;
cout << "Starting Server...\n";
nSocket server;
server.nStartServer(1000);
cout << "Server Started!\n";
while(true)
{
cout << "Waiting For Connection...\n";
server.nAcceptConnection();
cout << "Accepted Connection!\n";
run = 148;
while(run!=SOCKET_ERROR)
{
cout << "Recieving Data Info...\n";
server.nRecv((char*)&arraySize, (int)sizeof(arraySize));
server.nRecv((char*)&dataSize, (int)sizeof(dataSize));
server.nRecv((char*)&lastPacketSize, (int)sizeof(lastPacketSize));
cout << "Recieved Data Info!\n";
delete [] pixels;
pixels = new unsigned char[arraySize];
cout << "Recieving Main Data...\n";
for(int i=0;i<dataSize;i++)
{
server.nRecv((char*)recvData, bufferSize);
for(int j=0;j<bufferSize;j++)
{
pixels[(j+(i*bufferSize))] = recvData[j];
}
}
cout << "Recieved Main Data!\nRecieving Last Packet...\n";
if(lastPacketSize!=0)
{
server.nRecv((char*)recvData, bufferSize);
for(int i=0;i<lastPacketSize;i++)
{
pixels[(i+(dataSize*bufferSize))] = recvData[i];
}
}
cout << "Recieved Last Packet!\n";
delete [] uPixels;
uPixels = charUncompressPixels(pixels);
bmp = charSetPixels(uPixels);
setScreen(bmp);
}
}
}
If you need any more code for reference just let me know and I will put it back up, this time making sure that I dont double encode it in HTML. Thanks in advance for helping.