TCP server in Unity

Is it possible to modify following code to work in Unity as a TCP server? How would you do that? I’ve tried to convert the Main entry point into Update-function or coroutine, but Unity ‘freezes’ and nothing happens.
Why?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace TCPserver
{
   

    class MyTcpListener
    {
        public static void Main()
        {
            TcpListener server = null;
            try
            {
                // Set the TcpListener on port 13000.
                Int32 port = 13000;
                IPAddress localAddr = IPAddress.Parse("192.168.1.36");

                // TcpListener server = new TcpListener(port);
                server = new TcpListener(localAddr, port);

                // Start listening for client requests.
                server.Start();

                // Buffer for reading data
                Byte[] bytes = new Byte[256];
                String data = null;

                // Enter the listening loop. 
                while (true)
                {
                    Console.Write("Waiting for a connection... ");

                    // Perform a blocking call to accept requests. 
                    // You could also user server.AcceptSocket() here.
                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("Connected!");

                    data = null;

                    // Get a stream object for reading and writing
                    NetworkStream stream = client.GetStream();

                    int i;

                    // Loop to receive all the data sent by the client. 
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        // Translate data bytes to a ASCII string.
                        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        Console.WriteLine("Received: {0}", data);

                        // Process the data sent by the client.
                        data = data.ToUpper();

                        byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                        // Send back a response.
                        stream.Write(msg, 0, msg.Length);
                        Console.WriteLine("Sent: {0}", data);
                    }

                    // Shutdown and end connection
                    client.Close();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                // Stop listening for new clients.
                server.Stop();
            }


            Console.WriteLine("\nHit enter to continue...");
            Console.Read();
        }
    }
}

I was going to do it for you… but it’s not simple… :confused:

Firstly make sure you class inherits from Monobehaviour then you need to put all the setup code into either Awake or Start. I would then start a coroutine for the while loop because that’s what’s freezing your code, it never exits the while loop to continue rendering/physic-ing/etc. :confused: Finally you need to convert all the Console.WriteLine statements to Debug.Log so you can see the output.

Networking code can be annoying at times, trust me…

Thanks for the reply. Thats actually what I’ve already done, but the code is still freezing:

using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Text;
using System;
public class TCPserver:MonoBehaviour{
    public bool isConnection;
    // Use this for initialization
    void Start(){
        isConnection=false;
        print ("StartCoroutine");
        StartCoroutine(Update1());
    }
   
    // Update is called once per frame
    IEnumerator Update1()
    {   
        TcpListener server=null;  
        try
        {
            // Set the TcpListener on port 13000.
            Int32 port = 13000;
            IPAddress localAddr = IPAddress.Parse("192.168.1.36");
           
            // TcpListener server = new TcpListener(port);
            server = new TcpListener(localAddr, port);
           
            // Start listening for client requests.
            server.Start();
           
            // Buffer for reading data
            Byte[] bytes = new Byte[256];
            String data = null;
           
            // Enter the listening loop.
            while(!isConnection)
            {
                Debug.Log("Waiting for a connection... ");
               
                // Perform a blocking call to accept requests.
                // You could also user server.AcceptSocket() here.
                TcpClient client = server.AcceptTcpClient();   
                if(client!=null){

                    Debug.Log("Connected!");
                    isConnection=true;
                    client.Close();
                    //break;

                }
                data = null;
               
                // Get a stream object for reading and writing
                NetworkStream stream = client.GetStream();
               
                int i;
               
                // Loop to receive all the data sent by the client.
                while((i = stream.Read(bytes, 0, bytes.Length))!=0)
                {  
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    Debug.Log("Received:"+ data);
                   
                    // Process the data sent by the client.
                    data = data.ToUpper();
                   
                    byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
                   
                    // Send back a response.
                    stream.Write(msg, 0, msg.Length);
                    Debug.Log("Sent:"+ data);           
                }

                // Shutdown and end connection
                client.Close();
            }
        }
        catch(SocketException e)
        {
            Debug.Log("SocketException:"+ e);
        }
        finally
        {
            // Stop listening for new clients.
            server.Stop();
        }

        yield return null;
   
   
    }
}

