Hi,
I have implemented a communication model where I have a listening socket, waiting for requests on a specific port.
Once it gets called, it generates a client that creates two sockets: one sending a request to my server and another one that represents the client itself (and displays the data).
Perhaps it is a bit clearer with code:


public void StartRelay()
{
try
{
ClientSocket.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnClientReceive), ClientSocket);
DestinationSocket.BeginReceive(RemoteBuffer, 0, RemoteBuffer.Length, SocketFlags.None, new AsyncCallback(this.OnRemoteReceive), DestinationSocket);
}
catch
{
Dispose();
}
}

protected void OnClientReceive(IAsyncResult ar)
{
try
{
int Ret = ClientSocket.EndReceive(ar);
if (Ret <= 0)
{
Dispose();
return;
}
DestinationSocket.BeginSend(Buffer, 0, Ret, SocketFlags.None, new AsyncCallback(this.OnRemoteSent), DestinationSocket);
}
catch
{
Dispose();
}
}
protected void OnRemoteSent(IAsyncResult ar)
{
try
{
int Ret = DestinationSocket.EndSend(ar);
if (Ret > 0)
{
ClientSocket.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnClientReceive), ClientSocket);
return;
}
}
catch { }
Dispose();
}
protected void OnRemoteReceive(IAsyncResult ar)
{
try
{
int Ret = DestinationSocket.EndReceive(ar);
if (Ret <= 0)
{
Dispose();
return;
}

ClientSocket.BeginSend(RemoteBuffer, 0, Ret, SocketFlags.None, new AsyncCallback(this.OnClientSent), ClientSocket);
}
catch
{
Dispose();
}
}

protected void OnClientSent(IAsyncResult ar)
{
try
{
int Ret = ClientSocket.EndSend(ar);
if (Ret > 0)
{
DestinationSocket.BeginReceive(RemoteBuffer, 0, RemoteBuffer.Length, SocketFlags.None, new AsyncCallback(this.OnRemoteReceive), DestinationSocket);
return;
}
}
catch { }
Dispose();
}


This is working fine. However, I would like to replace the destination socket by an asynchronous webrequest and set the RemoteBuffer on "ClientSocket.BeginSend", by the result of that request (in bytes). The reason why I want to do this, is because I have a server with authentication and the webrequest allows me to use credentials, while the socket communication (in destinationSocket) will fail...
I am a bit confuse on how to use the asyncronous webrequest with the asyncronous client socket...
I imagine I have to do something like this?

request = (HttpWebRequest)WebRequest.Create(myUri);
request.Credentials = CredentialCache.DefaultCredentials;

IAsyncResult r = (IAsyncResult)request.BeginGetResponse(
new AsyncCallback(RespCallback), request);


and "RespCallback" call the "ClientSocket.BeginSend"; however, for retrieving the buffer, I need to get the stream of the request, and if that is also asynchronous I also need a "begin..." and a callback... for the stream...
Plus, do I need to store this buffer in a variable anywhere? (I imagine if the buffer is smaller than the response, the function will get called multiple times!)
Does anybody has implemented or experienced something like this?
Basically, I have a client socket and I want to send to it assyncronously a buffer of data that is get asynchronously from a webrequest...
Any suggestions will be really welcome! Thanks in advance for your time,
cheers,
Jo