public override EndPoint Create(SocketAddress socketAddress) { int port = (socketAddress[2] << 8 & 0x0000FF00) | (socketAddress[3] & 0x000000FF); int flowInformation = socketAddress[4] << 24 | socketAddress[5] << 16 | socketAddress[6] << 8 | socketAddress[7]; byte [] V6Array = new byte[IPv6Address.IPv6AddressSize]; for (int i = 0; i < IPv6Address.IPv6AddressSize; i++) { V6Array[i] = socketAddress[8 + i]; } uint scope = (uint) (socketAddress[27] << 24 | socketAddress[26] << 16 | socketAddress[25] << 8 | socketAddress[24]); IPv6Address ipv6Address = new IPv6Address(V6Array, scope); IPv6EndPoint created = new IPv6EndPoint(ipv6Address, port, flowInformation); return(created); }
static void ParseShouldSucceed(string text) { Assert.IsTrue(IPv6EndPoint.TryParse(text, out IPv6EndPoint actual)); IPEndPoint expected = IPEndPoint.Parse(text); Assert.AreEqual(expected, actual); Assert.AreEqual(expected.ToString(), actual.ToString()); }
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; }
static void Main(string[] args) { int Port = 5150; // Verify arguments if (args.Length < 2 || (args[0] != "IPv4" && args[0] != "IPv6")) { Console.WriteLine("Usage: TCPClient.exe <IPv4 | IPv6> <server name | IPv4 address | IPv6 address> [port]"); return; } if (args.Length >= 3) { Port = System.Convert.ToInt32(args[2].ToString()); } try { Socket MySocket; if (args[0] == "IPv4") { IPHostEntry IPHost = Dns.GetHostByName(args[1].ToString()); Console.Write("Connecting to: "); Console.Write(IPHost.AddressList.GetValue(0).ToString()); Console.WriteLine(":" + Port.ToString()); IPEndPoint ServerEndPoint = new IPEndPoint(IPHost.AddressList[0], Port); MySocket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); MySocket.Connect(ServerEndPoint); } else // IPv6 { IPv6Address ServerAddress = IPv6Address.Parse(args[1]); IPv6EndPoint ServerEndPoint = new IPv6EndPoint(ServerAddress, Port); MySocket = new Socket( AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.IP); MySocket.Connect(ServerEndPoint); } String s = "Hello - This is a test"; Byte[] buf = System.Text.Encoding.ASCII.GetBytes(s.ToCharArray()); int BytesSent = MySocket.Send(buf); System.Console.WriteLine("Successfully sent " + BytesSent.ToString() + " byte(s)"); MySocket.Close(); } catch (System.Net.Sockets.SocketException err) { Console.WriteLine("Error: " + err.Message); } }
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(">"); } } }
static void Main(string[] args) { int Port = ListeningPort; // Verify arguments if (args.Length < 1 || (args[0] != "IPv4" && args[0] != "IPv6")) { Console.WriteLine("Usage: TCPServer.exe <IPv4 | IPv6> [port]"); return; } if (args.Length >= 2) { Port = System.Convert.ToInt32(args[1].ToString()); } try { Socket ListeningSocket; if (args[0] == "IPv4") { // Create a new socket to listening for client connections. ListeningSocket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); // Setup a SOCKADDR_IN structure that will tell bind that we // want to listen for connections on all interfaces using port // 5150. IPEndPoint LocalEndPoint = new IPEndPoint(IPAddress.Any, Port); ListeningSocket.Bind(LocalEndPoint); } else // IPv6 { // Create a new socket to listening for client connections. ListeningSocket = new Socket( AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.IP); IPv6EndPoint LocalEndPoint = new IPv6EndPoint(IPv6Address.Any, Port); ListeningSocket.Bind(LocalEndPoint); } ListeningSocket.Listen(5); Console.WriteLine("Server started - Press RETURN to stop the server."); Console.WriteLine("Awaiting socket connections..."); AcceptInfo AI = new AcceptInfo(ListeningSocket); for (int i = 0; i < MaxAsyncAccepts; i++) { AI.ListeningSocket.BeginAccept(AI.AcceptCallback, AI); } Console.ReadLine(); ListeningSocket.Close(); AI.RemoveAllSockets(); Console.WriteLine("Pending connections were closed - press RETURN to stop application"); Console.ReadLine(); } catch (SocketException err) { Console.WriteLine("Error: " + err.Message); } }