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 :slight_smile:

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;
    }