Search Unity

.net sockets makes my unity3d not responding

Discussion in 'Scripting' started by NikoBusiness, Sep 17, 2014.

  1. NikoBusiness

    NikoBusiness

    Joined:
    May 6, 2013
    Posts:
    289
    Guys what's wrong?? I add this code to my main camera and when I start the game unity freezes
    Code (CSharp):
    1.  
    2. 0
    3. guys i add this csharp code to my Main Camera GameObject and when i start the game unity3d stops responding.. whats wrong?
    4.  
    5. using UnityEngine;
    6. using System;
    7. using System.Net;
    8. using System.Collections;
    9. using System.Data;
    10. using MySql.Data.MySqlClient;
    11. using System.Net.Sockets;
    12.  
    13. public class NewBehaviourScript : MonoBehaviour {
    14.  
    15.     public string informationLabelText;
    16.     TcpListener server = null;
    17.     // Use this for initialization
    18.     void Start () {
    19.  
    20.         try
    21.         {
    22.             Int32 port = 13000;
    23.             IPAddress localAddr = IPAddress.Parse("127.0.0.1");
    24.             server = new TcpListener(localAddr,port);
    25.             //start listening for clients
    26.             server.Start();
    27.  
    28.             Byte[] bytes =  new Byte[256];
    29.             string data = null;
    30.  
    31.             while(true)
    32.             {
    33.                 informationLabelText = "Waiting for a Connection";
    34.  
    35.                 //Perform a block call to accept requests
    36.                 //you can also use server.AcceptSocket(); here
    37.                 TcpClient client = server.AcceptTcpClient();
    38.                 informationLabelText = "Connected";
    39.  
    40.                 data = null;
    41.  
    42.                 //get a stream object for reading and writing
    43.                 NetworkStream stream = client.GetStream();
    44.  
    45.                 int i;
    46.  
    47.                 //loop to receive all the data sent by the client
    48.                 while( (i = stream.Read(bytes, 0, bytes.Length) ) !=0 )
    49.                 {
    50.                     data = System.Text.Encoding.ASCII.GetString(bytes,0,i);
    51.  
    52.                     //TODO SEE IF USERNAME PASSWORD EXISTS
    53.                     data = "accepted";
    54.                     byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
    55.                     stream.Write (msg,0,msg.Length);
    56.                     informationLabelText = "Information Sent";
    57.                 }
    58.                 //shut down & end connection
    59.                 client.Close();
    60.             }
    61.  
    62.         }
    63.         catch(SocketException e)
    64.         {
    65.             informationLabelText = e.ToString();
    66.         }
    67.         finally
    68.         {  
    69.             //stop listening for clients
    70.             server.Stop();
    71.         }
    72.         /**string connectionSTR = "SERVER=localhost;DATABASE=2vilages;UID=admin;PASSWORD=15263456987;";
    73.         MySqlConnection connection = new MySqlConnection(connectionSTR);
    74.         try
    75.         {
    76.             Debug.Log("Connecting to mysql");
    77.             connection.Open();
    78.             string sql = "SELECT COUNT(*) FROM users";
    79.             MySqlCommand cmd = new MySqlCommand(sql,connection);
    80.             object result = cmd.ExecuteScalar();
    81.             if(result != null)
    82.             {
    83.                 int r = Convert.ToInt32(result);
    84.                 Debug.Log ("Registered Users: " + r);
    85.             }
    86.         }
    87.         catch(Exception ex)
    88.         {
    89.             Debug.Log( ex.ToString() );
    90.  
    91.         }
    92.         connection.Close ();
    93.         Debug.Log ("Done");
    94. **/
    95.  
    96.  
    97.     }
    98.  
    99.     // Update is called once per frame
    100.     void Update () {
    101.  
    102.  
    103.     }
    104. }
     
  2. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585