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