I’m writing a simple game that requires continuous connection to multiple UDP Servers. Communication works like a charm when I test it using multiple console applications in Visual Studio.
One console application has multiple UDP clients connected to servers (other console applications). At each iteration loop without any delay I send to all services a message and get response instantly (thousands messages send and received per second). No latency as I’m testing it on a single PC each package send and received in less than half of millisecond.
but …
Now I tried this scenario with Unity. I’m new to Unity (my first project) quite straightforward for me was to try use MonoBehaviour.Update method as a place for my messaging requests. It works but only for one connection (socket) per update. So if I iterate my connections and address different sockets per update messages will not reach destination. Unfortunately trying to send messages to more sockets in a single update call results with only single success (first socket will receive messages). What’s funny placing breakpoint helps, messages are being send correctly.
so …
Mabey putting such calls in MonoBehaviour.Update was not a good idea at all, so I started a new thread and called communication functions within it but it did not solved anything. I can’t without appropriate delay send successfully message to more than 1 UDP server (socket).
Within one UDP socket I can send as many messages as I want with success. Problem occurs when I am connected with more than one and want to exchange messages with them at same time.
Any idea what might be the reason of such problem and how to overcome it ? I can thread sleep between different sockets, but it will introduce latency and seems impropriate.
You will need to change some of my protocol buffer specific stuff, but this is a proven udp client and its’ been tested with multiple instances. You will need the Spicy Pixel concurrency kit, which is free on the asset store or github.
Note I’m putting incoming messages into a concurrent queue, and then in another component in Update I’m checking to see if there are any to dequeue, where I dequeue X number at a time.
Not sure what you mean by one socket at a time. At a low level with udp you create a local socket, then every outgoing message you set the destination. You can send to thousands of different servers using one socket. But where your code is having problems no way to tell without seeing it.
So to be clear my unity game is ruining along with 3 or more other applications on one PC. Other applications work as services accepting upd pockets on specified ports. My unity game opens 3 sockets (udp clients) and starts exchanging messages with 3 other applications.
When testing this scenario in Visual Studio with client written as a console application, everything works like predicted.
I can use 3 sockets at same time without problems and everything is properly delivered.
When i try this with Unity project as a client I have to introduce a delay between I can successfully use another socket (client1 , client2 or client3).
so the above scenario in unity project will success only with client1 and other are omitted unless I delay that send appropriately.
yet it will work correctly if I submit multiple messages using one socket client1 for example:
I’m hoping someone had some similar issues on their way, and has some simple pinpoints, but unless someone will answer first I’ll reproduce a small experimental projects and try to test it with as small logic as possible. Once I have it ready and it will confirm my problem Ill attach it here it will take some time tho, thanks snactime for your time
The problem is in the code you aren’t showing us. Send will return the number of bytes actually sent. It doesn’t lie. If it say’s it sent X bytes that’s what it sent. So unless it’s returning 0 your message is being sent and your problem is elsewhere, like in the receiving end or somewhere in between.
Confirmed bug must be somewhere in my code ;/ Started clean project with just UDPprotocol working in loop everything works;/
one word “static” and whole day of pain, one field deep down in corresponding functions was marked static due to ctrl-c → ctrl-v, 5 new projects, lots of tests and liters of coffee …