static void Client() { UdpSocket client = UdpSocket.Create <UdpPlatformManaged, DummySerializer>(); client.Start(UdpEndPoint.Any); client.Connect(new UdpEndPoint(UdpIPv4Address.Localhost, 14000)); while (true) { UdpEvent ev = default(UdpEvent); while (client.Poll(ref ev)) { UdpLog.User("Event raised {0}", ev.EventType); switch (ev.EventType) { case UdpEventType.Connected: UdpLog.User("Connected to server at {0}", ev.Connection.RemoteEndPoint); break; #if DISABLE_AUTO_ACCEPT case UdpEventType.ConnectFailed: UdpLog.User("Connection to {0} failed", ev.EndPoint); break; #endif } } // Simulate ~60fps game loop Thread.Sleep(16); } }
static void Client(int count) { // random object used to generate random port Random rnd = new Random(); UdpSocket client = UdpSocket.Create <UdpPlatformManaged, DummySerializer>(); client.Start(UdpEndPoint.Any); client.Connect(new UdpEndPoint(UdpIPv4Address.Localhost, (ushort)(14000 + rnd.Next(count)))); while (true) { UdpEvent ev; while (client.Poll(out ev)) { UdpLog.User("Event raised {0}", ev.EventType); switch (ev.EventType) { case UdpEventType.Connected: UdpLog.User("Connected to server at {0}", ev.Connection.RemoteEndPoint); break; } } // Simulate ~60fps game loop Thread.Sleep(16); } }
static void Client() { UdpSocket socket = UdpSocket.Create <UdpPlatformManaged, Serializer>(); socket.Start(new UdpEndPoint(UdpIPv4Address.Localhost, 0)); socket.Connect(new UdpEndPoint(UdpIPv4Address.Localhost, 14000)); EventLoop(socket); }
public override void ConnectAsync(IPEndPoint localAddress, IPEndPoint remoteAddress) { udpRemote = new UdpEndPoint(new UdpIPv4Address(NetUtil.GetLongAddress(remoteAddress.Address.GetAddressBytes())), (ushort)remoteAddress.Port); remote = remoteAddress; if (!IsConnected() && !IsConnecting()) { socket.Connect(udpRemote); object statusLock; Monitor.Enter(statusLock = this.statusLock); try { connectionStatus = ConnectionStatus.Connecting; } finally { Monitor.Exit(statusLock); } } }
private static void DoUdp(string[] args) { if (args.Length == 0) { return; } else if (args[0].ToLower() == "open") { if (isUdpOpen) { Console.WriteLine("Udp already opened"); } else { isUdpOpen = true; } } else if (args[0].ToLower() == "close") { if (isUdpOpen) { //stop listener threads } else { isUdpOpen = false; } } else if (args[0].ToLower() == "bind") { //because ipv6 string parsing algorithm is complicated, for now and just in console application using .Net class for it System.Net.IPAddress ip = System.Net.IPAddress.Parse(args[1]); IPv6Address ipv6Address = new IPv6Address(ip.GetAddressBytes()); ushort port = Convert.ToUInt16(args[2]); threadUdpListening = new Thread(() => UDPListenerThread(ipv6Address, port)); threadUdpListening.Start(); } else if (args[0].ToLower() == "send" && args.Length == 4) { System.Net.IPAddress ip = System.Net.IPAddress.Parse(args[1]); IPv6Address ipv6Address = new IPv6Address(ip.GetAddressBytes()); ushort port = Convert.ToUInt16(args[2]); byte[] data = Encoding.UTF8.GetBytes(args[3]); UdpSocket udpClient = new UdpSocket(); udpClient.Connect(ipv6Address, port); udpClient.Send(data, data.Length); udpClient.Close(); } }
public void KcpStart() { //enabled = true; kcpClient = new UdpSocket((buff, length) => { var rcvtime = kcpUtil.nowTotalMilliseconds; var sndtime = BitConverter.ToUInt32(buff, 0); var usetime = rcvtime - sndtime; pckCntFrmTime += usetime; pckCalcCur++; }); kcpClient.Connect(curHost.host, curHost.portKcp); }
void Start() { socket = UdpKitUnityUtils.CreatePlatformSpecificSocket <demoSerializer>(); if (isServer) { socket.Start(new UdpEndPoint(serverAddress)); } else { socket.Start(UdpEndPoint.Any); socket.Connect(new UdpEndPoint(serverAddress)); } }
static void Main(string[] args) { var wait_response = true; var client = new UdpSocket((byte[] buf) => { wait_response = false; Console.WriteLine("recv message: {0}", ASCIIEncoding.UTF8.GetString(buf)); }); client.Connect("localhost", 13377); client.SendUTF8("Hello KCP."); while (wait_response) { client.Update(); System.Threading.Thread.Sleep(10); } }
public void ConnectServer(CClientNetworkCtrl clientNetworkCtrl) { //sever and client. localPort = clientNetworkCtrl.LocalPort; UdpEndPoint serverPoint = new UdpEndPoint(UdpIPv4Address.Any, (ushort)localPort); server.Start(serverPoint); IPAddress ipaddr = IPAddress.Parse(clientNetworkCtrl.ServerIP); hostUser.ProxyServer = new IPEndPoint(ipaddr, clientNetworkCtrl.ServerPort); UdpIPv4Address address = UdpIPv4Address.Parse(clientNetworkCtrl.ServerIP); UdpEndPoint endp = new UdpEndPoint(address, (ushort)clientNetworkCtrl.ServerPort); server.Connect(endp); Log.info("UdpClientNetWork", "UdpClientNetWork Start, UdpPort:" + localPort + " ServerIP:" + clientNetworkCtrl.ServerIP.ToString() + " ProxyServer address:" + endp.ToString()); CtrlOwner = clientNetworkCtrl; }
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(); }
static void Main(string[] args) { // Console.WriteLine("Hello World!"); UdpSocket.Connect(); Console.ReadKey(); }
public Client() { socket = UdpSocket.Create <UdpPlatformManaged, ChatSerializer>(); socket.Start(UdpEndPoint.Any); socket.Connect(new UdpEndPoint(UdpIPv4Address.Localhost, 14000)); }
public Client() { socket = UdpSocket.Create<UdpPlatformManaged, ChatSerializer>(); socket.Start(UdpEndPoint.Any); socket.Connect(new UdpEndPoint(UdpIPv4Address.Localhost, 14000)); }
public void Connect(string host, UInt16 port) { mSocket = new UdpSocket(OnEventHandler); mSocket.Connect(host, port); }