Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by STBarnard · May 24, 2016 at 10:35 PM · c#networkudpnodejs

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: alt text

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:

alt text

client-output.png (12.7 kB)
server-output.png (12.2 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · May 24, 2016 at 10:59 PM 0
Share

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.

avatar image STBarnard Bunny83 · May 25, 2016 at 12:02 AM 0
Share

I abort the thread on Application exit.

avatar image modonga · Jun 05, 2016 at 04:08 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image STBarnard · May 25, 2016 at 12:06 AM 0
Share

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;
         }
     }
avatar image Bunny83 STBarnard · May 26, 2016 at 03:17 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

155 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges