Click here to Skip to main content
12,249,279 members (33,368 online)

C / C++ / MFC

 
QuestionMFC and printing Pin
Argonia31-Jan-13 6:43
memberArgonia31-Jan-13 6:43 
AnswerRe: MFC and printing Pin
sujayg1-Feb-13 17:21
membersujayg1-Feb-13 17:21 
QuestionMFC Smart Device App with Visual C++ in Visual Std 2008 Pin
sefasolak31-Jan-13 4:09
membersefasolak31-Jan-13 4:09 
AnswerRe: MFC Smart Device App with Visual C++ in Visual Std 2008 Pin
Richard MacCutchan31-Jan-13 4:45
mvpRichard MacCutchan31-Jan-13 4:45 
Questionunable to register COM dll 64 bit Pin
john563231-Jan-13 3:12
memberjohn563231-Jan-13 3:12 
AnswerRe: unable to register COM dll 64 bit Pin
«_Superman_»1-Feb-13 17:59
member«_Superman_»1-Feb-13 17:59 
QuestionPlay Media File Pin
john563230-Jan-13 20:38
memberjohn563230-Jan-13 20:38 
AnswerRe: Play Media File Pin
Flaviu230-Jan-13 22:47
memberFlaviu230-Jan-13 22:47 
GeneralRe: Play Media File Pin
john563231-Jan-13 2:07
memberjohn563231-Jan-13 2:07 
AnswerRe: Play Media File Pin
jeron131-Jan-13 5:28
memberjeron131-Jan-13 5:28 
GeneralRe: Play Media File Pin
john563231-Jan-13 18:09
memberjohn563231-Jan-13 18:09 
GeneralRe: Play Media File Pin
jeron11-Feb-13 8:27
memberjeron11-Feb-13 8:27 
GeneralRe: Play Media File Pin
merano1-Feb-13 15:11
membermerano1-Feb-13 15:11 
QuestionTCP Server: Render the image on server side Pin
toms from newdelhi30-Jan-13 2:29
membertoms from newdelhi30-Jan-13 2:29 
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
#include
#include
#include

#include
#include
#include
#include


