none
Adding on additional functions to asynchronous client server RRS feed

  • General discussion

  • My goal is to be able to add functions to an existing sample provided here:
    https://github.com/AbleOpus/NetworkingSamples/blob/master/BasicAsyncClient/ClientForm.cs

    https://github.com/AbleOpus/NetworkingSamples/blob/master/BasicAsyncServer/ServerForm.cs

    I have worked with several different samples for using sockets for networking, however, they do not go much in depth or are too complex for me.

    So here is where I would like to be able to add additional functions to the existing sample.

    The existing code has a way to process messages, let us say I want to add a way to send a file, or receive a file, etc... any number of functions.

    I decided I could use the first byte sent be the indicator of what is being received on the other end. I wanted to be able to make it easier for coding as well for less confusion and readability, so I made an Enum which would be converted into a byte and cast back into the Enum value. I called it <g class="gr_ gr_1040 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="1040" id="1040">headertype</g> due to lack of <g class="gr_ gr_1081 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="1081" id="1081">better name</g> for now.

     public enum HeaderType { Message, FileDownload, FileUpload };

    To get the Enum value from the buffer on the receiving end I'm using: 

     HeaderType enuHeaderType = (HeaderType)Convert.ToInt32(bytBuffer[0]);

    To convert it to a byte I'm using the BitConverter class:

    BitConverter.GetBytes((byte)HeaderType.FileDownload)

    Since the above can be done just by casting it as a byte I used the BitConverter, in addition, a list of bytes to be able to add other bytes onto the list. This way I don't have to worry about the index to copy the bytes into the byte array. I can just simply do something like this:

    List<byte> byteList = new List<byte>();
                byteList.AddRange(BitConverter.GetBytes((byte)HeaderType.FileDownload)); //filedownload byte
                byteList.AddRange(BitConverter.GetBytes(intFileHeaderSize)); //file header size
                byteList.AddRange(bytFileHeader); //file header
                byteList.AddRange(bytFile); //the file itself

    This is where my issue comes in... with the <g class="gr_ gr_2162 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace gr-progress" data-gr-id="2162" id="2162">recievecallback</g> function... I am using the enum to direct what to do with the data.

     private void ReadHeader(IAsyncResult ar)
            {
                try
                {
                    // Read data from the client socket.   
                    int intBytesRead = socket.EndReceive(ar);
                    
                    if (intBytesRead > 0)
                    {
                        HeaderType EnuHeaderType = (HeaderType)Convert.ToInt32(BytHeader[0]);
    
                        switch (EnuHeaderType)
                        {
                            case HeaderType.Message:
                                //call back MsgRecieved, at buffersize requested
                                socket.BeginReceive(BytBuffer, 0, IntBufferSize, SocketFlags.None, new AsyncCallback(MsgRecieved), socket); //call this back to get rest of data
                                break;
                            case HeaderType.FileDownload:
                                //call back FileIncoming
                                socket.BeginReceive(BytBuffer, 0, intFileHeaderSize, SocketFlags.None, new AsyncCallback(FileIncoming), socket); 
                                break;
                            default:
                                // Not all data received. Get next data header 
                                socket.BeginReceive(BytHeader, 0, IntHeaderSize, SocketFlags.None, new AsyncCallback(ReadHeader), socket);
                                break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Disconnected.Invoke(this, new ClientDisconnectedArgs());
                }
            }

    My main confusion with the socket.BeginRecieve ... Here is my understanding of how it works.
    BeginRecieve is called and told how many bytes to receive, placing the data into the buffer, upon complete calls the AsyncCallback which will handle the data.

    In that AsyncCallback it is to handle the data and then upon completion tell to look for the next incoming byte header.

    Currently, I am having the first read callback only try to read one byte which is the byte that is sent to indicate the type of data... which upon receiving it will tell other receive callback functions to do the handling of that type.

    However, I am not sure I'm doing this correctly. Tips, and or links to improve the flow direction of handling the incoming data would be great! Thank you.


    Dim Blondedude092 as SkypeUser

    • Changed type Dmitry Post Friday, December 29, 2017 2:45 AM
    Friday, December 29, 2017 2:35 AM