Skip to main content
GameDev.net gamedev.net
🔒 Locked

C# Async Sockets (Non-Blocking)?

Started by GeekPlusPlus Feb 24, 2005 at 6:42 PM 1 replies 1.3k views
Original Post
GeekPlusPlus
GeekPlusPlus
Hello, I wrote a few C++ programs using windows AsyncSockets. And it seemed like an excellent way to do non-blocking. Events own. I was wondering if anyone knows of any tutorials or could tell me how I'd do something similar in C#?
Newb Programmer: Geek++
Holy Fuzz
Holy Fuzz
I am not an experience network programmer, but I recently started to play around with sockets (System.Net.Sockets.UdpClient) in C# and ran into the same problem.

My solution was to create a seperate thread that simply calls the Recieve() method, pushes the recieved packet onto a queue, and loops continuously. Then in my main thread I can check the queue whenever I want without worrying about my main program being blocked.

I did run into a problem where the looping thread would not abort when I told it to because it was being blocked by the call to Recieve(). I solved this problem by having my UdpClient send a blank packet to itself, which lets Recieve() return and thus the thread abort.

If someone has a better solution, let me know.

- Fuzz

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.