Search Unity

unity udp script crashes the editor/game

Discussion in 'Multiplayer' started by aclee, Apr 27, 2015.

  1. aclee

    aclee

    Joined:
    Oct 5, 2013
    Posts:
    10
    in my simple udp server finder script when i select client it makes unity crash and i can't find why;

    the comment code is what makes the crash; when i uncomment the while loop in StartClient function it crashes so at first i though it was the while loop that made it crash so i commented it and wrote it again in the update function so when i say that clientstarted is true in startclient() the update function will run the code but that makes it crash also, so i can't realy guess what makes it crash (when i mean crash i mean in windows make the editor not respond and eventually close itself and on android just close the app).

    and if it helps here is the log file http://pastebin.com/JZ1kY62a

    Code (JavaScript):
    1. import System.Net.Sockets;
    2. private var udp_server:UdpClient;
    3. private var udp_client:UdpClient;
    4. private var udp_port:int = 18000;
    5. private var udp_broadcast_ip:IPAddress = IPAddress.Parse ("224.0.0.224");
    6. private var udp_received_message:String;
    7. private var udp_endpoint:IPEndPoint;
    8. private var selected:boolean = false;
    9. private var clientStarted:boolean = false;
    10. function StartServer(){
    11.      udp_server = new UdpClient(udp_port, AddressFamily.InterNetwork);
    12.    
    13.      udp_server.JoinMulticastGroup(udp_broadcast_ip);
    14.      udp_endpoint = new IPEndPoint(udp_broadcast_ip, udp_port);
    15.      InvokeRepeating("StartBroadcastUDP", 0.0,0.3);
    16. }
    17. function StartClient(){
    18.      udp_client = new UdpClient();
    19.      udp_endpoint = new IPEndPoint(IPAddress.Any, udp_port);
    20.      udp_client.Client.Bind(udp_endpoint);
    21.      udp_client.JoinMulticastGroup(udp_broadcast_ip);
    22.      /*
    23.      while(true){
    24.          yield;
    25.          var udp_received_message_byte:byte[] = udp_client.Receive(udp_endpoint);
    26.          udp_received_message = Encoding.Unicode.GetString(udp_received_message_byte);
    27.          print("Received Message: " + udp_received_message);
    28.      }*/
    29.      clientStarted = true;
    30. }
    31. function StartBroadcastUDP(){
    32.      var udp_broadcast_message = Encoding.Unicode.GetBytes("GAME SERVER");
    33.      if(udp_broadcast_message != ""){
    34.          udp_server.Send(udp_broadcast_message, udp_broadcast_message.Length);
    35.      }
    36. }
    37. function OnGUI(){
    38.      if(!selected){
    39.          if(GUI.Button(Rect(0, 0, 100, 100), "Server")){
    40.              StartServer();
    41.              selected = true;
    42.          }else if(GUI.Button(Rect(100, 0, 100, 100), "Client")){
    43.              StartClient();
    44.              selected = true;
    45.          }
    46.      }
    47. }
    48. function Update(){
    49.      /*
    50.      if(clientStarted){
    51.          var udp_received_message_byte:byte[] = udp_client.Receive(udp_endpoint);
    52.          udp_received_message = Encoding.Unicode.GetString(udp_received_message_byte);
    53.          print("Received Message: " + udp_received_message);
    54.      }*/
    55. }
     
    Last edited: Apr 27, 2015