- Home /
Unity to Node.js UDP: datagrams received are all empty.
Hello,
CODE BELOW
Having some trouble getting this to work, I can send to the server fine, but all messages from the server come back empty. Output from client shows up as null so... it knows a message was received. No error server side or client side. Just empty buffers ;(
I have also tried the Asyncronous setup (BeginRecieve -> EndRecieve). Exact same results, just receiving empty packets.
Does anyone spot anything obviously wrong systemically or otherwise?
Any and all help is appreciated!
Client:
IPEndPoint server;
UdpClient client = new UdpClient ();
UTF8Encoding encoding = new UTF8Encoding ();
Thread networkThread;
string serverIP = "127.0.0.1";
int serverHTTPPort = 9081;
int serverUDPPort = 9082;
bool connected;
void UDPThread () {
while (true) {
byte[] buffer = client.Receive (ref server);
UDPReceive (buffer);
}
}
void UDPReceive(byte[] buffer){
string message = encoding.GetString (buffer);
JsonObject json = JsonUtility.FromJson <JsonObject> (message);
Debug.Log (json);
receive (message);
}
void UDPSend (string target, string action, params string[] values){
JsonObject requestJson = new JsonObject (target, action, values);
string requestString = JsonUtility.ToJson (requestJson);
byte[] request = encoding.GetBytes (requestString);
//client.Send (request, request.Length, serverIP, serverUDPPort);
client.Send (request, request.Length);
}
public void UDPConnect () {
networkThread = new Thread (new ThreadStart (UDPThread));
server = new IPEndPoint (System.Net.IPAddress.Parse (serverIP), serverUDPPort);
networkThread.IsBackground = true;
client.Connect (server);
networkThread.Start ();
}
public void UDPDisconnect () {
networkThread.Abort ();
client.Close ();
}
Client Output:
Server:
udpServer.on ('message', function (message, rinfo) {
controller.NETWORK.receive (message, rinfo, function (result) {
send (result, rinfo.port, rinfo.address);
})
});
function send (message, port, address) {
udpServer.send (message.toString (), message.length, 0, port, address, function (err) {
if (!err) {
console.log ('UDP => Sending: ' + message);
} else {
console.log (err);
}
});
}
Server Output:
ps: You should implement a way to gracefully ter$$anonymous$$ate your listener thread. Endless running threads are not really a problem in a build since they are ter$$anonymous$$ated when your application is ter$$anonymous$$ated. However inside the editor such threads can cause problems as they are not ter$$anonymous$$ated when you exit playmode.
Hi friend, this is my first message in Unity forum, i have never need to do a question here. Interesting post and trouble, i have exactly the same problem, i continue testing but no luck, did you find some solution?.
Best regards Roberto Peña PH-ELECTRONICA
Answer by Bunny83 · May 24, 2016 at 10:55 PM
Well, is the message actually empty of just the deserialized object? It's possible that the FromJson method failed to create the object from the json data. How is "JsonObject" defined? Is the class derived from another class? Is it marked as Serializable? Does it have a parameterless default constructor? If not the JsonUtility can't create an instance of the class.
Thanks for your reply, I am really looking to get this resolved. I checked the length of the buffer itself and it was 0. The dgrams are making it to the server and are functioning as expected, I also send HTTP messages using my JsonObject and it functions correctly. But here it is anyhow:
[System.Serializable]
public class JsonObject : System.Object {
public string target;
public string action;
public string [] values;
public JsonObject (string target, string action, string[] array) {
this.target = target;
this.action = action;
values = array;
}
}
If the buffer is really empty there might be something wrong with your network setup. Do you have WireShark? I suggest you inspect the traffic between your server and your client in order to get a clue where it goes wrong.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081507im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UDPSend not working with iPad over LAN 0 Answers
How to set up a UDP connection with a nodejs server in Unity? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Send Prefab by RPC? 1 Answer