public void SendMessageSync() { var socket = new Socket(SocketType.Dgram, ProtocolType.Udp); socket.Bind(new IPEndPoint(IPAddress.Loopback, 12346)); using (var peer = new FeatherUdpPeer <BinaryMessage>()) { peer.SendTo(new IPEndPoint(IPAddress.Loopback, 12346), TestMessage1); peer.SendTo(new IPEndPoint(IPAddress.Loopback, 12346), TestMessage2); } byte[] buf = new byte[100]; Int32 len; len = socket.Receive(buf); Assert.Equal(TestPayload1.Length, len); Assert.Equal(TestPayload1, buf.Take(3)); len = socket.Receive(buf); Assert.Equal(TestPayload2.Length, len); Assert.Equal(TestPayload2, buf.Take(3)); }
public async void Dispose() { var peer = new FeatherUdpPeer <BinaryMessage>(); Assert.False(peer.IsDisposed); peer.Bind(12347); Assert.False(peer.IsDisposed); peer.Dispose(); Assert.True(peer.IsDisposed); Assert.Throws <ObjectDisposedException>(() => { peer.SendTo(new IPEndPoint(IPAddress.Loopback, 12346), TestMessage1); }); await Assert.ThrowsAsync <ObjectDisposedException>(async() => { await peer.SendToAsync(new IPEndPoint(IPAddress.Loopback, 12346), TestMessage1); }); }
static void Main(string[] args) { Console.WriteLine("What is your name?"); var myName = Console.ReadLine(); using (var peer = new FeatherUdpPeer <GenericMessage>()) { // Watch for messages to arrive peer.OnMessageReceived += (endpoint, message) => { var name = message.ReadString(); var body = message.ReadString(); Console.WriteLine($"{name}> {body}"); }; // Bind socket to receive messages peer.Bind(12345); // Loop sending messages Console.WriteLine("Type message to send."); while (true) { var body = Console.ReadLine(); if (body == string.Empty) { break; } // Create message var message = new GenericMessage(); message.WriteString(myName); message.WriteString(body); // Broadcast message on the local network (similarly, we could send to a specific address on the internet instead) peer.SendTo(new IPEndPoint(IPAddress.Broadcast, 12345), message); } } }