public void Send() { UpdateWatch(); UdpClient tmpClient = new UdpClient(); NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in nics) { if (adapter.Name == "wlan0") { IPv4InterfaceProperties p = adapter.GetIPProperties().GetIPv4Properties(); tmpClient.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, (int)IPAddress.HostToNetworkOrder(p.Index)); //Console.WriteLine ("assigned " + adapter.Name + " as multicast address : " + (int)IPAddress.HostToNetworkOrder(p.Index)); } } IPAddress multicastaddress = IPAddress.Parse(MULTICAST_IP); tmpClient.JoinMulticastGroup(multicastaddress); IPEndPoint multicastPoint = new IPEndPoint(multicastaddress, PORT_NUMBER); // Can send on broadcast // Yep, you can serialize a whole list ! string json = new JsonService().Serialize(watches_); byte[] buffer = Encoding.ASCII.GetBytes(json); tmpClient.Send(buffer, buffer.Length, multicastPoint); tmpClient.Close(); }
private void Receive(IAsyncResult asyncResult) { IPEndPoint ip = new IPEndPoint(IPAddress.Any, PORT_NUMBER); // Can receive from everyone byte[] bytes = udpClient_.EndReceive(asyncResult, ref ip); string json = Encoding.ASCII.GetString(bytes); // TODO: tests on received data ! var watch = JsonService.Deserialize(json); // TODO: do stuff with received data StartListening(); }
public void Send(WatchModel watchModel) { UdpClient tmpClient = new UdpClient(); IPEndPoint broadcastIp = new IPEndPoint(IPAddress.Broadcast, PORT_NUMBER); // Can send on broadcast watchModel.Counter += 1; // Update "TimeStamp" to check for changes on remote watches ! string json = JsonService.Serialize(watchModel); byte[] buffer = Encoding.ASCII.GetBytes(json); tmpClient.Send(buffer, buffer.Length, broadcastIp); tmpClient.Close(); }
public void Receive(IAsyncResult asyncResult) { IPEndPoint ip = new IPEndPoint(IPAddress.Any, PORT_NUMBER); // Can receive from everyone byte[] bytes = udpClient_.EndReceive(asyncResult, ref ip); if (ip.Address.ToString() != LocalIP) { Console.WriteLine("listing from : " + ip.Address.ToString()); string json = Encoding.ASCII.GetString(bytes); // TODO: tests on received data ! var watches = new JsonService().Deserialize(json); UpdateWatchList(watches); } // (Re)Start listening to Receive data again PrepareReceive(); }