public void OnListeningServer() { try { lock (udpClientLock) { udpClient = new UdpClient(); udpClient.ExclusiveAddressUse = false; udpClient.Client.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); IPEndPoint localEp = new IPEndPoint(IPAddress.Any, clientPort); udpClient.Client.Bind(localEp); udpSocketState = UDPSocketState.CONNECTED; udpClient.BeginReceive(new AsyncCallback(OnWaitPacketsCallback), null); } } catch { throw; } }
/// <summary> /// Emit the pack or message to server. /// </summary> /// <param name="callbackID">Callback ID.</param> /// <param name="_pack">message</param> public void Emit(string callbackID, string _pack) { try { if (udpSocketState == UDPSocketState.CONNECTED) { lock (udpClientLock) { if (udpClient == null) { udpClient = new UdpClient(); udpClient.ExclusiveAddressUse = false; udpClient.Client.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); IPEndPoint localEp = new IPEndPoint(IPAddress.Any, clientPort); udpClient.Client.Bind(localEp); } udpSocketState = UDPSocketState.SENDING_MESSAGE; string new_pack = callbackID + ":" + _pack; byte[] data = Encoding.UTF8.GetBytes(new_pack.ToString()); //convert to bytes #if UNITY_ANDROID && !UNITY_EDITOR var endPoint = new IPEndPoint(IPAddress.Parse(CaptiveReality.Jni.Util.StaticCall <string>("GetServerIP", "Invalid Response From JNI", "com.rio3dstudios.basicwifilocalmultiplayerplugin.IPManager")), serverPort); #else var endPoint = new IPEndPoint(IPAddress.Parse(GetServerIP()), serverPort); #endif try { udpClient.EnableBroadcast = true; udpClient.Send(data, data.Length, endPoint); } catch (Exception e) { Console.WriteLine(e.ToString()); } udpSocketState = UDPSocketState.CONNECTED; } } } catch (Exception e) { listenerInput = e.InnerException.Message; } }
public void Awake() { handlers = new Dictionary <string, List <Action <SocketUDPEvent> > >(); eventQueueLock = new object(); eventQueue = new Queue <SocketUDPEvent>(); udpSocketState = UDPSocketState.DISCONNECTED; }
public void Awake() { Debug.Log("UDPClientComponent_Awake()"); handlers = new Dictionary <string, List <Action <SocketUDPEvent> > >(); eventQueueLock = new object(); eventQueue = new Queue <SocketUDPEvent>(); udpSocketState = UDPSocketState.DISCONNECTED; }