I used thread (using System.Threading namespace) instead of coroutine and got the code working.

Hey would you be kind enough to post the final code, I really need it!

Here’s the final server code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Text;
using System;
using System.Threading;

public class TCPserver:MonoBehaviour{
    public bool isConnection,senddata1,senddata2,valuechanged,valuechanged2;
    //public bool isZoomed,isPointsSet,isZooming,Zoompressed,rotatearound1,rotatearound2,rotatearound3,rotatearound4,distanceChange1,distanceChange2;
    public List<string> stairways=new List<string>();
    public List<bool> floorsBool = new List<bool> ();
    public List<bool> roomsBool = new List<bool> ();
    public List<bool> stairwaysBool = new List<bool> ();
    public List<int> rooms = new List<int> ();
    public List<GameObject> flats = new List<GameObject> ();
    public int floors;
    Zoom zoom;
    ShowAndHide show;
    public Thread mThread;
    public TcpListener server;
    public TcpClient client;
    public NetworkStream stream;
    byte[] msg1;
    string toSend;
    bool available,hide,show1,selection,endpointChanged,parsedata;
    public String data,prevdata;
    float frametime=0.0f;
    int q=0;
    int FPS=0;
    GameObject building;
    Vector3 pos;
    Sun1 sun;
    WindowLight wl;
    GameObject shader;
    // Use this for initialization
    void Start(){
        shader=GameObject.FindGameObjectWithTag ("Shader");
        building=GameObject.FindGameObjectWithTag("Building");
        sun = GameObject.FindGameObjectWithTag ("Light").GetComponent<Sun1> ();
        wl = GameObject.FindGameObjectWithTag ("Light").GetComponent<WindowLight> ();
        flats = getFlats ();
        floorsBool=InitializeFloorsBool ();
        roomsBool = InitializeRoomsBool ();
        stairwaysBool = InitializeStairwaysBool ();
        zoom = Camera.main.GetComponent<Zoom> ();
        show = Camera.main.GetComponent<ShowAndHide> ();
        isConnection=false;
        senddata1 = false;
        senddata2 = false;
        //print ("StartThread");
        ThreadStart ts = new ThreadStart(Update1);
        mThread = new Thread(ts);
        mThread.Start();
    }
    IEnumerator startThings(){
        for(int j=0;j<flats.Count;j++){
           
            StartCoroutine(parseName(flats[j]));
            yield return null;
        }
        yield return null;
    }
    // Update is called once per frame
    void Update(){
        //print (data);
        //FPScounter();
        if(hide){
            //print ("Hide:"+building.name);
            StartCoroutine(show.Hide(building,4,5.8f,6.5f,0.0025f));
            StartCoroutine(show.Hide(building,3,6.1f,6.4f,0.0025f));
            StartCoroutine(show.Hide(building,2,5.8f,6.1f,0.0025f));
            StartCoroutine(show.Hide(building,1,6.1f,6.4f,0.0025f));
            StartCoroutine(show.Hide(building,0,5.8f,6.4f,0.0025f));
            hide=false;
            for(int i=0;i<533;i++){
                wl.windows[i].SetActive(false);
            }
            shader.SetActive(false);
        }
        if(show1){
            //print ("Hide:"+building.name);
            StartCoroutine(show.Show(building,4,5.8f,6.5f,0.0025f));
            StartCoroutine(show.Show(building,3,6.1f,6.4f,0.0025f));
            StartCoroutine(show.Show(building,2,5.8f,6.1f,0.0025f));
            StartCoroutine(show.Show(building,1,6.1f,6.4f,0.0025f));
            StartCoroutine(show.Show(building,0,5.8f,6.4f,0.0025f));
            show1=false;

        }
        if (parsedata) {
            parsedata=false;
            StartCoroutine(parseData(prevdata));
        }
        if(selection){
            selection=false;
            StartCoroutine(startThings());
        }
        if(senddata1){
            senddata1=false;
            ZoomIn();
        }
        if(senddata2){
            senddata2=false;
            ZoomBack();
        }
        if(valuechanged){
            toSend =zoom.isZooming.ToString()+",";
            prevdata=toSend;
            //print (toSend);
            msg1 = System.Text.Encoding.ASCII.GetBytes(toSend);
            stream.Write (msg1, 0, msg1.Length);
            valuechanged=false;
        }
        if(valuechanged2){
            toSend =zoom.isZoomed.ToString()+":";
            prevdata=toSend;
            //print (toSend);
            msg1 = System.Text.Encoding.ASCII.GetBytes(toSend);
            stream.Write (msg1, 0, msg1.Length);
            valuechanged2=false;
        }
        if(endpointChanged){
            endpointChanged=false;
            zoom.Zoompoint.position=pos;
            zoom.Zoompoint.rotation=Quaternion.Euler(40.0f,275.0f,0.0f);
            zoom.rotateAroundObject.position=new Vector3(pos.x-0.498778f,pos.y-0.423f,pos.z-0.074f);
        }
    }
    void FPScounter(){
        frametime+=Time.deltaTime;
        q++;
        if(frametime>=1.0f){
            FPS=q;
            //print ("FPS:"+FPS);
            q=0;
            frametime=0.0f;
        }
    }
    void OnApplicationQuit(){
        server.Stop();
        mThread.Abort ();
    }
    void Update1()
    {   
        server=null;  
        try
        {
            // Set the TcpListener on port 13000.
            Int32 port = 3333;
            IPAddress localAddr = IPAddress.Parse("192.168.0.242");
           
            // TcpListener server = new TcpListener(port);
            server = new TcpListener(IPAddress.Any, port);
           
            // Start listening for client requests.
            server.Start();
           
            // Buffer for reading data
            Byte[] bytes = new Byte[256];
            String data = null;
           
            // Enter the listening loop.
            while(true)
            {
                Thread.Sleep(10);
               
                Debug.Log("Waiting for a connection... ");
               
                // Perform a blocking call to accept requests.
                // You could also user server.AcceptSocket() here.
                client = server.AcceptTcpClient();   
                if(client!=null){
                   
                    Debug.Log("Connected!");
                    //isConnection=true;
                    //client.Close();
                    //break;
                   
                }
                data = null;
               
                // Get a stream object for reading and writing
                stream = client.GetStream();
                StreamWriter swriter=new StreamWriter(stream);
                int i;
               
                // Loop to receive all the data sent by the client.
                while((i = stream.Read(bytes, 0, bytes.Length))!=0)
                {  
                    //msg1 = System.Text.Encoding.ASCII.GetBytes(prevdata);
                   
                    // Send back a response.
                    //stream.Write(msg1, 0, msg1.Length);
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    //Debug.Log("Received:"+ data+"data");
                   
                   
                    //Debug.Log("Sent:"+ data);
                    // Process the data sent by the client.
                    bool isTrue=false;
                    switch (data)
                    {
                    case "params":
                        //senddata=true;
                        string stairways1 = Stairways ();
                        string rooms1 = Rooms ();
                        toSend = floors.ToString () + "/" + stairways1 + "/" + rooms1;
                        //print (toSend);
                        prevdata=toSend;
                        msg1 = System.Text.Encoding.ASCII.GetBytes(toSend);
                        stream.Write (msg1, 0, msg1.Length);
                        //stream.Flush();
                        break;
                       
                    case "isZooming":
                        toSend =zoom.isZooming.ToString()+",";
                        //print (toSend);
                        prevdata=toSend;
                        msg1 = System.Text.Encoding.ASCII.GetBytes(toSend);
                        stream.Write (msg1, 0, msg1.Length);
                        //stream.Flush();
                        break;
                    case "isZoomed":
                        toSend =zoom.isZoomed.ToString()+":";
                        prevdata=toSend;
                        //print (toSend);
                        msg1 = System.Text.Encoding.ASCII.GetBytes(toSend);
                        stream.Write (msg1, 0, msg1.Length);
                        stream.Flush();
                        break;
                        break;
                    case "Zoompressed1,true":
                        senddata1=true;
                       
                        //stream.Flush();
                        break;
                    case "Zoompressed2,true":
                        senddata2=true;
                       
                        break;
                    case "Left,true":
                        //msg1 = System.Text.Encoding.ASCII.GetBytes(data);
                        //stream.Write (msg1, 0, msg1.Length);
                        zoom.rotatearound1=true;
                        break;
                    case "Left,Stop":
                        //msg1 = System.Text.Encoding.ASCII.GetBytes(data);
                        //stream.Write (msg1, 0, msg1.Length);
                        zoom.rotatearound1=false;
                        break;
                    case "Right,true":
                        //msg1 = System.Text.Encoding.ASCII.GetBytes(data);
                        //stream.Write (msg1, 0, msg1.Length);
                        zoom.rotatearound2=true;
                        break;
                    case "Right,Stop":
                        //msg1 = System.Text.Encoding.ASCII.GetBytes(data);
                        //stream.Write (msg1, 0, msg1.Length);
                        zoom.rotatearound2=false;
                        break;
                    case "Up,true":
                        //msg1 = System.Text.Encoding.ASCII.GetBytes(data);
                        //stream.Write (msg1, 0, msg1.Length);
                        zoom.rotatearound3=true;
                        break;
                    case "Up,Stop":
                        //msg1 = System.Text.Encoding.ASCII.GetBytes(data);
                        //stream.Write (msg1, 0, msg1.Length);
                        zoom.rotatearound3=false;
                        break;
                    case "Down,true":
                        //msg1 = System.Text.Encoding.ASCII.GetBytes(data);
                        //stream.Write (msg1, 0, msg1.Length);
                        zoom.rotatearound4=true;
                        break;
                    case "Down,Stop":
                        //msg1 = System.Text.Encoding.ASCII.GetBytes(data);
                        //stream.Write (msg1, 0, msg1.Length);
                        zoom.rotatearound4=false;
                        break;
                    case "ZoomIn,true":
                        //msg1 = System.Text.Encoding.ASCII.GetBytes(data);
                        //stream.Write (msg1, 0, msg1.Length);
                        zoom.distanceChange1=true;
                        break;
                    case "ZoomIn,Stop":
                        //msg1 = System.Text.Encoding.ASCII.GetBytes(data);
                        //stream.Write (msg1, 0, msg1.Length);
                        zoom.distanceChange1=false;
                        break;
                    case "ZoomOut,true":
                        //msg1 = System.Text.Encoding.ASCII.GetBytes(data);
                        //stream.Write (msg1, 0, msg1.Length);
                        zoom.distanceChange2=true;
                        break;
                    case "ZoomOut,Stop":
                        //msg1 = System.Text.Encoding.ASCII.GetBytes(data);
                        //stream.Write (msg1, 0, msg1.Length);
                        zoom.distanceChange2=false;
                        break;
                    case "Disconnect":
                        goto q;
                        client.Close();
                        break;
                    case "Show":
                        show1=true;
                        break;
                    case "Hide":
                        hide=true;

                        break;
                    default:
                        byte[] msg = System.Text.Encoding.ASCII.GetBytes(prevdata);
                       
                        // Send back a response.
                        stream.Write(msg, 0, msg.Length);
                        //isTrue=true;
                        //msg1 = System.Text.Encoding.ASCII.GetBytes(data);
                        //stream.Write (msg1, 0, msg1.Length);
                        //senddata=true;
                        //StartCoroutine(sendString1(stream));
                        //sendString1(stream);
                        break;
                    }
                    bool contains1=data.Contains("Left,Stop");
                    if(contains1)zoom.rotatearound1=false;
                    bool contains2=data.Contains("Right,Stop");
                    if(contains2)zoom.rotatearound2=false;
                    bool contains3=data.Contains("Up,Stop");
                    if(contains3)zoom.rotatearound3=false;
                    bool contains4=data.Contains("Down,Stop");
                    if(contains4)zoom.rotatearound4=false;
                    bool contains5=data.Contains("ZoomIn,Stop");
                    if(contains5)zoom.distanceChange1=false;
                    bool contains6=data.Contains("ZoomOut,Stop");
                    if(contains6)zoom.distanceChange2=false;
                    if(data.StartsWith("(")){
                        //data=subS(data,'(');
                        parsedata=true;
                        //selection=true;

                        byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
                        prevdata=data;
                        //Debug.Log("SentPrevdata:"+ prevdata);
                        // Send back a response.
                        stream.Write(msg, 0, msg.Length);
                    }
                    if(data==" "){
                        byte[] msg = System.Text.Encoding.ASCII.GetBytes(prevdata);
                        //Debug.Log("SentPrevdata:"+ prevdata);
                        // Send back a response.
                        stream.Write(msg, 0, msg.Length);
                    }
                    if(data.StartsWith("!")){
                        //Debug.Log(data);
                        Vector3 vector;
                        try{
                        vector=parseVector3(data);
                        pos=vector;
                        endpointChanged=true;
                        //print (vector.ToString());
                        }
                        catch(Exception e){
                        }
                    }
                    if(data.StartsWith("&")){
                        //Debug.Log(data);
                        float timeOfDay=0.0f;
                        try{
                        timeOfDay=float.Parse(data.Substring(1,data.Length-1));
                        sun.currentTime=timeOfDay;
                        }
                        catch(FormatException e){
                        }
                        //endpointChanged=true;
                        //print (timeOfDay.ToString());
                    }
                    if(data.StartsWith("#")){
                        //Debug.Log(data);
                        float cloudiness=0.0f;
                        try{
                            cloudiness=float.Parse(data.Substring(1,data.Length-1));
                            sun.cloudiness=cloudiness;
                        }
                        catch(FormatException e){
                        }
                        //endpointChanged=true;
                        //print (cloudiness.ToString());
                    }
                    //Debug.Log("Sent:"+ data); 
                }
            q:
                    show1=true;
                    // Shutdown and end connection
                    client.Close();
            }
        }
        catch(SocketException e)
        {
            Debug.Log("SocketException:"+ e);
        }
        finally
        {
            // Stop listening for new clients.
            server.Stop();
        }
       
        //yield return null;
       
       
    }
    Vector3 parseVector3(string toParse){
        Vector3 vector1;
        string[] splits0=toParse.Split (new char[]{'(',')'});
        string[] splits = splits0[1].Split (new char[]{','});
        //string[] splits2=splits[0].Split(new char[]{'!'});
        float x = float.Parse (splits[0]);
        float y = float.Parse (splits[1]);
        float z = float.Parse (splits[2]);
        vector1 = new Vector3 (x,y,z);
        return vector1;
    }
    List<GameObject> getFlats(){
        List<GameObject> flats1 = new List<GameObject> ();
        GameObject parent = GameObject.FindGameObjectWithTag ("Jsaari");
        Shader shader = Shader.Find("Transparent/Diffuse");
   
        Color color=new Color(1.0f,1.0f,1.0f,0.1f);
        //shader.material.color=color;
        for (int i = 0; i < parent.transform.childCount; i++)
        {

            GameObject obj = parent.transform.GetChild(i).gameObject;
       

            if(obj.name.StartsWith("_")){
                Renderer r=obj.GetComponent<Renderer>();
                Material material=r.sharedMaterial;
                r.sharedMaterial.shader=shader;
                r.sharedMaterial.color=color;
                /*material.SetColor("_EmissionColor",Color.white);
                material.SetFloat("_Mode", 2);
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                material.SetInt("_ZWrite", 0);
                material.DisableKeyword("_ALPHATEST_ON");
                material.EnableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = 3000;
                material.color=color;
                material.SetColor("_EmissionColor",Color.white);
                obj.GetComponent<Renderer>().sharedMaterial=material;*/
                obj.tag="Available";
                flats1.Add(obj);
            }
        }
        return flats1;
    }
    IEnumerator parseName(GameObject obj){
        string name = obj.name;
        List<String> nameparts = new List<string> ();
        string[] splits = name.Split (new char[]{'_'});
        bool stairway=false, rooms1=false, floor=false;
        foreach(string s in splits){
            //print (s);
            if(s!="")nameparts.Add(s);
            yield return null;
        }
        //print (nameparts.Count);
        int index = 0;
        for(int i=0;i<nameparts.Count;i++){
            switch (i){
            case 0:

                index=0;
                //print (nameparts[i]);
                switch(nameparts[i]){
                case "a":
                    index=0;
                    yield return null;
                    break;
                case "b":
                    index=1;
                    yield return null;
                    break;
                case "c":
                    index=2;
                    yield return null;
                    break;
                case "d":
                    index=3;
                    yield return null;
                    break;
                case "e":
                    index=4;
                    yield return null;
                    break;
                case "f":
                    index=5;
                    yield return null;
                    break;
                case "g":
                    index=6;
                    yield return null;
                    break;
                case "h":
                    index=7;
                    yield return null;
                    break;
                case "i":
                    index=8;
                    yield return null;
                    break;
                case "j":
                    index=9;
                    yield return null;
                break;
                case "k":
                    index=10;
                    yield return null;
                    break;
                case "l":
                    index=11;
                    yield return null;
                    break;
                case "m":
                        index=12;
                    yield return null;
                    break;
                default:
                    yield return null;
                    break;
                }
                //print ("Index:"+index);
                //print ("Stairwaysbool:"+stairwaysBool[index]);
                if(stairwaysBool[index])stairway=true;
                yield return null;
                break;
            case 1:
                print ("Id");
                yield return null;
                break;
            case 2:

                index=Convert.ToInt32(nameparts[i]);
                //print (index);
                //print (roomsBool[index-1]);
                if(roomsBool[index-1])rooms1=true;
                yield return null;
                break;
            case 3:
                index=Convert.ToInt32(nameparts[i]);
                //print (index);
                //print (floorsBool[index-1]);
                if(floorsBool[index-1])floor=true;
                yield return null;
                break;
            default:
                print ("Error");
                yield return null;
                break;
            }
            yield return null;
        }
        //print (stairway.ToString() + rooms1.ToString()+floor.ToString() +obj.CompareTag("Available").ToString());
        if (stairway && rooms1 && floor && obj.CompareTag ("Available")) {
            //print ("show");
            Color color1 = new Color (0.0f, 0.5f, 0.8f, 0.6f);
            obj.GetComponent<Renderer> ().sharedMaterial.color = color1;
            //obj.GetComponent<Renderer> ().sharedMaterial.SetColor("_EmissionColor",Color.cyan);
            yield return null;
        }
        else {
            //print ("hide");
            Color color1 = new Color (1.0f, 1.0f, 1.0f, 0.1f);
            obj.GetComponent<Renderer> ().sharedMaterial.color = color1;
            //obj.GetComponent<Renderer> ().sharedMaterial.SetColor("_EmissionColor",Color.white);
            yield return null;
        }
        yield return null;
    }
    IEnumerator parseData(string data){
        string s1 = subS (data,'(');

        //print (s1);
        AssignList (ref stairwaysBool,s1);
        string s2 = subS (data,'_');
        //print (s2);
        AssignList (ref floorsBool, s2);
   
        string s3 = subS (data,'%');
        //print (s3);
        AssignList (ref roomsBool,s3);
        string s4 = subS (data,'

);
available = Convert.ToBoolean (s4);
selection = true;
yield return null;
}
void AssignList(ref List sBool, string s){

    String[] s31 = s.Split (new char[]{','});
    for(int i=0;i<sBool.Count;i++){
        for(int j=0;j<s31.Length-1;j++){
           
            int a=Convert.ToInt32(s31[j]);
            //print (a);
            if(a==i){sBool[i]=true;
                break;}
            sBool[i]=false;
        }
    }
}
string subS(string s, char c){
    string s1 = "";
    int first = s.IndexOf (c);
    if (first != null) {
        int last=s.LastIndexOf(c);
        int length=last-first-1;
        if(length>0){
            s1=s.Substring(first+1,length);
        }
    }
    return s1;
}
public void ZoomIn(){
    if(!zoom.isPointsSet){
        zoom.startpoint = zoom.transform.position;
        zoom.endpoint = zoom.Zoompoint.position;
        zoom.startrotation = zoom.transform.rotation;
        zoom.endrotation = zoom.Zoompoint.rotation;
        zoom.isPointsSet=true;
        zoom.isZoomed=false;
    }
    zoom.isZooming = true;
    toSend =zoom.isZooming.ToString()+",";
    //print (toSend);
    msg1 = System.Text.Encoding.ASCII.GetBytes(toSend);
    stream.Write (msg1, 0, msg1.Length);
    zoom.Zoompressed = true;
}
public void ZoomBack(){
    if(!zoom.isPointsSet){
        zoom.endpoint=zoom.transform.position;
        zoom.endrotation=zoom.transform.rotation;
        zoom.isPointsSet=true;
        zoom.isZoomed=true;
    }
    zoom.isZooming = true;
    toSend =zoom.isZooming.ToString()+",";
    //print (toSend);
    msg1 = System.Text.Encoding.ASCII.GetBytes(toSend);
    stream.Write (msg1, 0, msg1.Length);
    zoom.Zoompressed=true;
}
List<bool> InitializeFloorsBool(){
    List<bool> list = new List<bool> ();
    for(int i=0;i<floors;i++){
        list.Add(true);
    }
    return list;
}
List<bool> InitializeRoomsBool(){
    List<bool> list = new List<bool> ();
    for(int i=0;i<rooms.Count;i++){
        list.Add(true);
    }
    return list;
}
List<bool> InitializeStairwaysBool(){
    List<bool> list = new List<bool> ();
    for(int i=0;i<stairways.Count;i++){
        list.Add(true);
    }
    return list;
}
IEnumerator sendString1(NetworkStream stream1){
    string stairways1 = Stairways ();
    string rooms1 = Rooms ();
    string toSend = floors.ToString () + "/" + stairways1 + "/" + rooms1;
    //print (toSend);
    byte[] msg1 = System.Text.Encoding.ASCII.GetBytes(toSend);
    stream1.Write (msg1, 0, msg1.Length);
    yield return null;
}
string Stairways(){
    string s = "";
    for(int i=0; i<stairways.Count;i++){
        s+=stairways[i]+",";
    }
    return s;
}
string Rooms(){
    string s = "";
    for(int i=0; i<rooms.Count;i++){
        s+=rooms[i].ToString()+",";
    }
    return s;
}

}

