Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# non-blocking socket without while(true) loop

I'm just trying to make some socket programming, using non-blocking sockets in c#. The various samples that i've found, such as this, seems to use a while(true) loop, but this approach causes the cpu to burst at 100%. Is there a way to use non-blocking sockets using a event programming style? Thanks

like image 434
ʞᴉɯ Avatar asked Nov 03 '09 07:11

ʞᴉɯ


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Why is C named so?

Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.


3 Answers

See the MSDN example here. The example shows how to receive data asynchronously. You can also use the Socket BeginSend/EndSend methods to send data asynchronously.

You should note that the callback delegate executes in the context of a ThreadPool thread. This is important if the data received inside the callback needs to be shared with another thread, e.g., the main UI thread that displays the data in a Windows form. If so, you will need to synchronized access to the data using the lock keyword, for example.

As you've noticed, with nonblocking sockets and a while loop, the processor is pegged at 100%. The asynchronous model will only invoke the callback delegate when there is data to send or receive.

like image 53
Matt Davis Avatar answered Nov 04 '22 03:11

Matt Davis


Talking generally about blocking/non-blocking IO, applicable generally:

The key thing is that in real life your program does other things whilst not doing IO. The examples are all contrived in this way.

In blocking IO, your thread 'blocks' while waiting for IO. The OS goes and does other things, e.g. allows other threads to run. So your application can do many things (conceptually) in parallel by using many threads.

In non-blocking IO, your thread queries to see if IO is possible, and otherwise goes and does something else. So you do many things in parallel by explicitly - at an application level - swapping between them.

like image 27
Will Avatar answered Nov 04 '22 03:11

Will


To avoid a CPU issue in heavy while loop, when no data receive put thread.sleep(100) or less. That will let other processes change to do their task

like image 25
TuanAnh207 Avatar answered Nov 04 '22 05:11

TuanAnh207