- Home /
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 :)
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!!!
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;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612134737im_/https://answers.unity.com/themes/thub/images/avi.jpg)