1 Like

For readability or maintenance (this also applied for 1 person project), you could split your code into more classes, each managing a specific aspect of your game.
I see at least the following classes reading your code:

  • NetworkManager
  • CommandParser
  • CameraManager
  • Map

True (but what do you mean, Map?). My excuse for writing such a code is that it was done in a hurry. It was just a demo, not final project.

I see that there are objects such as floors, stairways, rooms. I guess these objects can be managed by a Map class, or Environment class.

I know what it is to write codes in a rush ;). These are often the codes I have a hard time with when I come back at it later and I don’t remember what the hell I was thinking about while writing that.

It’s a good habit to have you code organized from the start, It would safe you from trouble later on. Also, if it is organized from the start, that’s something you don’t have to do anymore, which means more time to do something else!

Environment class would suit better in that case.

I’m also often confused when I read own old code trying to figure out the logic behind it.

I think I’ve learned my lesson, from now on I’ll organize the code better from the start.

I hope DPH can still find the information needed from that relatively unorganized code.

I’m kinda new to Unity/C# so yeah it is a bit overwhelming! My goal is to make a TCP server to read anything (string, float etc.) and to use them to move my humanoid avatar (big school project).

In case you’re really that new, that could be some heavy stuff to start with.

Have you tried Unity’s build-in networking? It’s UDP based as far as i can recall but also offers configurations to achieve reliability, in order delivery etc.
Unless you don’t need to send it via a secure stream such as (SSL/TLS based) you should be fine to go with it.

If you still aim for your own system you’re probably better off starting with a clean base, i know MSDN examples are not the best but it’s what i had started with to get a basic idea of everything and which is still the base for my networking system’s prototype.
You can find synchronous client-server and asynchronous client-server samples here. I personally had to change them alot as the example is quite useless for gaming, but you’ll get the idea.

Thank you very much for your code, @Samuli_Kuusisto . It’s been really helpful.

Thank you very much for your code @