Here's a simple example of accepting and reading asynchronously from a
socket. It's off the top of my head, so don't expect it to compile/work, but
you should get the general idea...

Piers. 


Socket _sock;

void StartListening (int nPort)
{
        _sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
        _sock.Bind (new IPEndPoint (IPAddress.Any, nPort));
        _sock.Listen (10);
        _sock.BeginAccept (new AsyncCallback (OnAccept), _sock);
}

void OnAccept (IAsyncResult ar)
{
        Socket sockCon = sockListen.EndAccept ((Socket) ar.AsyncState);
        new MyConnection ().StartReadingFrom (new NetworkStream (sockCon));
}

class MyConnection
{
        byte _rgb = new byte [1024];

        public void StartReadingFrom (Stream stream)
        {
                stream.BeginRead (_rgb, 0, _rgb.Length, new AsyncCallback
(OnReadBytes), stream);
        }
        
        void OnReadBytes (IAsyncResult ar)
        {
                Stream stream = (Stream) ar.AsyncState;
                int cbRead = stream.EndRead ();
                if (cbRead == 0)
                        // connection closed
        
                // do stuff with bytes...
        
                StartReadingFrom (stream);
        }
}


-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Shaun ONeil
Sent: Tuesday, March 02, 2004 2:45 PM
To: [EMAIL PROTECTED]
Subject: [Mono-list] Newbie seeks clear async socket example

I'm trying to write a simple server listening on a unix socket (ie,
AF_UNIX).  So far the single example I've found is:

http://primates.ximian.com/~gonzalo/mono/unixsockets/

..but this method is blocking. I need a connection to be an event I respond
to, not something I sit and wait for. Unfortunately I don't know sockets (or
C#) well enough to make the transition.

Everything else I've found that comes close to readable uses TcpListener,
which I can't seem to press into service as it requires IPEndPoint rather
than UnixEndPoint.

Being unix-specific, I'm having great troubles finding this particular
needle on google .. can anyone here throw me a clear example?

Thanks for your time,
  Shaun


_______________________________________________
Mono-list maillist  -  [EMAIL PROTECTED]
http://lists.ximian.com/mailman/listinfo/mono-list


_______________________________________________
Mono-list maillist  -  [EMAIL PROTECTED]
http://lists.ximian.com/mailman/listinfo/mono-list

Reply via email to