none
Async Sockets and State RRS feed

  • Question

  • I'm working on a project to consume a network stream from a server.  I am going to be passing messages and then receiving results.  I am wondering if I need to track state from BeginSend and EndSend.  Also, what do I gain from tracking state?  Does the socket split off from the main socket if I add it to my state object?  Below is an example send function of my client, the class that calls this function will be tracking that all bytes have been sent and recursively call the method below until all bytes have been sent.

            public async Task<int> SendAsync(string Data, int Offset, int Length)
            {
                byte[] dataBuffer = Encoding.ASCII.GetBytes(Data);
    
                int bytesSent = await Task.Factory.FromAsync(
                    (c, s) => _client.BeginSend(dataBuffer, Offset, dataBuffer.Length, SocketFlags.None, c, s),
                    (ar) => _client.EndSend(ar), 
                    TaskCreationOptions.None);
    
                return bytesSent;
            }

    Thursday, October 22, 2015 2:51 PM

Answers

  • Hi Mike Dovell,

    >>actual state object

    Actual state object let you pass a object to begin method. If you follow the source code you can see that the state also can be accessed from the Task instance using the Task.AsyncState.

    var task = Task.Factory.FromAsync<TResult>(...);
    //You can use this state by task.AsyncState object state = task.AsyncState;
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 27, 2015 6:27 AM
    Moderator

All replies

  • There was an error in the function, below is the revised code.

            public async Task<int> SendAsync(string Data)
            {
                byte[] dataBuffer = Encoding.ASCII.GetBytes(Data);
    
                int bytesSent = await Task.Factory.FromAsync(
                    (c, s) => _client.BeginSend(dataBuffer, 0, dataBuffer.Length, SocketFlags.None, c, s),
                    (ar) => _client.EndSend(ar), 
                    TaskCreationOptions.None);
    
                return bytesSent;
            }

    Thursday, October 22, 2015 2:58 PM
  • There was an error in the function, below is the revised code.

            public async Task<int> SendAsync(string Data)
            {
                byte[] dataBuffer = Encoding.ASCII.GetBytes(Data);
    
                int bytesSent = await Task.Factory.FromAsync(
                    (c, s) => _client.BeginSend(dataBuffer, 0, dataBuffer.Length, SocketFlags.None, c, s),
                    (ar) => _client.EndSend(ar), 
                    TaskCreationOptions.None);
    
                return bytesSent;
            }

    Does it works? Thank you for sharing your workaround.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 23, 2015 2:58 AM
    Moderator
  • The function works fine, I just want to understand what happens when you attach an actual state object.  I'm wondering specifically what the benefit of using an actual state object would be.
    Friday, October 23, 2015 4:58 PM
  • Hi Mike Dovell,

    >>actual state object

    Actual state object let you pass a object to begin method. If you follow the source code you can see that the state also can be accessed from the Task instance using the Task.AsyncState.

    var task = Task.Factory.FromAsync<TResult>(...);
    //You can use this state by task.AsyncState object state = task.AsyncState;
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 27, 2015 6:27 AM
    Moderator