networking - unity udp not receiving -
i'm trying make networking script find game servers on lan running trouble; import system.net.sockets;
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 selected:boolean = false; private var udp_received_message:string; function startserver(){ udp_server = new udpclient(udp_port, addressfamily.internetwork); var udp_endpoint:ipendpoint = new ipendpoint(udp_broadcast_ip, udp_port); udp_server.connect (udp_endpoint); invokerepeating("startbroadcastudp", 0.0,0.3); } function startclient(){ if(udp_client == null){ udp_client = new udpclient(udp_port); udp_client.beginreceive(new asynccallback(startreceiveudp), null); debug.log("searching udp"); } } function startbroadcastudp(){ var udp_broadcast_message:string = "game server"; if(udp_broadcast_message != ""){ udp_server.send(encoding.ascii.getbytes (udp_broadcast_message), udp_broadcast_message.length); } } function startreceiveudp(result:iasyncresult){ debug.log("searching udp"); var udp_endpoint:ipendpoint = new ipendpoint(ipaddress.any, udp_port); var udp_received_message_byte:byte[]; if(udp_client != null){ udp_received_message_byte = udp_client.endreceive(result, udp_endpoint); }else{ return; } udp_client.beginreceive(new asynccallback(startreceiveudp), null); udp_received_message = encoding.ascii.getstring(udp_received_message_byte); debug.log("searching udp"); } function update(){ if(udp_received_message != null){ debug.log("received message: " + udp_received_message); } } function ongui(){ if(!selected){ if(gui.button(rect(0, 0, 50, 50), "server")){ startserver(); selected = true; }else if(gui.button(rect(50, 0, 50, 50), "client")){ startclient(); selected = true; } } }
it seams when start receiving udp (beginreceive in startclient) plan doesn't id. make sure case added few debug.logs , still got nothing
unfortunately async calls somehow problematic in unity. can use blocking calls in threads or nonblocking calls in main thread (in update methods).
Comments
Post a Comment