CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2009
    Location
    London
    Posts
    13

    Asynchronous socket and webrequest

    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

  2. #2
    Join Date
    Jul 2006
    Posts
    297

    Re: Asynchronous socket and webrequest

    First of all, it will help a little if you can wrap the code in [ code ] tags without the spaces... It will help us be able to read your code a little easier.

    Microsoft has a nice document on using the Async web requests. Let me know if you have any questions on implementing this, i'll be more than willing to help with any questions. This should get you started in the right direction though.

    http://msdn.microsoft.com/en-us/libr...09(VS.71).aspx

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured