unity3d free .net sockets Unity3d not responding

guys i add this csharp code to my Main Camera GameObject and when i start the game unity3d stops responding… whats wrong?

using UnityEngine;
using System;
using System.Net;
using System.Collections;
using System.Data;
using MySql.Data.MySqlClient;
using System.Net.Sockets;

public class NewBehaviourScript : MonoBehaviour {

	public string informationLabelText;
	TcpListener server = null;
	// Use this for initialization
	void Start () {

		try
		{
			Int32 port = 13000;
			IPAddress localAddr = IPAddress.Parse("127.0.0.1");
			server = new TcpListener(localAddr,port);
			//start listening for clients
			server.Start();
			
			Byte[] bytes =  new Byte[256];
			string data = null;
			
			while(true)
			{
				informationLabelText = "Waiting for a Connection";
				
				//Perform a block call to accept requests
				//you can also use server.AcceptSocket(); here
				TcpClient client = server.AcceptTcpClient();
				informationLabelText = "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 )
				{
					data = System.Text.Encoding.ASCII.GetString(bytes,0,i);
					
					//TODO SEE IF USERNAME PASSWORD EXISTS
					data = "accepted";
					byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
					stream.Write (msg,0,msg.Length);
					informationLabelText = "Information Sent";
				}
				//shut down & end connection
				client.Close();
			}
			
		}
		catch(SocketException e)
		{
			informationLabelText = e.ToString();
		}
		finally
		{	
			//stop listening for clients
			server.Stop();
		}


	}
	
	// Update is called once per frame
	void Update () {

	
	}
}

Couple of things you might want to consider:

  1. I really recommend removing user names and passwords from source files before posting them on the internet!
  2. The snippet you posted seems like it is intended to run on it’s own thread - on line 27 you declare an infinite loop - while(true), and you never break out of it, which is what freezes your game.

You could either try moving the block inside the while statement to a coroutine, and add a yield wait-for-end-of-frame (not sure the exact class name for this one, look it up ) at the end of the loop. Not sure how well it will behave, as I seem to recall reading some people positing issues stating sockets in coroutines hinder perfromance.

Another option is to use a separate thread for your server. Threading on it’s own is not a simple topic, and especially when trying to use threads in Unity. If you lean towards this solution, I highly recommend you spend a good amount of time reading into the whole subject before proceeding.

Lastly, the best solution imho would be to look at existing solutions / plugins and work from there. There are several networking plugins for Unity, all of which are probably better documented and clearer when it comes to Unity integration than general C# sockets tutorials.