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 JacobFlynnPrendi · Sep 13, 2017 at 01:36 AM · networkingudp

Send String from One client to Another using UDP

Hi,

I'm brand new to Networking but have a good working of c#.

What i'm trying to do is when i click a UI Button on one open copy of the application, i want to send a string to Another Open Copy and get it to overwrite the UI Text on the screen with the string i receive.

I've tried numerous solutions, none of which have worked for me, probably due to my own incompetence, none the less, i'm just after a super easy solution to get me going in the right direction. As far as i'm aware, using UDP appears to be the best way to do this?

Thanks for the help :)

Comment
Add comment · Show 1
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 toddisarockstar · Sep 13, 2017 at 03:15 AM 0
Share

UDP is designed for faster speed but unreliable for accuracy. TCP would be preferable for messaging because TCP protocal is checked for accuracy so that you dont get missing or incorrent bytes back through the internet . Unity's neworking system should handle a lot of this sort of thing in the background for you so you dont need to worry about it. I havent used unity's new network system so i cant give specific details but messaging should be done through RPC calls in unity. If you where coding in C# and creating your own network system, the most dificult thing you would run into is creating the NAT punch through with your server to make direct connections between users. if you are not intending to use unity's master server and network, and just need simple messages to appear, the easiest way would be to just have the game copies send messages to the server, then have all the clients request those messages back from the server. I think people could give you more specific answers if we knew more specifacally what you intend to accomplish, how much data you are sending and how time critical the messages are. good luck!!!

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by GunLengend · Sep 13, 2017 at 03:33 AM

As what i'm thinking you're trying to make simple chat system, maybe. And this is Unity Answer so i suggest a solution in Unity Networking only.

1.Create GameObject call Server, attached Server.cs with script below :

 using UnityEngine;
 using UnityEngine.Networking;
 using System.Collections;
 
 public class Server : MonoBehaviour
 {
     void Start () 
     {
     listenPort = 8888;
         //Listening...
         NetworkServer.Listen(listenPort);
 
         //System handle
         NetworkServer.RegisterHandler(MsgType.Connect, OnClientConnected);
         NetworkServer.RegisterHandler(MsgType.Disconnect, OnClientDisconnected);
         
         //Custom handle - here is your string come
         NetworkServer.RegisterHandler(ZMessageType.String, OnServerReceiveStringMessage);
      }
     
      //Receive function
      void OnServerReceiveStringMessage(NetworkMessage netMsg)
      {
         var stringMsg = netMsg.ReadMessage<StringMessage>();
          Debug.Log("You string text: " + stringMsg.stringText)
         //Now you can do anything here to display on screen in the first "open copy"
      } 
 
     //Sending function
     public void OnServerSendStringMessage(int connectionId)
    {
        StringMessage stringMsg = new StringMessage();
        stringMsg.stringText = "Your message here, write what u wanna do";
        NetworkServer.SendToClient(connectionId, ZMessageType.String, stringMsg);
    }
 }

2.Create GameObject called Client and attach Client.cs below :

 using UnityEngine;
 using UnityEngine.Networking;
 using System.Collections;
 
 public class Client : MonoBehaviour 
 {
     public NetworkClient m_Client = new NetworkClient();
     void Start () 
     {
         string serverIP = 127.0.0.1; //Local is 127.0.0.1, if u test online server, please change.
         int serverPort = 8888;    //same as Server.cs port
 
         //Connecting...
         m_Client.Connect(serverIP, serverPort);
 
         //System handle
         m_Client.RegisterHandler(MsgType.Connect, ClientConnected);
         m_Client.RegisterHandler(MsgType.Disconnect, ClientDisconnected);
 
         //Custom handle, u message listen here
         m_Client.RegisterHandler(ZMessageType.STRING,ClientReceiveStringMessage);
     }
 
     //Receive function
     void ClientReceiveStringMessage(NetworkMessage netMsg)
     {
         var stringMsg = netMsg.Read<StringMessage>();
         Debug.Log("Your message is " + stringMsg.stringText);
         //Do what u wanna do here, display UI, etc..
     }
     //Send function
     //Same like Server.cs, just copy
 }

3.Create a C# class have any name u want, i create ZMessage.cs, don't attach to any object please

     using UnityEngine;
     using UnityEngine.Networking;
     using System.Collections;
    
     public class ZMessageType
     {
         public static short STRING          = 5000;
     }
     
     public class StringMessage : MessageBase
     {
         public string stringValue;
     }















Comment
Add comment · 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

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

99 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

Related Questions

How can I receive UDP packets into Unity Webplayer? 1 Answer

UDP implementation in Unity, unable to send to two different machines 0 Answers

C# Networking: UDP Packets dont arrive on Android 2 Answers

Unity networking tutorial? 6 Answers

UDP Server Or Master Server 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