I'm working on a project and I'm a beginner in VC++.
I'm creating a TCP server that will receive a bitmap and will show it on window.
The motive is to receive the continuous images from a client and show them one by one on server window.
if(!RegisterClassEx(&wClass))
{
int nResult=GetLastError();
MessageBox(NULL,
"Window class creation failed\r\nError code:",
"Window Class Failed",
MB_ICONERROR);
}
On the sender side I suggest to transmit the size of the image first.
On the receiver side use that to allocate the required buffer size on the heap (100 k might even not be enough). Then recv in a loop until the data is received (or an error occurs). You need an offset in your buffer where the next data can be stored.
There's a problem: your gui will freeze until the whole image is received.
for(int i=0; i<32; i++) // You received 16 bytes, why this 32?
{
packetsize = data[i]*256+ data[i]; // I don't understand this calculation
num=recv(Socket, (char*)(data+16), packetsize-16, 0); // you store the received data alsways at the same location. I.e. it's overwritten the next iteration
// you don't care whether data is received or not?
}
Is your socket blocking? If so and if no more data is available it will freeze you program until data is available. Or is async always non blocking?
The problem is that you might not get the expected amount of bytes.
if you write
1 2
num=recv(Socket, (char*)(data+3), packetsize-3, 0);
// I would have understood datasize-3, but why packetsize-3?
num != packetsize-3 might very well be they case. It is not guaranteed that you get size you expect!
async is a bit inconvenient: if you get less than expected you need to wait for the next event to get the rest (this might be as well less then expected...)
Btw you cannot use BITMAPINFO like that. The bmiColors is supposed to contain all colors. So you need to create memory that holds BITMAPINFOHEADER + all RGBQUAD (not just one) then cast it to a pointer to BITMAPINFO for CreateDIBSection