473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need help! Not able to get UdpClient BeginReceive work in VC++/Could be complier bug!?

Hi,

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/lib...kets.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();

};

}

---------------------------------------------------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.

Sarah

Jun 29 '06 #1
1 2484
I have tested your code.

Not able to get it work.

You may be right. It could be compiler bug.
Jul 12 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gregory Nans | last post by:
hello, i need some help to 'tree-ify' a string... for example i have strings such as : s = """A(here 's , B(A ) silly test) C(to show D(what kind) of stuff i need))""" and i need to...
11
by: blah | last post by:
Need python Pro at newtopython@googlegroups.com , if u wanna help, plz reply to that address. We are python beginners. need a real good Python Programmer for Help. TIA!! -- * Posted with...
2
by: Paul Mendez | last post by:
I really need some help Date Code ConCAT Bal_Fwd NS_Fees Amt_Coll Cur_End_Bal 1/15/2004 KW 11KW2003 $500.00 $250.00 $250.00 2/15/2004 KW 12KW2003 $300.00 $500.00 ...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
1
by: MSDN Account | last post by:
We have web site that used the IIS ResKit tool MSWC.PermissionChecker to check file permissions. The web site has been upgraded and that upgrade included changing the default server side language...
2
by: bryan | last post by:
I have a situation that's pretty delicate that I need some help on. I've been stumped for awhile I just need some advice on the best possible solution. The problem: I have a site I'm making...
8
by: Bruno Alexandre | last post by:
Hi guys, I'm using a session to save an ArrayList, so I do not read Database everytime user reload the page or enter the site (the Data is consistent for all entire session time when the user is...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
8
by: SanjaiGandhi | last post by:
Hi ...i am new to programing....pls help to overcome this program.. The Program is..: if a = 557..using for loop or while or dowhile ..we have to get the answer for 5+5+7..that is what ever...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.