UDP Interaction

using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class UDPSend : MonoBehaviour
{
    private static int localPort;
 
    // prefs
    private string IP;  // define in init
    public int port;  // define in init
 
    // "connection" things
    IPEndPoint remoteEndPoint;
    UdpClient client;
 
    // gui
    string strMessage="";
 
     
    // call it from shell (as program)
    private static void Main()
    {
        UDPSend sendObj=new UDPSend();
        sendObj.init();
     
        // testing via console
        // sendObj.inputFromConsole();
     
        // as server sending endless
        sendObj.sendEndless(" endless infos \n");
     
    }
    // start from unity3d
    public void Start()
    {
        init();
    }
 
    // OnGUI
    void OnGUI()
    {
        Rect rectObj=new Rect(40,380,200,400);
            GUIStyle style = new GUIStyle();
                style.alignment = TextAnchor.UpperLeft;
        GUI.Box(rectObj,"# UDPSend-Data\n127.0.0.1 "+port+" #\n"
                    + "shell> nc -lu 127.0.0.1  "+port+" \n"
                ,style);
     
        // ------------------------
        // send it
        // ------------------------
        strMessage=GUI.TextField(new Rect(40,420,140,20),strMessage);
        if (GUI.Button(new Rect(190,420,40,20),"send"))
        {
            sendString(strMessage+"\n");
        }    
    }
 
    // init
    public void init()
    {
        // Endpunkt definieren, von dem die Nachrichten gesendet werden.
        print("UDPSend.init()");
     
        // define
        IP="127.0.0.1";
        port=8051;
     
        // ----------------------------
        // Senden
        // ----------------------------
        remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), port);
        client = new UdpClient();
     
        // status
        print("Sending to "+IP+" : "+port);
        print("Testing: nc -lu "+IP+" : "+port);
 
    }
    // inputFromConsole
    private void inputFromConsole()
    {
        try
        {
            string text;
            do
            {
                text = Console.ReadLine();
                // Den Text zum Remote-Client senden.
                if (text != "")
                {
                    // Daten mit der UTF8-Kodierung in das Binärformat kodieren.
                    byte[] data = Encoding.UTF8.GetBytes(text);
                    // Den Text zum Remote-Client senden.
                    client.Send(data, data.Length, remoteEndPoint);
                }
            } while (text != "");
        }
        catch (Exception err)
        {
            print(err.ToString());
        }
    }
    // sendData
    private void sendString(string message)
    {
        try
        {
                //if (message != "")
                //{
                    // Daten mit der UTF8-Kodierung in das Binärformat kodieren.
                    byte[] data = Encoding.UTF8.GetBytes(message);
                    // Den message zum Remote-Client senden.
                    client.Send(data, data.Length, remoteEndPoint);
                //}
        }
        catch (Exception err)
        {
            print(err.ToString());
        }
    }
 
 
    // endless test
    private void sendEndless(string testStr)
    {
        do
        {
            sendString(testStr);
         
         
        }
        while(true);
     
    }
 
}
using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class UDPReceive : MonoBehaviour {
 
    // receiving Thread
    Thread receiveThread;
    // udpclient object
    UdpClient client;
    // public
    // public string IP = "127.0.0.1"; default local
    public int port; // define > init
    // infos
    public string lastReceivedUDPPacket="";
    public string allReceivedUDPPackets=""; // clean up this from time to time!
 
 
    // start from shell
    private static void Main()
    {
       UDPReceive receiveObj=new UDPReceive();
       receiveObj.init();
        string text="";
        do
        {
             text = Console.ReadLine();
        }
        while(!text.Equals("exit"));
    }
    // start from unity3d
    public void Start()
    {
     
        init();
    }
 
    // OnGUI
    void OnGUI()
    {
        Rect rectObj=new Rect(40,10,200,400);
            GUIStyle style = new GUIStyle();
                style.alignment = TextAnchor.UpperLeft;
        GUI.Box(rectObj,"# UDPReceive\n106.51.8.242 "+port+" #\n"
                    + "shell> nc -u 106.51.8.242 : "+port+" \n"
                    + "\nLast Packet: \n"+ lastReceivedUDPPacket
                    + "\n\nAll Messages: \n"+allReceivedUDPPackets
                ,style);
    }
     
    // init
    private void init()
    {
        // Endpunkt definieren, von dem die Nachrichten gesendet werden.
        print("UDPSend.init()");
     
        // define port
        port = 443;
        // status
        print("Sending to 127.0.0.1 : "+port);
        print("Test-Sending to this Port: nc -u 127.0.0.1  "+port+"");
 
        // ----------------------------
        // Abhören
        // ----------------------------
        // Lokalen Endpunkt definieren (wo Nachrichten empfangen werden).
        // Einen neuen Thread für den Empfang eingehender Nachrichten erstellen.
        receiveThread = new Thread(
            new ThreadStart(ReceiveData));
        receiveThread.IsBackground = true;
        receiveThread.Start();
    }
    // receive thread
    private  void ReceiveData()
    {
        client = new UdpClient(port);
        while (true)
        {
            try
            {
                // Bytes empfangen.
                IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
                byte[] data = client.Receive(ref anyIP);
                // Bytes mit der UTF8-Kodierung in das Textformat kodieren.
                string text = Encoding.UTF8.GetString(data);
                // Den abgerufenen Text anzeigen.
                print(">> " + text);
             
                // latest UDPpacket
                lastReceivedUDPPacket=text;
             
                // ....
                allReceivedUDPPackets=allReceivedUDPPackets+text;
             
            }
            catch (Exception err)
            {
                print(err.ToString());
            }
        }
    }
 
    // getLatestUDPPacket
    // cleans up the rest
    public string getLatestUDPPacket()
    {
        allReceivedUDPPackets="";
        return lastReceivedUDPPacket;
    }
}

I am trying to send co-ordinates as string from one system to other, read the string in the next system and move a object accordingly. I am using UDP for the transmission purpose. I got the code to send and recieve string using UDP from the forum itself but it uses a dummy IP and port to connect which only works if both the programs i.e send and recieve are on the same system which does not suffice my requirement. I tried changing the IP to the IP of the other system but i can’t figure what should i change the port to.
Can someone tell how to know the port of any server or what should i change?

Hi @[unity_4J-FEv3jCWkBSQ]( UDP Interaction members/unity_4j-fev3jcwkbsq.4205436/),

Your question doesn’t look to be specific to Package Management. I suggest you post your question in either the
Editor & General Support forum.

Regards,

Pascal