Multi-thread and UDP communication in C#
In my last project I also used the UDP system in c#. I have learned the course of computer network, but have never used c# or .NET(is UDP a part of .NET frame?) for programming.
I know that UDP do not require a connection between the client and the server. It is more like a broadcast system, the client and the server can send message to each other, but they can not confirm that if the other side have received the message completely. But I have no idea about how to use it to convey messages in an instance of system.
After a period of researching, I found that the basement of UDP in C# is multi-thread. Actually in my experience with unity and C#, I have never used the multi-thread system. At the most time, the coroutine and the function “Invoke” can fulfill most of my requirements. However, if I need to use UDP to receive message in C#, at least I need a while(true) loop in both the client and the server, to keep listening if there is an UDP message needs to be received. In that case, if I do not use the multi-thread, the main thread would stuck in this receive loop and can not do any other operation.
We can just regard a simple UDP client(or server) as two threads: the main thread can deal with any events in the scene, such as button click or keyboard input, and act accordingly(including graphic changes or sending message), meanwhile the auxiliary thread can keep the while(true) receive circle to detect any message that sent to the certain IP address and port. Once it find there is a message, it would receive and tell the main thread to act accordingly.(through coroutine maybe)
As for the concrete realization of multi-thread in C#, at first the operations of the auxiliary thread needed to be defined in a function. Then it is convenient to use “Thread auxiliaryThread = new Thread(new ThreadStart(FunctionName))” to declare an auxiliary thread, then the thread will run the function automatically. But when I was tried to write a testing program, which have a simple auxiliary thread with function follows:
Void multi_threadTest()
{
while(true)
{
Debug.Log(1);
}
}
The program compile and run auspiciously, while when I tried to stop the program by press the stop button in the unity engine, I found that even though the program has been stopped, that “Debug.Log” was keep printing 1 every period of time, and the whole unity would been stuck if I try to start the program again.
![]() |
| Keep Writing "1"(use my modifying jam game as background) |
I searched in the internet to find how to stop a multi-thread completely, and there was a blog(http://www.manongjc.com/article/33232.html, Chinese blog) told me that I need to use a bool to break the while(true) loop, then use Thread.Abort() to stop a multi-thread. So changed the program:
void multi_threadTest()
{
while(true && test)
{
print(1);
}
}
void OnApplicationQuit()
{
test = false;
auxiliaryThread.Abort();
}
Now the multi-thread can be stopped when if I press the stop button.
![]() |
| Stop Output After Press Stop Button |
As for the UDP part, though in our project there is a lot of cross call of scripts to deal with messages that need to be sent, and transfer it to binary form, but in a word, I used “udpClient.Send(sendBytes, sendBytes.Length);” to send my message, which is also convenient to use; and use “udpServer.ReceiveFrom(data, ref remoteEndPoint);” to receive the UDP message in the multi-thread. There is nothing special about these part.


评论
发表评论