private void _setup() { Done = true; _endPoint = new IPEndPoint(IPAddress.Any, Port); _udpSocket = new UdpSocket(); _udpSocket.Bind(_endPoint); }
public void Start() { buffer = new SocketBuffer(1556); socket = factory.Udp(); context.Queue.Add(() => { socket.Bind(); socket.Receive(buffer, OnReceived); }); }
/// <summary> /// Starts listening for packets from LFS. /// </summary> /// <param name="host">The host to listen to.</param> /// <param name="port">The port to listen on.</param> public void Connect(string host, int port) { ThrowIfDisposed(); udpSocket.Bind(host, port); if (timeoutTimer != null) { timeoutTimer.Start(); } }
//------------------------------------------------------------ #region Socket管理 private IUdpSocket CreateSocket(AddressFamily family, string host, int port) { UdpSocket socket = null; if (family == AddressFamily.InterNetwork) { socket = new UdpSocket(family, EnableReceiveBlock); socket.Bind(UdpSocket.AnyPort); } else { socket = new UdpSocket(family, EnableReceiveBlock); socket.Bind(UdpSocket.AnyPort); } socket.SetLocalSocket(m_IsLanOB, true); return(socket); }
public void CanTransferDataUsingUdpSockets() { using (CompletionThread worker = new CompletionThread()) { worker.Start(); using (ManualResetEvent sync = new ManualResetEvent(false)) { IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 44556); byte[] data = Encoding.ASCII.GetBytes("abc"); SocketBuffer buffer = new SocketBuffer(data); SocketFactory factory = new SocketFactory(worker); using (UdpSocket sender = factory.Udp()) using (UdpSocket receiver = factory.Udp()) { sender.Bind(); receiver.Bind(endpoint.Port); receiver.Receive(new SocketBuffer(10), received => { Assert.That(received.Status, Is.EqualTo(SocketStatus.OK)); Assert.That(received.Count, Is.EqualTo(3)); Assert.That(received.Buffer.Data.Take(3), Is.EqualTo(data)); sync.Set(); }); sender.Send(endpoint, buffer, sent => { Assert.That(sent.Status, Is.EqualTo(SocketStatus.OK)); Assert.That(sent.Count, Is.EqualTo(3)); }); } Assert.That(sync.WaitOne(200), Is.True); } } }
public void SendReceiveTest() { const string serverMessage = "HelloFromServer"; const string clientMessage = "ResponseFromClient"; IPEndPoint ep = new IPEndPoint(IPAddress.Any, 666); IPEndPoint serverEp = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 666); UdpSocket client = new UdpSocket(); UdpSocket server = new UdpSocket(); server.Bind(666); Assert.AreEqual(SocketStatus.Done, client.Connect(serverEp)); NetPacket packet = new NetPacket(); NetPacket clientPacket = new NetPacket(); packet.WriteString(serverMessage); // Send message to client. Assert.AreEqual(SocketStatus.Done, server.Send(packet, client.LocalEndpoint)); // Read message from server. Assert.AreEqual(SocketStatus.Done, client.Receive(clientPacket, ref ep)); Assert.AreEqual(serverMessage, clientPacket.ReadString()); // Send message back to server. clientPacket.Clear(SerializationMode.Writing); clientPacket.WriteString(clientMessage); Assert.AreEqual(SocketStatus.Done, client.Send(clientPacket)); // Read message from client. Assert.AreEqual(SocketStatus.Done, server.Receive(packet, ref ep)); Assert.AreEqual(clientMessage, packet.ReadString()); client.Dispose(); server.Dispose(); packet.Dispose(); clientPacket.Dispose(); }
public Client(IHub hub, IConfiguration configuration, ILogger <Server> logger, IHostApplicationLifetime appLifetime) { _hub = hub; _configuration = configuration; _logger = logger; CancelSource = new CancellationTokenSource(); QueriedVirtualAddresses = new HashSet <IPAddress>(); Tunnels = new Dictionary <IPAddress, IPEndPoint>(); Tun = new Tun("alicetun"); var serverAddress = IPAddress.Parse(_configuration["Client:ServerAddress"]); var serverPort = int.Parse(_configuration["Client:ServerPort"]); ServerEndPoint = new IPEndPoint(serverAddress, serverPort); var listenAddress = IPAddress.Parse(_configuration["Client:ListenAddress"]); var listenPort = int.Parse(_configuration["Client:ListenPort"]); ListenEndPoint = new IPEndPoint(listenAddress, listenPort); TunnelReceiveQueue = new Queue <TunFrame>(); TunnelSendQueue = new Queue <TunFrame>(); UdpSocket = new UdpSocket(); UdpSocket.Bind(ListenEndPoint); }
private static void UDPListenerThread(IPv6Address ipAddress, ushort port) { UdpSocket receiver = new UdpSocket(); receiver.Bind(ipAddress, port); IPv6EndPoint remoteIp = null; isUdpListenerRunning = true; while (isUdpListenerRunning) { if (receiver.Poll(-1, SelectMode.SelectRead)) { byte[] data = receiver.Receive(ref remoteIp); string message = Encoding.ASCII.GetString(data); Console.WriteLine("\n"); Console.WriteLine("{0} bytes from {1} {2} {3}", message.Length, remoteIp.Address, remoteIp.Port, message); Console.WriteLine(">"); } } receiver.Close(); receiver = null; }
/// <summary> /// Constructor that binds this object instance to an IPEndPoint. If you need to change /// IPEndPoint dynamically, Dispose and recreate a new object. /// </summary> /// <param name="endPoint">IPEndPoint where we should be listening for IP Multicast packets.</param> /// <param name="TimeoutMilliseconds">Milliseconds before lack of a packet == a Network Timeout</param> /// <example> /// ... /// UdpReceiver mcListener = new UdpReceiver(endPoint1); /// mcListener.Receive(packetBuffer); /// mcListener.Dispose(); /// /// UdpReceiver mcListener = new UdpReceiver(endPoint2); /// mcListener.Receive(packetBuffer); /// mcListener.Dispose(); /// /// mcListener = null; /// ... /// </example> public UdpReceiver(IPEndPoint endPoint, int timeoutMilliseconds) { this.MulticastEP = endPoint; this.disposed = false; this.fSocket = null; externalInterface = Utility.GetLocalRoutingInterface(endPoint.Address, endPoint.Port); //IPAddress ipa = IPAddress.Parse("192.0.0.11"); //int ad = (int)ipa.Address; //s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, ad); // Create the socket this.fSocket = new UdpSocket(endPoint.AddressFamily); if (Utility.IsMulticast(endPoint.Address)) { // Allow multiple binds to this socket, as it will still function properly // (this is only the case if it is a multicast socket. Unicast sockets fail to // receive all data on all sockets) //fSocket.ExclusiveAddressUse = false; fSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); } // Bind to the socket before joining a multicast group IPEndPoint iep = new IPEndPoint(IPAddress.Any, endPoint.Port); EndPoint localEndpoint = (EndPoint)iep; fSocket.Bind(localEndpoint); try { // Join the multicast group // This allows the kernel to inform routers that packets meant // for this group should come here. if (Utility.IsMulticast(endPoint.Address)) { if (endPoint.AddressFamily == AddressFamily.InterNetworkV6) { // Join the IPv6 Multicast group fSocket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(endPoint.Address)); } else { // Join the IPv4 Multicast group MulticastOption mcOption = new MulticastOption(this.MulticastEP.Address); fSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcOption); // The following is hinted at by MSDN, but is wrong // The SocketOptionLevel needs to be 'IP', not 'Udp' //fSocket.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.AddMembership, mcOption); } } // Set the timeout on the socket if (timeoutMilliseconds > 0) fSocket.ReceiveTimeout = timeoutMilliseconds; //fSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeoutMilliseconds); // Make room for 80 packets plus some overhead //fSocket.ReceiveBufferSize = UDP.MTU * 80; //fSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, UDP.MTU * 80); } catch (SocketException sockex) { Console.WriteLine(sockex.Message); } catch (Exception e) { Console.WriteLine(e.Message); this.Dispose(); throw; } }
static void Main(string[] args) { if (args.Length != 1) { string[] ports = SerialPort.GetPortNames(); Console.WriteLine("COM port parameter not provided."); Console.WriteLine("Available serial ports: "); foreach (var serialPort in ports) { Console.WriteLine(serialPort); } Console.ReadKey(); return; } StreamUART uartStream = new StreamUART(args[0]); ncpInterface = new NcpInterface(); try { ncpInterface.Open(uartStream); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); return; } // NetworkingInterface.SetupInterface(ncpInterface); Console.Write("Networkname:"); string networkname = Console.ReadLine(); Console.Write("Channel:"); byte channel = Convert.ToByte(Console.ReadLine()); Console.Write("Masterkey:"); string masterkey = Console.ReadLine(); Console.Write("Panid:"); ushort panid = Convert.ToUInt16(Console.ReadLine()); Console.Write("Listener port:"); ushort port = Convert.ToUInt16(Console.ReadLine()); try { ncpInterface.Form(networkname, channel, masterkey, panid); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); return; } UdpSocket receiver = new UdpSocket(); receiver.Bind(IPv6Address.IPv6Any, port); IPv6EndPoint remoteIp = null; while (true) { if (receiver.Poll(-1, SelectMode.SelectRead)) { byte[] data = receiver.Receive(ref remoteIp); string message = Encoding.ASCII.GetString(data); Console.WriteLine("\n"); Console.WriteLine("{0} bytes from {1} {2} {3}", message.Length, remoteIp.Address, remoteIp.Port, message); Console.WriteLine(">"); } } }