T
tccode97
To whom it may concern,
I am developing a socket application in VC++ that uses asynchronous
connnection.
After doing search on google, I found the following link
http://msdn2.microsoft.com/en-us/library/system.net.sockets.udpclient.beginreceive.aspx
First of all, thanks for microsoft team for this post.
However I have couple questions concerning the C++ code in the link.
Before posting my questions, first let me show you how I did set up a
VS2005 C++ project to test this code.
To create a test project, I do following
File->New->Project->(Other Languages)->Visaul C++->CLR->Class Library
Fill in solution name and put in the following code.
----------------------------------------------- TestAsynCallback.h
-----------------------------------------
namespace TestAsynCallback {
public ref class UdpState
{
public:
UdpState (void) {};
IPEndPoint^ ipEndPoint;
UdpClient^ udpClient;
};
public ref class Class1
{
// TODO: Add your methods for this class here.
public:
Class1(void)
{
};
bool isMessageReceived;
void ReceiveCallback(IAsyncResult^ asyncResult);
void ReceiveMessages();
};
}
---------------------------------------------------TestAsynCallback.cpp
-------------------------------------
// This is the main DLL file.
#include "stdafx.h"
#include "TestAsynCallback.h"
namespace TestAsynCallback
{
void Class1::ReceiveCallback(IAsyncResult^ asyncResult)
{
UdpClient^ udpClient =
((UdpState)(asyncResult->AsyncState)).udpClient;
IPEndPoint^ ipEndPoint =
((UdpState)(asyncResult->AsyncState)).ipEndPoint;
array<Byte>^ receiveBytes =
udpClient->EndReceive(asyncResult, ipEndPoint);
String^ receiveString =
Encoding::ASCII->GetString(receiveBytes);
Console::WriteLine("Received: {0}", receiveString);
isMessageReceived = true;
}
void Class1::ReceiveMessages()
{
// Receive a message and write it to the console.
IPEndPoint^ ipEndPoint = gcnew IPEndPoint(IPAddress::Any,
listenPort);
UdpClient^ udpClient = gcnew UdpClient(ipEndPoint);
UdpState^ udpState = gcnew UdpState();
udpState->ipEndPoint = ipEndPoint;
udpState->udpClient = udpClient;
Console::WriteLine("listening for messages");
udpClient->BeginReceive(gcnew AsyncCallback(ReceiveCallback),
udpState);
// Do some work while we wait for a message. For this example,
// we'll just sleep
while (!isMessageReceived)
{
Thread::Sleep(100);
}
}
}
-----------------------------------------------------------------------------------------------------------
Question 1)
Notice I took out key word 'static' in the header file. So instead, for
example,
static void ReceiveCallback(IAsyncResult^ asyncResult);
it becomes
void ReceiveCallback(IAsyncResult^ asyncResult);
However when I complie, I get the following error message
Error 1 error C3867: 'TestAsynCallback::Class1::ReceiveCallback':
function call missing argument list; use
'&TestAsynCallback::Class1::ReceiveCallback' to create a pointer to
member
Error 2 error C3350: 'System::TestAsyncCallback' : a delegate
constructor expects 2 argument(s)
which comes from this line of code
udpClient->BeginReceive(gcnew AsyncCallback(ReceiveCallback),
udpState);
Therefore I like to know how do I go around this problem. The reason I
have to change from static to non-static methode is because I have to
raise an event which requires passing 'this' keyword; however static
methode does not allow to do this.
Question 2)
If I leave the code intact and try to complie, I get the following
error.
Error 1 error C2440: 'type cast' : cannot convert from 'System::Object
^' to 'TestAsynCallbackTony::UdpState'
Error 2 error C2101: '&' on constant
Error 3 fatal error C1903: unable to recover from previous error(s);
stopping compilation
which comes from this line of code
UdpClient^ udpClient =
((UdpState)(asyncResult->AsyncState)).udpClient;
And I have no clue how to fix it.
I wonder what have I done wrong or these errors could be resulted from
complier bugs.
Any help from anyone will be greatly appreciated.
TC
I am developing a socket application in VC++ that uses asynchronous
connnection.
After doing search on google, I found the following link
http://msdn2.microsoft.com/en-us/library/system.net.sockets.udpclient.beginreceive.aspx
First of all, thanks for microsoft team for this post.
However I have couple questions concerning the C++ code in the link.
Before posting my questions, first let me show you how I did set up a
VS2005 C++ project to test this code.
To create a test project, I do following
File->New->Project->(Other Languages)->Visaul C++->CLR->Class Library
Fill in solution name and put in the following code.
----------------------------------------------- TestAsynCallback.h
-----------------------------------------
namespace TestAsynCallback {
public ref class UdpState
{
public:
UdpState (void) {};
IPEndPoint^ ipEndPoint;
UdpClient^ udpClient;
};
public ref class Class1
{
// TODO: Add your methods for this class here.
public:
Class1(void)
{
};
bool isMessageReceived;
void ReceiveCallback(IAsyncResult^ asyncResult);
void ReceiveMessages();
};
}
---------------------------------------------------TestAsynCallback.cpp
-------------------------------------
// This is the main DLL file.
#include "stdafx.h"
#include "TestAsynCallback.h"
namespace TestAsynCallback
{
void Class1::ReceiveCallback(IAsyncResult^ asyncResult)
{
UdpClient^ udpClient =
((UdpState)(asyncResult->AsyncState)).udpClient;
IPEndPoint^ ipEndPoint =
((UdpState)(asyncResult->AsyncState)).ipEndPoint;
array<Byte>^ receiveBytes =
udpClient->EndReceive(asyncResult, ipEndPoint);
String^ receiveString =
Encoding::ASCII->GetString(receiveBytes);
Console::WriteLine("Received: {0}", receiveString);
isMessageReceived = true;
}
void Class1::ReceiveMessages()
{
// Receive a message and write it to the console.
IPEndPoint^ ipEndPoint = gcnew IPEndPoint(IPAddress::Any,
listenPort);
UdpClient^ udpClient = gcnew UdpClient(ipEndPoint);
UdpState^ udpState = gcnew UdpState();
udpState->ipEndPoint = ipEndPoint;
udpState->udpClient = udpClient;
Console::WriteLine("listening for messages");
udpClient->BeginReceive(gcnew AsyncCallback(ReceiveCallback),
udpState);
// Do some work while we wait for a message. For this example,
// we'll just sleep
while (!isMessageReceived)
{
Thread::Sleep(100);
}
}
}
-----------------------------------------------------------------------------------------------------------
Question 1)
Notice I took out key word 'static' in the header file. So instead, for
example,
static void ReceiveCallback(IAsyncResult^ asyncResult);
it becomes
void ReceiveCallback(IAsyncResult^ asyncResult);
However when I complie, I get the following error message
Error 1 error C3867: 'TestAsynCallback::Class1::ReceiveCallback':
function call missing argument list; use
'&TestAsynCallback::Class1::ReceiveCallback' to create a pointer to
member
Error 2 error C3350: 'System::TestAsyncCallback' : a delegate
constructor expects 2 argument(s)
which comes from this line of code
udpClient->BeginReceive(gcnew AsyncCallback(ReceiveCallback),
udpState);
Therefore I like to know how do I go around this problem. The reason I
have to change from static to non-static methode is because I have to
raise an event which requires passing 'this' keyword; however static
methode does not allow to do this.
Question 2)
If I leave the code intact and try to complie, I get the following
error.
Error 1 error C2440: 'type cast' : cannot convert from 'System::Object
^' to 'TestAsynCallbackTony::UdpState'
Error 2 error C2101: '&' on constant
Error 3 fatal error C1903: unable to recover from previous error(s);
stopping compilation
which comes from this line of code
UdpClient^ udpClient =
((UdpState)(asyncResult->AsyncState)).udpClient;
And I have no clue how to fix it.
I wonder what have I done wrong or these errors could be resulted from
complier bugs.
Any help from anyone will be greatly appreciated.
TC