Asyncronous Socket Problem

In The Name God

hi

i got a problem in Asyncronous sockets, i wanna get back the byte array received by an asyncronous socket. but Asynccallback only accept the function which are void. by a global variable i cannot do that, What can i do

thanks in advance



Answer this question

Asyncronous Socket Problem

  • Jonas.Lewin

    In The Name of God

    hello,

    thanks for your response,

    in your response you said that instead of sending the socket to the Asynccallbak method we send a class object right but the method that you wanna send to asynccallback got the IAsyncResult argument that's not got any enumeration like UserState please tell me how shoud i do this

    Error 1 'System.IAsyncResult' does not contain a definition for 'UserState'


    thanks




  • deo1979

    You don't need the global variables. The general technique is to create a state class lets say

    class MyState
    {
    Socket s;
    byte[] SendBuffer = new byte[...];
    unsigned long SendOffset
    byte[] ReceiveBuffer = new byte[...];
    unsigned long ReceiveOffset;
    }

    and when you post an async operation do something like
    s.BeginReceive(new AsyncCallback(...), MyState m)

    In the Callback

    you can get the state by using

    MyState m = (MyState)IAsyncResuct.UserState




  • Rick Walker

    In The Name of God

    hello

    my problem solved.

    your implementation (total structure) was correct, but that didn't have enough consideration.
    I mean there were some some points that must have been observed.

    1- IAsyncResult doesn't have any enumeration like UserState.
    2- if u use define the MyState class object and send it to AsyncCallBack delegate it generates an exception cause you have not referenced it.
    3- if you reference your object but don't fill it with proper data another exception would be raised and tell that the socket in your class is Nothing. so you must fill the object before you send it to AsyncCallBack.

    so the proper code would be :

    this is the class that you told me but in VB
    -------------------------------------------


    Public Class MyState

    Public s As Socket

    Public SendBuffer(1024) As Byte

    Public SendOffset As Long

    Public ReceiveBuffer(1024) As Byte

    Public ReceiveOffset As Long

    'any kinda of necessary data can be here
    End Class

    '----------------------------------------------------
    'Main primary function ( sck_Accepted_Socket is an accepted socket )
    '-----------------------------------------------------


    Private Sub btn_Server_Receive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Server_Send.Click

    Dim M As New MyState
    M.s = sck_Accepted_Socket
    M.SendBuffer = byt_Buffer

    sck_Accepted_Socket.BeginReceive(byt_Buffer, 0, 5, SocketFlags.None, New AsyncCallback(AddressOf Receive_Delegate), M)

    txt_Server.Text = Encoding.ASCII.GetString(byt_Buffer, 0, 5)
    MessageBox.Show(Encoding.ASCII.GetString(M.SendBuffer))

    End Sub

    ------------------------------------------------------
    the receive delegate
    -----------------------------------------------------

    Private Sub Receive_Delegate(ByVal iar As IAsyncResult)

    Dim M As MyState = iar.AsyncState
    M.s.EndReceive(iar)

    End Sub

    '----------------------------------------------------


    'and at last special thanks for your help.


  • Asyncronous Socket Problem