#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(&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!
AnswerRe: TCP Server: Render the image on server side Pin
Richard MacCutchan30-Jan-13 4:01
mvpRichard MacCutchan30-Jan-13 4:01 
GeneralRe: TCP Server: Render the image on server side Pin
toms from newdelhi30-Jan-13 17:44
membertoms from newdelhi30-Jan-13 17:44 
GeneralRe: TCP Server: Render the image on server side Pin
Richard MacCutchan30-Jan-13 22:52
mvpRichard MacCutchan30-Jan-13 22:52 
GeneralRe: TCP Server: Render the image on server side Pin
toms from newdelhi30-Jan-13 23:53
membertoms from newdelhi30-Jan-13 23:53 
GeneralRe: TCP Server: Render the image on server side Pin
Richard MacCutchan30-Jan-13 23:56
mvpRichard MacCutchan30-Jan-13 23:56 
GeneralRe: TCP Server: Render the image on server side Pin
toms from newdelhi30-Jan-13 23:59
membertoms from newdelhi30-Jan-13 23:59 
GeneralRe: TCP Server: Render the image on server side Pin
Richard MacCutchan31-Jan-13 0:08
mvpRichard MacCutchan31-Jan-13 0:08 
AnswerRe: TCP Server: Render the image on server side Pin
Erudite_Eric30-Jan-13 4:13
memberErudite_Eric30-Jan-13 4:13 
GeneralRe: TCP Server: Render the image on server side Pin
Richard MacCutchan30-Jan-13 5:35
mvpRichard MacCutchan30-Jan-13 5:35 
GeneralRe: TCP Server: Render the image on server side Pin
Erudite_Eric30-Jan-13 6:01
memberErudite_Eric30-Jan-13 6:01 
GeneralRe: TCP Server: Render the image on server side Pin
toms from newdelhi30-Jan-13 17:47
membertoms from newdelhi30-Jan-13 17:47 
GeneralRe: TCP Server: Render the image on server side Pin
Erudite_Eric30-Jan-13 23:36
memberErudite_Eric30-Jan-13 23:36 
GeneralRe: TCP Server: Render the image on server side Pin
toms from newdelhi30-Jan-13 23:57
membertoms from newdelhi30-Jan-13 23:57 
GeneralRe: TCP Server: Render the image on server side Pin
Erudite_Eric31-Jan-13 22:35
memberErudite_Eric31-Jan-13 22:35 
AnswerRe: TCP Server: Render the image on server side Pin
Andy Bantly30-Jan-13 13:12
memberAndy Bantly30-Jan-13 13:12 
GeneralRe: TCP Server: Render the image on server side Pin
toms from newdelhi30-Jan-13 17:51
membertoms from newdelhi30-Jan-13 17:51 
GeneralRe: TCP Server: Render the image on server side Pin
Andy Bantly3-Feb-13 4:58
memberAndy Bantly3-Feb-13 4:58 
QuestionCan a modeless dialog box be a main window Pin
ForNow29-Jan-13 12:44
memberForNow29-Jan-13 12:44 
AnswerRe: Can a modeless dialog box be a main window Pin
«_Superman_»29-Jan-13 20:17
member«_Superman_»29-Jan-13 20:17 
GeneralRe: Can a modeless dialog box be a main window Pin
ForNow29-Jan-13 20:34
memberForNow29-Jan-13 20:34 
AnswerRe: Can a modeless dialog box be a main window Pin
Richard MacCutchan29-Jan-13 22:10
mvpRichard MacCutchan29-Jan-13 22:10 
GeneralRe: Can a modeless dialog box be a main window Pin
ForNow30-Jan-13 8:30
memberForNow30-Jan-13 8:30 
GeneralRe: Can a modeless dialog box be a main window Pin
Richard MacCutchan30-Jan-13 8:34
mvpRichard MacCutchan30-Jan-13 8:34 
GeneralRe: Can a modeless dialog box be a main window Pin
ForNow30-Jan-13 9:13
memberForNow30-Jan-13 9:13 
GeneralRe: Can a modeless dialog box be a main window Pin
Richard MacCutchan30-Jan-13 10:07
mvpRichard MacCutchan30-Jan-13 10:07 
AnswerRe: Can a modeless dialog box be a main window Pin
Erudite_Eric30-Jan-13 4:04
memberErudite_Eric30-Jan-13 4:04 
GeneralRe: Can a modeless dialog box be a main window Pin
ForNow30-Jan-13 8:31
memberForNow30-Jan-13 8:31 
QuestionAnyone heard of an Anonymous Statistics / User Experience API or Toolkit Pin
Blake Miller29-Jan-13 7:11
memberBlake Miller29-Jan-13 7:11 
QuestionHow to load a png image in PictureControl? Pin
VCProgrammer29-Jan-13 2:41
groupVCProgrammer29-Jan-13 2:41 
QuestionRe: How to load a png image in PictureControl? Pin
Richard MacCutchan29-Jan-13 4:33
mvpRichard MacCutchan29-Jan-13 4:33 
AnswerRe: How to load a png image in PictureControl? Pin
yu-jian29-Jan-13 16:40
groupyu-jian29-Jan-13 16:40 
GeneralRe: How to load a png image in PictureControl? Pin
VCProgrammer30-Jan-13 23:19
groupVCProgrammer30-Jan-13 23:19 
GeneralRe: How to load a png image in PictureControl? Pin
yu-jian1-Feb-13 5:04
groupyu-jian1-Feb-13 5:04 
GeneralRe: How to load a png image in PictureControl? Pin
VCProgrammer4-Feb-13 2:24
groupVCProgrammer4-Feb-13 2:24 
QuestionIn win7 ,how to modify registry with Non - administrator permission Pin
haha_c28-Jan-13 22:08
memberhaha_c28-Jan-13 22:08 
AnswerRe: In win7 ,how to modify registry with Non - administrator permission Pin
Jochen Arndt28-Jan-13 22:38
memberJochen Arndt28-Jan-13 22:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web01 | 2.8.160426.1 | Last Updated 3 May 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid