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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lewi

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

    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.Sock ets 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 AsyncAcceptCall back(IAsyncResu lt 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 AsyncAcceptCall back(IAsyncResu lt 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.BUF FER_SIZE,0, new
    AsyncCallback(A sync_Send_Recei ve.Read_Callbac k), 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 AsyncAcceptCall back() defined above correctly and what is StateObject
    and what can I do to fix this...

    Any help...




  • Rich Blum

    #2
    Re: Quest: Building C# Client/Server CAsyncSocket(MF C) like class...

    "lewi" <bj7lewis@willa pabay.com> wrote in message news:<vqjauv44i mi965@corp.supe rnews.com>...[color=blue]
    >
    > Now on to C# Window app, I am trying to use the Socket class member of
    > System.Net.Sock ets 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?
    >[/color]
    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(AddressF amily.InterNetw ork,
    SocketType.Stre am,
    ProtocolType.Tc p);
    IPEndPoint iep = new IPEndPoint(IPAd dress.Any, 9050);
    sock.Bind(iep);
    sock.Listen(5);
    sock.BeginAccep t(new AsyncCallback(C allAccept), sock);

    then in the callback method:

    private static void CallAccept(IAsy ncResult iar)
    {
    Socket server = (Socket)iar.Asy ncState;
    Socket client = server.EndAccep t(iar);

    Comment

    • lewi

      #3
      Re: Quest: Building C# Client/Server CAsyncSocket(MF C) like class...

      > property of the IAsyncResult object. The example you copied created an[color=blue]
      > 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.[/color]
      Thanks, I will look into it...
      [color=blue]
      > Rich Blum - Author
      > "C# Network Programming" (Sybex)[/color]
      This would be a great book to checkout when I have to money...

      Thanks again...


      Comment

      Working...