I need an urgent help.
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...
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();
};
-------------------------------------
// 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.
Sarah

Need help! Not able to get UdpClient BeginReceive work in VC++/Could this be a complier bug!?
David McMahon
I have tried the code that was shown in the link you gave me.
First of all, I can't find UdpState. Therefore I create my own UdpState.
That's all I added.
To create a test project, I do following
File->New->Project->(Other Languages)->Visaul C++->CLR->Class LibraryUdpClient^ udpClient =
((UdpState)(asyncResult->AsyncState)).udpClient;
The above line of code gave me 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 previouserror(s);
stopping compilation
That's all I have done. If you paste the code that was in the link that you gave me and test it out, you will get those error message and understand what I am talking about.
Thanks,
Sarah
mivecxtr
Brian,
Nobody ever posted a working answer to Sarah97's question about UdpClient->BeginReceive. I am just getting into an application and need this functionality as well. The cited example code does not work and I have been all through the VS2005 help and examples looking for something that does. I started with a C++, CLR, Form application. I am trying to send out a broadcast udp packet and then receive ant responses that come back as part of a device discovery process for a product we make. I would prefer to use C++ and .NET instead of VB or C#.
static void ReceiveCallback(IAsyncResult^ asyncResult){
// The following line causes error C2440: 'type cast': cannot convert 'System:
bject ^' to Class::UdpState'
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;}
vsuneel
Thanks for your respond.
However I have hard time to locate the document of BeginCallback in msdn2.microsoft.com.
Would you mind showing the link
Thank you so much.
Sarah
chris445
It's a couple hops away from the link you gave in your original post. Funny that I swore it had two overloaded forms: one with the state argument, and one without. This shows the two argument version of BeginReceive.
http://msdn2.microsoft.com/en-us/library/system.net.sockets.udpclient.beginreceive.aspx
Morn
The AsyncCallback must be static, per the MSDN example for BeginCallback. But there is an overloaded form of BeginCallback that lets you pass any object of your liking. You pass this as the second argument into BeginCallback and cast it back within the corresponding handler.
Check out the docs, adjust your code, and post back with any other errors you need help with.
Sudhakara
http://msdn2.microsoft.com/en-us/library/system.net.sockets.udpclient.beginreceive.aspx
Sarah
robinjam
Dear sarah97,
Would you please let me know whether you found any answer for your questions If you could solve them kindly please send me the workaround. I have an urgent project to do and I got stuck with same problems.
You can either post in this forum or send to my email box:
khademi20 AATT yahoo DDOTT com
Thanks a lot
Tryst
Thanks for your respond :)
However I have hard time to locate the document of BeginCallback in msdn2.microsoft.com.
Would you mind showing me the link
Thank you very much.
Sarah
Lalicia Bickerton