PC Review


Reply
Thread Tools Rate Thread

callback to another class (sockets)

 
 
Adie
Guest
Posts: n/a
 
      7th Jan 2005
I'm using async-callbacks for a socket client.

I have a class doing the work with the server - includes the socket,
AsyncResult, AsynCallback etc.. (call it client for arguments sake)

Also a class that is calling the above but acting like a message broker and
wants the result from it's call to the above class. (call it broker)

The question is, can I pass a delegate from the broker to the client that I
can call from the client when data is recieved?

Here's my client code for now:

using System;
using System.Net;
using System.Text;
using System.Net.Sockets;

namespace NNTP_Test
{

public class Pipe
{
private IAsyncResult _asyncResult;
private AsyncCallback _asyncCallback;
private Socket _skt = null;
private int _port = 0;
private string _ip = "";
private Byte[] _buffer;
private IPEndPoint _endpoint;


public Pipe(string ip, int port)
{
_ip = ip;
_port = port;
_skt = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
_buffer = new byte[75];
IPAddress ipAddress = IPAddress.Parse(_ip);
_endpoint = new IPEndPoint(ipAddress, _port);

try
{
_skt.Connect(_endpoint);
WaitForData();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}

public void SendData(string message)
{

}

private void WaitForData()
{
if ( _asyncCallback == null )
{
_asyncCallback = new AsyncCallback (OnDataReceived);
}

// begin waiting for data
_asyncResult = _skt.BeginReceive(_buffer,
0,_buffer.Length,SocketFlags.None,_asyncCallback,null);
}

public void OnDataReceived(IAsyncResult asyn)
{
//end receive...
int iRx = 0 ;
iRx = _skt.EndReceive(asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(_buffer, 0, iRx, chars, 0);
String szData = new System.String(chars);
Console.WriteLine(szData);
WaitForData();
}
}
}

I'd like to be able to pass a delegate from the broker that can be called
from the client with the string szData in the public void
OnDataReceived(IAsyncResult asyn) method.

But I'm not sure how to wire it all up.
 
Reply With Quote
 
 
 
 
Adie
Guest
Posts: n/a
 
      7th Jan 2005
Adie wrote:

> I'm using async-callbacks for a socket client.
>
> I have a class doing the work with the server - includes the socket,
> AsyncResult, AsynCallback etc.. (call it client for arguments sake)
>
> Also a class that is calling the above but acting like a message broker and
> wants the result from it's call to the above class. (call it broker)
>
> The question is, can I pass a delegate from the broker to the client that I
> can call from the client when data is recieved?
>
> Here's my client code for now:
>
> using System;
> using System.Net;
> using System.Text;
> using System.Net.Sockets;
>
> namespace NNTP_Test
> {
>
> public class Pipe
> {
> private IAsyncResult _asyncResult;
> private AsyncCallback _asyncCallback;
> private Socket _skt = null;
> private int _port = 0;
> private string _ip = "";
> private Byte[] _buffer;
> private IPEndPoint _endpoint;
>
>
> public Pipe(string ip, int port)
> {
> _ip = ip;
> _port = port;
> _skt = new Socket(AddressFamily.InterNetwork,
> SocketType.Stream, ProtocolType.Tcp);
> _buffer = new byte[75];
> IPAddress ipAddress = IPAddress.Parse(_ip);
> _endpoint = new IPEndPoint(ipAddress, _port);
>
> try
> {
> _skt.Connect(_endpoint);
> WaitForData();
> }
> catch(Exception e)
> {
> Console.WriteLine(e.Message);
> }
> }
>
> public void SendData(string message)
> {
>
> }
>
> private void WaitForData()
> {
> if ( _asyncCallback == null )
> {
> _asyncCallback = new AsyncCallback (OnDataReceived);
> }
>
> // begin waiting for data
> _asyncResult = _skt.BeginReceive(_buffer,
> 0,_buffer.Length,SocketFlags.None,_asyncCallback,null);
> }
>
> public void OnDataReceived(IAsyncResult asyn)
> {
> //end receive...
> int iRx = 0 ;
> iRx = _skt.EndReceive(asyn);
> char[] chars = new char[iRx + 1];
> System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
> int charLen = d.GetChars(_buffer, 0, iRx, chars, 0);
> String szData = new System.String(chars);
> Console.WriteLine(szData);
> WaitForData();
> }
> }
> }
>
> I'd like to be able to pass a delegate from the broker that can be called
> from the client with the string szData in the public void
> OnDataReceived(IAsyncResult asyn) method.
>
> But I'm not sure how to wire it all up.


Looking at it further, I think i need to create an event in client and pass
it a function from broker, sort of publish/subcribe stuff.
 
Reply With Quote
 
Adie
Guest
Posts: n/a
 
      7th Jan 2005
A... ha! Got it.

this goes in the client

public delegate void DataHandler(string message);
public event DataHandler GetData;

then call the OnGetData event,
from there call GetData(string)

and in the message broker -

static void Main(string[] args)
{
Pipe pipe = new Pipe("blah bha",119);
pipe.GetData += new NNTP_Test.Pipe.DataHandler(DataHandler);
}

private static void DataHandler(string message)
{
Console.WriteLine(message);
}

// groovy!
 
Reply With Quote
 
William Stacey [MVP]
Guest
Posts: n/a
 
      7th Jan 2005

> I'd like to be able to pass a delegate from the broker that can be called
> from the client with the string szData in the public void
> OnDataReceived(IAsyncResult asyn) method.


What you mean by "client" in this case? The network client across the wire
or a client object in the server? Your callback delegate could call another
registered delegate and/or event.

--
William Stacey, MVP
http://mvp.support.microsoft.com


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.Net.Sockets class Alain Dekker Microsoft Dot NET 1 14th Feb 2010 05:15 AM
Callback mainthread in a class Cheryl Microsoft C# .NET 3 20th Jul 2007 09:16 PM
using Sockets class -- not getting responses D. Patrick Microsoft VB .NET 0 25th Jun 2006 06:25 AM
Class for sockets? Terry Olsen Microsoft VB .NET 3 1st Nov 2004 02:43 PM
Raising an exception in a Sockets Async Callback Problem. David Microsoft C# .NET 3 17th Feb 2004 10:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:12 PM.