|
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.
My code is:
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <io.h>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#pragma comment (lib, "ws2_32.lib")
#define IDC_EDIT_IN 101
//#define IDC_EDIT_OUT 102
//#define IDC_MAIN_BUTTON 103
#define WM_SOCKET 104
#define DEFAULT_BUFLEN 1024
PAINTSTRUCT ps;
int nPort=5552;
HBITMAP hbitmap;
BITMAP cBitmap;
/*HWND hEditIn=NULL;
HWND hEditOut=NULL*/;
SOCKET Socket=NULL;
char szHistory[10000];
sockaddr sockAddrClient;
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{
WNDCLASSEX wClass;
ZeroMemory(&wClass,sizeof(WNDCLASSEX));
wClass.cbClsExtra=NULL;
wClass.cbSize=sizeof(WNDCLASSEX);
wClass.cbWndExtra=NULL;
wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wClass.hIcon=NULL;
wClass.hIconSm=NULL;
wClass.hInstance=hInst;
wClass.lpfnWndProc=(WNDPROC)WinProc;
wClass.lpszClassName="Window Class";
wClass.lpszMenuName=NULL;
wClass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClassEx(&wClass))
{
int nResult=GetLastError();
MessageBox(NULL,
"Window class creation failed\r\nError code:",
"Window Class Failed",
MB_ICONERROR);
}
HWND hWnd=CreateWindowEx(NULL,
"Window Class",
"Capture Server",
WS_OVERLAPPEDWINDOW,
200,
200,
640,
480,
NULL,
NULL,
hInst,
NULL);
if(!hWnd)
{
int nResult=GetLastError();
MessageBox(NULL,
"Window creation failed\r\nError code:",
"Window Creation Failed",
MB_ICONERROR);
}
ShowWindow(hWnd,nShowCmd);
MSG msg;
ZeroMemory(&msg,sizeof(MSG));
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd,&ps);
if(hbitmap != 0)
{
HDC hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hbitmap);
BitBlt(hdc, 10, 10, cBitmap.bmWidth, cBitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);
}
EndPaint (hWnd, &ps);
}
break;
case WM_CREATE:
{
ZeroMemory(szHistory,sizeof(szHistory));
// Create incoming message box
/*hEditIn=CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD|WS_VISIBLE|ES_MULTILINE|
ES_AUTOVSCROLL|ES_AUTOHSCROLL,
20,
20,
200,
20,
hWnd,
(HMENU)IDC_EDIT_IN,
GetModuleHandle(NULL),
NULL);*/
/*if(!hEditIn)
{
MessageBox(hWnd,
"Could not create incoming edit box.",
"Error",
MB_OK|MB_ICONERROR);
}*/
HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
/*SendMessage(hEditIn,
WM_SETFONT,
(WPARAM)hfDefault,
MAKELPARAM(FALSE,0));*/
/*SendMessage(hEditIn,
WM_SETTEXT,
NULL,
(LPARAM)"Waiting for client to connect...");*/
WSADATA WsaDat;
int nResult=WSAStartup(MAKEWORD(2,2),&WsaDat);
if(nResult!=0)
{
MessageBox(hWnd,
"Winsock initialization failed",
"Critical Error",
MB_ICONERROR);
SendMessage(hWnd,WM_DESTROY,NULL,NULL);
break;
}
Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(Socket==INVALID_SOCKET)
{
MessageBox(hWnd,
"Socket creation failed",
"Critical Error",
MB_ICONERROR);
SendMessage(hWnd,WM_DESTROY,NULL,NULL);
break;
}
SOCKADDR_IN SockAddr;
SockAddr.sin_port=htons(nPort);
SockAddr.sin_family=AF_INET;
SockAddr.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(Socket,(LPSOCKADDR)&SockAddr,sizeof(SockAddr))==SOCKET_ERROR)
{
MessageBox(hWnd,"Unable to bind socket","Error",MB_OK);
SendMessage(hWnd,WM_DESTROY,NULL,NULL);
break;
}
nResult=WSAAsyncSelect(Socket,
hWnd,
WM_SOCKET,
(FD_CLOSE|FD_ACCEPT|FD_READ));
if(nResult)
{
MessageBox(hWnd,
"WSAAsyncSelect failed",
"Critical Error",
MB_ICONERROR);
SendMessage(hWnd,WM_DESTROY,NULL,NULL);
break;
}
if(listen(Socket,(1))==SOCKET_ERROR)
{
MessageBox(hWnd,
"Unable to listen!",
"Error",
MB_OK);
SendMessage(hWnd,WM_DESTROY,NULL,NULL);
break;
}
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
shutdown(Socket,SD_BOTH);
closesocket(Socket);
WSACleanup();
return 0;
}
break;
case WM_SOCKET:
{
switch(WSAGETSELECTEVENT(lParam))
{
case FD_READ:
{
/*char szIncoming[1024];*/
char recvbuffer[102400];
int filebuflen = DEFAULT_BUFLEN;
ZeroMemory(recvbuffer, filebuflen);
int bytes_read = recv(Socket, recvbuffer, filebuflen, 0 );
/* ZeroMemory(szIncoming,sizeof(szIncoming));*/
/*int inDataLength=recv(Socket,
(char*)szIncoming,
sizeof(szIncoming)/sizeof(szIncoming[0]),
0);*/
/*strncat(szHistory,szIncoming,inDataLength);
strcat(szHistory,"\r\n");*/
/*SendMessage(hEditIn,
WM_SETTEXT,
sizeof(szIncoming)-1,
reinterpret_cast<lparam>(&szHistory));*/
if(bytes_read==0)
/* MessageBox(hWnd,
"File Read Error",
"Corrupt File",
MB_ICONINFORMATION|MB_OK);*/
break;
if(bytes_read <0)
{
MessageBox(hWnd,
"File Read Error",
"Corrupt File",
MB_ICONINFORMATION|MB_OK);
}
std::ifstream is;
char* pbuffer;
/*is.open("image.bmp", std::ios::binary);*/
/*is.seekg(0, std::ios::end);
*/
/* pbuffer = new char[bytes_read];*/
bytes_read = is.tellg();
/* is.seekg(0, std::ios::beg);*/
pbuffer = new char[bytes_read];
is.read(pbuffer, bytes_read);
is.close();
tagBITMAPFILEHEADER bfh = *(tagBITMAPFILEHEADER*)pbuffer;
tagBITMAPINFOHEADER bih = *(tagBITMAPINFOHEADER*)(pbuffer+sizeof(tagBITMAPFILEHEADER));
RGBQUAD rgb = *(RGBQUAD*)(pbuffer+sizeof(tagBITMAPFILEHEADER)+sizeof(tagBITMAPINFOHEADER));
BITMAPINFO bi;
bi.bmiColors[0] = rgb;
bi.bmiHeader = bih;
char* pPixels = (pbuffer+bfh.bfOffBits);
char* ppvBits;
hbitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (void**) &ppvBits, NULL, 0);
SetDIBits(NULL, hbitmap, 0, bih.biHeight, pPixels, &bi, DIB_RGB_COLORS);
GetObject(hbitmap, sizeof(BITMAP), &cBitmap);
delete[] pbuffer;
/*infile = fopen("test.txt","wb");
fwrite (filebuf, 1, sizeof(filebuf), infile);
fclose (infile);*/
}
break;
case FD_CLOSE:
{
MessageBox(hWnd,
"Client closed connection",
"Connection closed!",
MB_ICONINFORMATION|MB_OK);
/*closesocket(Socket);*/
SendMessage(hWnd,WM_DESTROY,NULL,NULL);
}
break;
case FD_ACCEPT:
{
int size=sizeof(sockaddr);
Socket=accept(wParam,&sockAddrClient,&size);
if (Socket==INVALID_SOCKET)
{
int nret = WSAGetLastError();
WSACleanup();
}
/*SendMessage(hEditIn,
WM_SETTEXT,
NULL,
(LPARAM)"Client connected!");*/
}
break;
}
}
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
and I'm getting an error. when client is sending the image then it is completely sent from client side but I can't see it on server side.
Help me Please.
Thanks!
|
|
|
|