示例#1
0
文件: Program.cs 项目: lypvc/p2pcopy
        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);
        }
示例#2
0
文件: Program.cs 项目: lypvc/p2pcopy
        static void Main(string[] args)
        {
            CommandLineArguments cla = CommandLineArguments.Parse(args);

            if (cla == null || (!cla.Sender && !cla.Receiver))
            {
                CommandLineArguments.ShowUsage();
                Environment.Exit(1);
                return;
            }
            else if (cla.Sender && String.IsNullOrEmpty(cla.File))
            {
                CommandLineArguments.ShowUsage();
                Environment.Exit(1);
                return;
            }

            if (cla.File != null && (!(File.Exists(cla.File))))
            {
                Console.Error.WriteLine($"File {cla.File} does not exist");
                Environment.Exit(2);
                return;
            }

            string remoteIp;
            int    remotePort;

            Socket socket = new Socket(
                AddressFamily.InterNetwork,
                SocketType.Dgram, ProtocolType.Udp);

            try
            {
                if (cla.LocalPort != -1)
                {
                    Console.WriteLine("Using local port: {0}", cla.LocalPort);
                }
                else
                {
                    cla.LocalPort = 0;
                }

                socket.Bind(new IPEndPoint(IPAddress.Any, cla.LocalPort));

                P2pEndPoint p2pEndPoint = GetExternalEndPoint(socket);

                if (p2pEndPoint == null)
                {
                    return;
                }

                Console.WriteLine("Tell this to your peer: {0}", p2pEndPoint.External.ToString());

                Console.WriteLine();
                Console.WriteLine();

                Console.Write("Enter the ip:port of your peer: ");
                string peer = Console.ReadLine();

                if (string.IsNullOrEmpty(peer))
                {
                    Console.WriteLine("Invalid ip:port entered");
                    return;
                }

                // try again to connect to external to "reopen" port
                GetExternalEndPoint(socket);

                ParseRemoteAddr(peer, out remoteIp, out remotePort);

                UdtSocket connection = PeerConnect(socket, remoteIp, remotePort);

                if (connection == null)
                {
                    Console.Error.WriteLine("Failed to establish P2P conn to {0}", remoteIp);
                    Environment.Exit(3);
                    return;
                }

                try
                {
                    if (cla.Sender)
                    {
                        Sender.Run(connection, cla.File, cla.Verbose);
                        return;
                    }

                    Receiver.Run(connection);
                }
                finally
                {
                    connection.Close();
                }
            }
            finally
            {
                socket.Close();
            }
        }