Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            using (UdtSocket client = new UdtSocket(ProtocolType.IPv4, SocketType.Dgram))
            {
                client.Connect(new IPEndPoint(IPAddress.Loopback, 8888));

                while (true)
                {
                    client.SendMessage("Hello 你好 UDT! " + DateTime.Now.ToString());
                    Console.ReadLine();
                }
            }
        }
Exemplo n.º 2
0
        static UdtSocket PeerConnect(Socket socket, string remoteAddr, int remotePort)
        {
            bool bConnected = false;
            int  retry      = 0;

            UdtSocket client = null;

            while (!bConnected)
            {
                try
                {
                    DateTime now = InternetTime.Get();

                    int sleepTimeToSync = SleepTime(now);

                    Console.WriteLine("[{0}] - Waiting {1} sec to sync with other peer",
                                      now.ToLongTimeString(),
                                      sleepTimeToSync);
                    System.Threading.Thread.Sleep(sleepTimeToSync * 1000);

                    GetExternalEndPoint(socket);

                    if (client != null)
                    {
                        client.Close();
                    }

                    client = new UdtSocket(socket.AddressFamily, socket.SocketType);
                    client.Bind(socket);

                    Console.Write("\r{0} - Trying to connect to {1}:{2}.  ",
                                  retry++, remoteAddr, remotePort);

                    client.Connect(new IPEndPoint(IPAddress.Parse(remoteAddr), remotePort));

                    Console.WriteLine("Connected successfully to {0}:{1}",
                                      remoteAddr, remotePort);

                    bConnected = true;
                }
                catch (Exception e)
                {
                    Console.Write(e.Message.Replace(Environment.NewLine, ". "));
                }
            }

            return(client);
        }