|
Hi all,
I am working on TFTP server application.
When A client is requested for a file, My OnReceive() function is called.
In the OnReceive(),I am unable to get the data requested. I used the function like
CListenSocket::OnReceive(int nErrorCode)
{
char buff[100];
memset(buff,0,sizeof(buff));
CAsyncSocket::OnReceive(nErrorCode);
if (nErrorCode)
{
Message.Format("OnReceive nErrorCode: %i", nErrorCode);
AfxMessageBox(Message);
return;
}
int Size = ReceiveFrom(buff, 100,IPAddress,Port);
}
Here I am able to get the IPAddress and Port correctly.
But Unable to get data in the buff variable.But Size value is not zero.
So what could be the problem?
Please help me.
Regards,
Sunil Kumar
|
|
|
|
|
Make sure that the value returned by the function "ReceiveFrom" is a positive number less than 100.
Remember that the buffer ( buff[100]) you used to receive the data is sitting in the stack and will go out of scope when the function returns.
|
|
|
|
|
Yeah I have checked the value itis returned. it is less than 100 only and Im checking the
buffer just after the Receivefrom function.i.e it is not out of the scope.
Regards,
Sunil Kumar
|
|
|
|
|
CListenSocket::OnReceive(int nErrorCode)
{
char buff[100];
memset(buff,0,sizeof(buff));
if (nErrorCode)
{
Message.Format("OnReceive nErrorCode: %i", nErrorCode);
AfxMessageBox(Message);
return;
}
int Size = ReceiveFrom(buff, 100,IPAddress,Port);
CAsyncSocket::OnReceive(nErrorCode);
}
Just try this way.
-@SuDhIrKuMaR@-
|
|
|
|
|
Thanks for the reply,
but still there is no data in buff.
I think atleast it must have the filename I am requesting from tftp client.!
Regards,
Sunil Kumar
|
|
|
|
|
Why you are using ReciveFrom not Revice?
-@SuDhIrKuMaR@-
|
|
|
|
|
I need to know the host address and port.
Even i tried receive() but no use.
Regards,
Sunil Kumar
|
|
|
|
|
First, there's no need to call CAsyncSocket::OnReceive().
It does nothing.
If Size is > 0, then you received a datagram. If the data you expect
isn't in the datagram, you need to look at the sender to see why.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I am getting the compiler error :
"error C2440: '=' : cannot convert from 'LPWSTR' to 'LPCSTR'"
and error line is
cmi.lpVerb = MAKEINTRESOURCE (uiCommand);
And my Code is :
CMINVOKECOMMANDINFO cmi;
ZeroMemory(&cmi, sizeof(CMINVOKECOMMANDINFO));
cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
cmi.fMask = CMIC_MASK_FLAG_NO_UI;
cmi.hwnd = m_hWnd;
cmi.lpParameters = NULL;
cmi.lpDirectory = NULL;
cmi.lpVerb = MAKEINTRESOURCE (uiCommand);
cmi.nShow = SW_SHOWNORMAL;
cmi.dwHotKey = NULL;
cmi.hIcon = NULL;
hr = pCtxMenu->InvokeCommand(&cmi);
How to do it?
|
|
|
|
|
In the Project Settings, Select Configuration Properties->General->Character Set. Set this value to "Use Multi-Byte Character Set".
|
|
|
|
|
You don't have to change your compiler settings.
Just use MAKEINTRESOURCEA instead of MAKEINTRESOURCE.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi friends,
I am using VFW to display the video stream in my application.
I want to connect two or more than two cameras and same no of
display windows. I want to select a camera to display in particular window.
So is there any way to find the camera name so that i choose from a list.
So help me in this issue.
Reagards
Shiva
|
|
|
|
|
|
I am somewhat familiar with a try-catch block and that it automatically destructs objects on exception. However, I'm curious if it automatically frees dynamically allocated memory from the "new" operator?
void failfunc()
{
throw 1;
}
void main()
{
try
{
char *mystr = new char[512];
failfunc();
delete [] mystr;
}
catch (int ierr)
{
if (mystr != NULL) delete [] mystr;
}
}
|
|
|
|
|
You have allocated memory in a heap by using new.
And after that you are executing the next statement which is failing.
As i Know you should delete the memory which is created dynamically.
So in catch statement the deletion part is must.
But if
failfunc();
char *mystr = new char[512];
be allocated in the try block
Statement 1.....
delete [] mystr;
then as function failed it will not try to execute the next statment so there it will skip the creation of memory .so you need not to delete ....
|
|
|
|
|
No, you have to free the memory yourself.
|
|
|
|
|
Objects allocated on the heap (i.e. via new ) are not freed automatically. Objects allocated on the stack (like MyObject obj; ) are freed when they go out of scope (i.e. when the execution leaves the innermost {} that surrounds the declaration) or when an exception is thrown. So in your example, if failfunc() throws an exception other than an integer (which can easily happen when someone changes failfunc() but not main()), you got yourself a memory leak.
However, you can use what's called a smart pointer to get the desired behavior. The smart pointer is is a helper class that is allocated on the stack and behaves like a regular pointer, and when an exception is thrown its destructor gets called. In that destructor, it calls delete[] on its pointee. Just google smart pointers.
Example (not tested):
template< typename T >
class ArrayPtr
{
public:
ArrayPtr( T* pointee )
: _pointee( pointee )
{
}
inline ~ArrayPtr() {
if( _pointee )
delete[] _pointee;
}
operator T*() {
return _pointee
}
private:
T* _pointee;
};
void failfunc()
{
throw 1;
}
void main()
{
try {
ArrayPtr< char > mystr( new char[512] );
failfunc();
} catch( int err ) {
std::cerr << "Error #" << err << std::endl;
}
}
Or if you don't need to print out an error message:
template< typename T > class ArrayPtr { };
void failfunc() { }
void main()
{
ArrayPtr< char > mystr( new char[512] );
failfunc();
}
Note that you cannot use the standard C++ std::auto_ptr for arrays since its destructor only calls delete , not delete[] . This also implies that you need a separate smart pointer type for data allocated with malloc since it must be freed using free , not delete[] .
I suggest that you use smart pointers, otherwise you'll end up having loads of manual try...catch blocks just for ensuring that memory is freed properly, which is hard to maintain and leads to bugs. Also note that the version of main() using the smart pointer is much shorter than your original one since you don't need all the cleanup code.
If sombody now changed failfunc() to throw something else than an integer, your memory would still be freed correctly since the smart pointer would take care of that.
Hope that helps
Peter
|
|
|
|
|
Thanks very much for the descriptive information. The example code is an excellent solution for my problem. I really appreciate you taking the time to answer that thoroughly.
|
|
|
|
|
Glad I could help
|
|
|
|
|
You could use something like Boost[^]'s scoped_array[^] to avoid having to free the memory using a catch .
Steve
|
|
|
|
|
Hai !
Good Morning !
I have created a MSI installer , when i press setup.exe it copies MyApp1.exe , MyApp2.exe in the destination folder , but the problem is that i have MyApp.exe in customs action of setup project, which must be executed once the exe files are copied to the destination folder
thanks
kapardhi
|
|
|
|
|
This is not the right forum to get the question answered.
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
"Costum action" screams for running your app with "/install". Write the install co dein your app.
Greetings from Germany
|
|
|
|
|
We are building our system which has hundred's of workspaces for which debug info is not set manually so now for debugging release binaries we are building through batch file which has following script
Set ReBuild_Required=
if /I "%1"=="clean" GOTO CLEAN
if /I "%1"=="clean_only" GOTO CLEAN_ONLY
Set Config=%1
if /I "%Config%"=="" Set Config=Release
GOTO BUILD
:CLEAN_ONLY
Set ReBuild_Required=/CLEAN
GOTO CLEAN_REMAINING
:CLEAN
Set ReBuild_Required=/REBUILD
:CLEAN_REMAINING
Set Config=%2
if /I "%Config%"=="" Set Config=Release
del /Q ..\..\Include\*.*
del /Q ..\..\..\Common\Include\*.*
if /I %Config%==Release GOTO CLEAN_RELEASE
del /Q ..\..\Bin\Debug\*.*
del /Q ..\..\..\Common\Bin\Debug\*.*
del /Q ..\..\Lib\*Dbg.Lib
del /Q ..\..\..\Common\Lib\*Dbg.Lib
GOTO BUILD
:CLEAN_RELEASE
del /Q ..\..\Bin\Release\*.*
del /Q ..\..\..\Common\Bin\Release\*.*
ren ..\..\Lib\*Dbg.Lib *Dbg.Tlf
del /Q ..\..\Lib\*.Lib
ren ..\..\Lib\*.Tlf *.Lib
ren ..\..\..\Common\Lib\*Dbg.Lib *Dbg.Tlf
del /Q ..\..\..\Common\Lib\*.Lib
ren ..\..\..\Common\Lib\*.Tlf *.Lib
:BUILD
Set Proj_Dir=..\..\..\Common\Source\Database
Set Proj_Name=DBRecall
MSDEV /USEENV %Proj_Dir%\%Proj_Name%\%Proj_Name%.dsp /MAKE "%Proj_Name% - Win32 %Config%" %ReBuild_Required%
if %ERRORLEVEL% GEQ 1 GOTO end
In the above script what changes I need to do inorder to generate debug info??
|
|
|
|
|