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). Code (JavaScript): import System.Net.Sockets; private var udp_server:UdpClient; private var udp_client:UdpClient; private var udp_port:int = 18000; private var udp_broadcast_ip:IPAddress = IPAddress.Parse ("224.0.0.224"); private var udp_received_message:String; private var udp_endpoint:IPEndPoint; private var selected:boolean = false; private var clientStarted:boolean = false; function StartServer(){ udp_server = new UdpClient(udp_port, AddressFamily.InterNetwork); udp_server.JoinMulticastGroup(udp_broadcast_ip); udp_endpoint = new IPEndPoint(udp_broadcast_ip, udp_port); InvokeRepeating("StartBroadcastUDP", 0.0,0.3); } function StartClient(){ udp_client = new UdpClient(); udp_endpoint = new IPEndPoint(IPAddress.Any, udp_port); udp_client.Client.Bind(udp_endpoint); udp_client.JoinMulticastGroup(udp_broadcast_ip); /* while(true){ yield; var udp_received_message_byte:byte[] = udp_client.Receive(udp_endpoint); udp_received_message = Encoding.Unicode.GetString(udp_received_message_byte); print("Received Message: " + udp_received_message); }*/ clientStarted = true; } function StartBroadcastUDP(){ var udp_broadcast_message = Encoding.Unicode.GetBytes("GAME SERVER"); if(udp_broadcast_message != ""){ udp_server.Send(udp_broadcast_message, udp_broadcast_message.Length); } } function OnGUI(){ if(!selected){ if(GUI.Button(Rect(0, 0, 100, 100), "Server")){ StartServer(); selected = true; }else if(GUI.Button(Rect(100, 0, 100, 100), "Client")){ StartClient(); selected = true; } } } function Update(){ if(clientStarted){ var udp_received_message_byte:byte[] = udp_client.Receive(udp_endpoint); udp_received_message = Encoding.Unicode.GetString(udp_received_message_byte); print("Received Message: " + udp_received_message); } } and if it helps here is the log file http://pastebin.com/JZ1kY62a
Seems that you never exit the while(true) loop, it basically means it will run that loop infinitely and never finish that update cycle (frame won't be rendered). You need some condition to exit the loop.
i tried changing the loop to this but it still crashed Code (JavaScript): while(true){ yield; var udp_received_message_byte:byte[] = udp_client.Receive(udp_endpoint); udp_received_message = Encoding.Unicode.GetString(udp_received_message_byte); print("Received Message: " + udp_received_message); if(udp_received_message != null){ break; } }
I would check to see if udp_received_message != null before you access it. Also, do you really need to check this every frame? Also, do you ever set it to false? Now it will run forever, even if it worked.
Not really knowing much of how networking and receiving things actually works in Unity this sounds like instead of while loop you might want to check for received messages in update loop. Then the place to do that would be in Update function. But again, don't really know about networking side of things so recommended approach might be different here.
it basically stops being null the second it gets a udp message but after some testing i.e erasing the entire loop and just writing var udp_received_message_byte:byte[]= udp_client.Receive(udp_endpoint); still crashes unity i did try it in the update function when writing if(clientStarted){ var udp_received_message_byte:byte[] = udp_client.Receive(udp_endpoint); udp_received_message = Encoding.Unicode.GetString(udp_received_message_byte); print("Received Message: " + udp_received_message); } in the update function and instead of the while loop just setting clientstarted to true, but this also crashed unity. could it be that unity just doesn't handle udp well ? (or am i just missing something obvious ?) is there any other way to do lan server lookup without udp?