Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Async. Socket Begin/EndReceive Loop Woes

Async. Socket Begin/EndReceive Loop Woes

Scheduled Pinned Locked Moved C#
helpdata-structuresjson
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    foahchon
    wrote on last edited by
    #1

    Hi, I'm trying to write a receive loop in my socket class that will receive what data is in the buffer, call an event to let the user know that there's new data, and then receive the rest of the data. It kinda almost works; it receives much of the data, but seems to miss the last packet or two. Here is the code in the loop:

    private void OnSockReceive(IAsyncResult ar)
    {

    try
    {
        int read = socket.EndReceive(ar);
    
        if (read > 0)
        {                    
            OnReceive();                    
        }
        socket.BeginReceive(ReceivedData, 0, ReceivedData.Length, SocketFlags.None, OnSockReceive, null);
    }
    
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "OnSockReceive", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    

    }

    ReceivedData is a global byte array of size 1024. Any help would be greatly appreciated. Thanks.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups