Quest: Building C# Client/Server CAsyncSocket(MFC) like class...

L

lewi

I started an unmanaged VC++ dlg app that I want to play arround with socket
for Peer to Peer app on my local LAN and I got it connected up the the app
is the same on both computer and creates a listener socket, one(1 Max user
for now) Server socket connection on acception of listener, and 1 client
socket connection. Then I got it also to Send hardcoded CString and Receive
it in the varible window while debugging...

Now on to C# Window app, I am trying to use the Socket class member of
System.Net.Sockets namespace and I gotten to the Point of working with Async
callback for BeginAccept(). Now first I don't know if the next code sample
to defined right for a callback function. Is it?

public void AsyncAcceptCallback(IAsyncResult ar){...}

Now inside this function I need to construct the connection socket obj and I
was able to do that but when setting up

public void AsyncAcceptCallback(IAsyncResult ar)
{
//Local code lines from MSDN
Socket s = (Socket) ar.AsyncState;
Socket s2 = s.EndAccept(ar);
StateObject so2 = new StateObject();
so2.workSocket = s2;
s2.BeginReceive(so2.buffer, 0, StateObject.BUFFER_SIZE,0, new
AsyncCallback(Async_Send_Receive.Read_Callback), so2);
}

When compiling I get the error "The type or namespace name 'StateObject'
could not be found..." so I looked in the index tabMSDN for StateObject and
it wasn't there so I don't know what namespace this is part of or how it
works but I need it for BeginReceive()...

So is AsyncAcceptCallback() defined above correctly and what is StateObject
and what can I do to fix this...

Any help...
 
R

Rich Blum

lewi said:
Now on to C# Window app, I am trying to use the Socket class member of
System.Net.Sockets namespace and I gotten to the Point of working with Async
callback for BeginAccept(). Now first I don't know if the next code sample
to defined right for a callback function. Is it?
Lewi -

The BeginAccept() method specifies an object that is passed to the
callback method. That object is what is retrieved as the AsyncState
property of the IAsyncResult object. The example you copied created an
object called StateObject, and passed it from the BeginAccept() method
to the EndAccept() method. You most likely followed a code snippet
that did not define the actual object class.

You can just as easily just pass the plain Socket object between
the methods. This would look something like this:

Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
sock.Bind(iep);
sock.Listen(5);
sock.BeginAccept(new AsyncCallback(CallAccept), sock);

then in the callback method:

private static void CallAccept(IAsyncResult iar)
{
Socket server = (Socket)iar.AsyncState;
Socket client = server.EndAccept(iar);
 
L

lewi

property of the IAsyncResult object. The example you copied created an
object called StateObject, and passed it from the BeginAccept() method
to the EndAccept() method. You most likely followed a code snippet
that did not define the actual object class.
Thanks, I will look into it...
Rich Blum - Author
"C# Network Programming" (Sybex)
This would be a great book to checkout when I have to money...

Thanks again...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top