|
How about making the constructor protected.
If you do that it will be accessed only in derived class.
Note: I never tried to do so. Why do you try and let me know too.
Regards
|
|
|
|
|
Let me confess that I am fresher in network programming can any one help me???
I made very simple server using IOCP but my server is unable to receive all the message. Here is code for my server
#include "stdafx.h"
//#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include "winsock2.h"
#include "fstream"
#include "conio.h"
#include "stdio.h"
#pragma comment(lib,"ws2_32.lib")
/*--- MACRO Defination --*/
#define SERVER_PORT htons(8888)
#define DATA_BUFSIZE 4096
#define OP_READ 0
#define OP_WRITE 1
#define OP_ACCEPT 2
/* OverlapPlus Data Used to store Ovelap info and other information need to user*/
typedef struct _OVERLAPPEDPLUS
{
OVERLAPPED Overlapped;
SOCKET sServer,sClient;
int nOperationCode;
WSABUF wbuf;
DWORD dwBytes,dwFlags;
SOCKADDR_STORAGE ClientAddr;
int nClientNumber;
}OVERLAPPEDPLUS, * LP_OVERLAP_PLUS;
void Initialize();
DWORD WINAPI ServerWorkerThread(LPVOID lpParam);
FILE *stream;
void appendLog(TCHAR* strMessage)
{
stream = _wfopen( _T("c:\\iServer.log"), _T("a") );
fputws(_T("\n"),stream);
fputws(strMessage,stream);
fclose( stream );
}
int main()
{
WSADATA wsd;
SYSTEM_INFO SystemInfo;
SOCKADDR_IN InternetAddr;
SOCKET sListen;
HANDLE hIocp;
int nClientInc = 1;
/* Initialize Winsock2 */
Initialize();
/*--- Create an I/O completion port ---*/
hIocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)0, 0);
if(NULL == hIocp)
{
printf("Error in initialization of Completion Port! Press any to exit...");
return -1;
}
/* Determine how many processors are on the system, For thead optimization */
GetSystemInfo(&SystemInfo);
/*--- Create worker threads based on the number of processors available on the system ---*/
for(int nProcessor = 0; nProcessor < (int) SystemInfo.dwNumberOfProcessors; nProcessor++)
{
HANDLE ThreadHandle;
ThreadHandle = CreateThread(NULL, 0,ServerWorkerThread, hIocp,0, NULL);
//CloseHandle(ThreadHandle); // Why We close thread Handle ?????
}
/*--- Create a listening socket --- */
sListen = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0,WSA_FLAG_OVERLAPPED);
/*--- Fill TCP Header Info for bind a socket ---*/
InternetAddr.sin_family = AF_INET;
InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InternetAddr.sin_port = SERVER_PORT;
/*-- Bind the socket with port --*/
bind(sListen, (PSOCKADDR) &InternetAddr, sizeof(InternetAddr));
/*--- Prepare socket for listening mode and Start the Server ---*/
listen(sListen, 5);
printf("Server Started in listen mode ....");
while(TRUE)
{
int ret;
char strBuffer[DATA_BUFSIZE];
ULONG_PTR *PerHandleKey;
OVERLAPPED *Overlap;
OVERLAPPEDPLUS *OverlapPlus,*olpClientOverlappedPlusData;
DWORD dwBytesXfered;
SOCKET sClient;
SOCKADDR_IN saRemote;
int nRemoteLen;
/*-- Accept connections and assign to the completion port--*/
nRemoteLen = sizeof(saRemote);
sClient = WSAAccept(sListen, (SOCKADDR *)&saRemote,&nRemoteLen,0 , 0);
printf("New Client! Socket number %d connected\n", sClient);
/* Allocate memory to olpClientOverlappedPlusData */
olpClientOverlappedPlusData = (LP_OVERLAP_PLUS)GlobalAlloc(GPTR, sizeof(OVERLAPPEDPLUS));
if(NULL == olpClientOverlappedPlusData)
{
printf("Error in initialization Memory! Press any to exit...");
return -1;
}
/*-- Fill OverlapPlusData -- */
olpClientOverlappedPlusData->sClient = sClient;
olpClientOverlappedPlusData->nClientNumber = nClientInc++;
olpClientOverlappedPlusData->nOperationCode = OP_READ;
olpClientOverlappedPlusData->wbuf.buf = strBuffer;
olpClientOverlappedPlusData->wbuf.len = DATA_BUFSIZE;
memcpy(&olpClientOverlappedPlusData->ClientAddr, &saRemote, nRemoteLen);
// Associate the accepted socket with the completion port
if(NULL == CreateIoCompletionPort((HANDLE)sClient, hIocp,(ULONG_PTR)0,0) )
{
printf("Error in initialization of Completion Port for server! Press any to exit...");
return -1;
}
/*-- Make Ready to Recive Data From given Port -- */
ret = WSARecv(sClient,&olpClientOverlappedPlusData->wbuf,1,&olpClientOverlappedPlusData->dwBytes,&olpClientOverlappedPlusData->dwFlags,&olpClientOverlappedPlusData->Overlapped, NULL);
}
return 0;
}
DWORD WINAPI ServerWorkerThread(LPVOID CompletionPortID)
{
/*-- Notify Starting of server -- */
printf("\nHello Server Started, I am in worker thread! \n");
HANDLE hIocp = (HANDLE) CompletionPortID;
ULONG_PTR *PerHandleKey;
OVERLAPPED *Overlap;
OVERLAPPEDPLUS *OverlapPlus,*newolp;
DWORD dwBytesXfered;
int ret;
char *strData ;
while (1)
{
ret = GetQueuedCompletionStatus(hIocp,&dwBytesXfered,(PULONG_PTR)&PerHandleKey,&Overlap,INFINITE);
if (ret == 0){
continue;
}
OverlapPlus = CONTAINING_RECORD(Overlap, OVERLAPPEDPLUS, Overlapped);
/*--- Process Data According to Operation Code ---*/
switch (OverlapPlus->nOperationCode )
{
case OP_READ:
// Process the data read Repost the read if necessary, reusing the same receive buffer as before
memset(&OverlapPlus->Overlapped, 0, sizeof(OVERLAPPED));
ret = WSARecv(OverlapPlus->sClient,&OverlapPlus->wbuf,1,&OverlapPlus->dwBytes,&OverlapPlus->dwFlags,&OverlapPlus->Overlapped, NULL);
appendLog((TCHAR*) &OverlapPlus->wbuf.buf[0]);
ZeroMemory(&(OverlapPlus->Overlapped),sizeof(OVERLAPPED));// Why we reset memory ???
printf("\n\n****Inforamtion*****\n\nClient Socket:%d \n Bytes:%d \n Buff :%s \n Client No: %d\n FLAG :%d ",(int)OverlapPlus->sClient,OverlapPlus->dwBytes,OverlapPlus->wbuf.buf,OverlapPlus->nClientNumber,OverlapPlus->dwFlags);
if (ret == SOCKET_ERROR)
{
ret = WSAGetLastError();
if ( ret != WSA_IO_PENDING)
{
// What should i do in case of Error???
//printf("Error occur at WSARecv() : %d", ret);
//ExitThread(0);
break;
}
}
break;
case OP_WRITE:
// Process the data sent, etc.
break;//…
}
}
}
void Initialize()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
printf("\n couldn't find a useable winsock.dll");
exit(1);
}
}
Madhu S. Kapoor
Madhu S. Kapoor
|
|
|
|
|
Hi! I would be most grateful for assistance. I wish to read data from a file into a CByteArray and return the array. So most simplistically, I have tried variants on:
_____
void MyClass::GetData(CString Pathname, int filesize)
{
// get pathname
// get size of file
BYTE* myarray;
myarray=ReadData(Pathname,filesize);
}
//
/////
//
BYTE* MyClass::ReadData(CString Pathname,int filesize)
{
CByteArray array;
array.SetSize(filesize,-1);
FILE* ifile=fopen(Pathname,"rb");
array.SetSize(size);
BYTE *Array = array.GetData();
ZeroMemory(Array,size);
fread(Array,sizeof(BYTE),size,ifile);
//
return Array;
}
//
_______________
I am evidently doing something totally stupid, so would be indebted to anyone who can gently point out what it is and perhaps provide the requisite correction.
Thanks in advance!
Brian
|
|
|
|
|
Do not return the pointer to the variable:
BYTE *Array = array.GetData();
Instead, return an object of some type, or allocate the memory on the heap; i.e, make it static like:
static CByteArray array;
I would rather use simple stuff:
CString strData;<br />
...<br />
return CString;
I myself have never used CByteArray, infact, I don't recall hearing about it.
this is this.
|
|
|
|
|
khan++ wrote: Instead, return an object of some type, or allocate the memory on the heap; i.e, make it static like:
static CByteArray array;
Never do that. What happens if more than one thread calls the function? Disaster
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
You are violating one of the cardinal rules of C and C++ :- Never return a pointer to a local variable. In ReadData , you are calling GetData on a local variable and then returning a pointer to it. array will be destroyed as soon as the function ends, which means you'll be left with a pointer to nowhere.
This will solve that problem.
CByteArray MyClass::ReadData(CString Pathname,int filesize)
{
CByteArray array;
array.SetSize(filesize,-1);
FILE* ifile=fopen(Pathname,"rb");
array.SetSize(size);
BYTE *Array = array.GetData();
ZeroMemory(Array,size);
fread(Array,sizeof(BYTE),size,ifile);
return array;
}
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Hi Senthil! Thank you for your response. Violating fundmental principles is, unhappily, a main skill of mine. But when I try the routine as you suggest, I get two compiler errors:
error C2558: class 'CByteArray' : no copy constructor available or copy constructor is declared 'explicit'
is invoked by
return array; // return instance of CByteArray,
while
error C2582: 'operator =' function is unavailable in 'CByteArray'
is invoked by
CByteArray byteArray;
byteArray=GetRawData(Pathname);
Thanks for your patience!
Brian
|
|
|
|
|
hello.
i want to import 3D file in VC++.and then to controll the animation or movement of object by giving values to the object through vc++ program.
thanks.
|
|
|
|
|
well... to keep it simple, u need to use DirectX SDK or OpenGL SDK. If I were in ur place, I wud've used OpenGL-GLUT. google for "GLUT" to know more...
"Do first things first, and second things not at all."
— Peter Drucker.
|
|
|
|
|
hello.
i m new in this field so please help how to get start in
open-gl using it.i should use which functions and how.
and one thing more please tell me is it not possible at all to make movie in 3D and then import in VC++
thanks
|
|
|
|
|
U just check this link for problem with code.
http://www.codeguru.com/forum/showthread.php?t=361063
sf
|
|
|
|
|
how can i override a non-virtual MFC function. it may belong to an MFC class or not. i want to tell linker to execute mine instead of the MFC one.
my program contains an .exe file with 22 DLLs, all linked with MFC as a shared library.
|
|
|
|
|
I believe that you can define your own function with the same name and parameters and return type as the original one, when your class is a descendent of the class whose function you want to override, provided that that function is not internally called by the MFC libraries.
Maybe someone can shed some light on my thoughts above.
If you wanted to override a function for lets say CDialog, then you would subclass it, and implement your own function. But I believe that that function should be in CDialog, not in its some parent in the hierarchy, used by its some other parent, to be effective.
this is this.
|
|
|
|
|
thanx khan for answering me.
let's discuss about ur answer!
1. it's better to use "derived" word. instead of "when your class is a descendent of the class whose function you want to override" it's better to say "when ur class is derived from the class whose function..."
2. when a function is declared as virtual, if it's overriden in the derived class, functions of the base class see it and call it instead of the original one (the function in the base class) while they may think they're really calling the original one.
if the function is not declared as virtual in the base class both classes have their own function with the same prototype. that is functions of base class when call it call the function of base class and function of the derived class when call it call the function of the derived class.
this is what the "virtual" keyword provides.
3. what do u mean by subclassing?
4. u may also use "base" instead of "parent"
i remind my question again. i'm looking for a way for functions of base class to call the function i write instead of what they're linked to. when this member function is not declared as virtual this sounds impossible, but i think there must be a way. besides, there may be a function not belonging to any class and i need to override it.
this may be a linker option or so that when encounters an ambiguity decides which one to use. normally it ignores my code and uses one defined in MFC itself.
when i was programming with BC++ or TC++ some years ago, the linker prefered my code over the original one.
and khan! English seems not to be ur mother tongue like me!
thanks again for ur appreciate and also waiting for other answers
|
|
|
|
|
i new in use mfc grid control by CMaunder
it default cell type is CString, i want to process cell value as int, can some one help me to conver CString to int, or any other sugestion?
|
|
|
|
|
msnet wrote: CMaunder
makes a good class name.
-prakash
|
|
|
|
|
This might help u:
<br />
CString str = "123";<br />
int i = atoi( str );<br />
|
|
|
|
|
CString str;
// do some thing, like load data into string
int i = _ttoi(str.GetBuffer(str.GetLength()));
str.ReleaseBuffer();
// do some thing with i
I should be that simple.
Note: _ttoi() is the MICROSOFTS specific version of atoi(), it is used to support both MBCS and UNICODE.
INTP
Every thing is relative...
|
|
|
|
|
I have not seen the article, but,
to get the integer from a char array or CString:
int i;<br />
CString str;<br />
str = "12345";<br />
i = atoi(str);
this is this.
|
|
|
|
|
HI!
I have used the GetModuleFileName function in my MFC SDI application. The syntax is:
DWORD GetModuleFileName(
HMODULE hModule, // handle to module
LPTSTR lpFilename, // path buffer
DWORD nSize // size of buffer
);
Parameters
hModule
[in] Handle to the module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path for the current module.
Now, the problem is with the HModule parameter. But, I don`t know how to set the handle to the module or file. What type of variable to declare. NULL is not serving my purpose.
Thanx
|
|
|
|
|
Your question is not very clear. Anyway,
If you want to get the filepath of the current exe file, then you can set it to NULL:
char str[MAX_PATH];<br />
GetModuleFileName(NULL,str,MAX_PATH);
Will get the current exe's path.
If you want the path for another file, like a DLL, then when you load it using
HMODULE hModule;<br />
hModule = LoadLibrary(...) , its handle is returned. This is the same handle for use with GetModuleFileName(...).
this is this.
|
|
|
|
|
Hi!
I hav an MFC SDI Application with two dialogs. One is for selecting source file and the other is for selecting destination file. BOth, dialogs have one edit box for browsing or editing the path of file.
Actually, I don`t want to get the path of current exe. Instead, I want to get the file path from edit box of another file. I want to get the path , which was entered in the edit box for file name on the SelectSource dialog.
Thanx a lot.
|
|
|
|
|
How are you creating both the dialogs?
If they are in the parent:
CDialog1* m_pDialog1;<br />
CDialog2* m_pDialog2;
Then you can include a pointer to the parent in dialog1:
CParent* m_pParent; //in dialog1.h
And have it initialized by the parent after the dialog creation:
m_pDialog1 = new CDialog1....<br />
.....<br />
m_pDialog1->m_pParent = this;
Then inside CDialog1:
m_pParent->m_pDialog2->m_strFileName; //whatever the variable names are.
So you can access the dialog2 from dialog1.
I hope it is clear.
this is this.
|
|
|
|
|
HI!!
Could u please suggest me some good books on MFC.
I am learning MFC. I had never worked on it earlier. But, now I am working on a project. Currently, I am trying to develop an application for converting "ini" files to "xml" files through MFC.
Thanx a lot.
|
|
|
|
|
Hello,
i'm working a VC++ class that calls stored procedures using ADO,
and i'd really like to know how to set a VARIANT to a datetime value,
without using COleDateTime class.
so instead of:
_ParameterPtr pParameter = m_pCommand->CreateParameter(
_bstr_t(sParamName.c_str()),
adDBTimeStamp,
adParamInput,
0,
_variant_t(COleDateTime(1998, 10, 1, 0, 0, 0))
);
m_pCommand->Parameters->Append( pParameter);
i'd like to go
VARIANT vMyDate;
...then some how set vMyDate to '1997-10-1 0 '
then...
_ParameterPtr pParameter = m_pCommand->CreateParameter(
_bstr_t(sParamName.c_str()),
adDBTimeStamp, adParamInput,
0,
vMyDate
);
m_pCommand->Parameters->Append( pParameter);
why do i want to do this?
I'm working on an existing program, a massive monster of program with about 60 people working on it.
the existing class has functions which add parameters of various datatypes, but theres no function for datatime.
the program uses its own custom class to handle dates and times, from which i can easily extract formatted strings
I tried to use and the COleDateTime class, but the compiler didn't recognise it:
'COleDateTime' : undeclared identifier
I'd assume that means the correct header hasn't been included.
This is a bit of a problem, my workplace is very political and anti-microsoft (even though we use vc++ and mssql!), i'm very junior here, if i add a microsoft header file there's a very good chance someone senior will take it out and back out my changes.
Is there any chance that the header has been included and i need to specify a namespace?
I tried to add what i thought was the correct header:
afxdisp.h
and i get the error:
c:\program files\microsoft visual studio\vc98\mfc\include\afxv_w32.h(14) : fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>
can anyone offer me any suggestions?
thanks
Mat
|
|
|
|
|