Esempio n. 1
0
 /// <summary>
 /// 使用 <see cref="TcpClient"/> 建立连接并异步接收数据
 /// </summary>
 /// <param name="ip"></param>
 /// <param name="port"></param>
 public void Connect(string ip, int port, Action <object> callback = null)
 {
     if (connectCallback != null)
     {
         return;
     }
     else
     {
         connectCallback = callback;
     }
     try
     {
         IsConnected = false;
         client      = new TcpClient
         {
             NoDelay             = true,
             ExclusiveAddressUse = true,
         };
         Address = string.Format("{0}:{1}", ip, port);
         client.BeginConnect(ip, port, new AsyncCallback(ConnectCallback), client);
     }
     catch (Exception ex)
     {
         client = null;
         Debug.LogErrorFormat("Can not connect to Server. Detail: {0}\n{1}", ex.Message, ex.StackTrace);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Connects directly to an IP address
        /// </summary>
        /// <param name="address">The IP address to which to connect</param>
        private void ConnectToIPAddress(IPAddress address)
        {
            IPEndPoint ipep = new IPEndPoint(address, GetPortForInitialConnection());
            StreamType sttp = destinationSsl ? StreamType.SslStream : StreamType.NetworkStream;

            socket = new StreamSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, sttp);
            try
            {
                socket.BeginConnect(ipep, EndConnectToIPAddress, null);
            }
            catch (Exception sockex)
            {
                OnConnectionFailed("Cannot connect to server: {0}", sockex);
            }
        }
Esempio n. 3
0
        public ReliableConnection(string remotehost, int remoteport, IRTSessionInternal session) : base(remotehost, remoteport, session)
        {
#if __WINDOWS__
            client = new StreamSocket();

            client.Control.NoDelay   = true;
            client.Control.KeepAlive = true;
            //client.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted | ChainValidationResult.InvalidName);

            System.Diagnostics.Debug.WriteLine("*** Connecting to server...");

            connected = false;

            Task t = Task.Run(() =>
            {
                connected = ConnectAsync(remotehost, remoteport).Result;

                ConnectCallback();
            });
#else
            bool ipv6 = false;

            this.remotehost = remotehost;

            if (remoteEndPoint.AddressFamily == AddressFamily.InterNetworkV6)
            {
                try
                {
                    if (Socket.OSSupportsIPv6)
                    {
                        ipv6 = true;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Socket.OSSupportsIPv6: " + e.ToString());
                }

                try
                {
                    if (Socket.SupportsIPv6)
                    {
                        ipv6 = true;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Socket.SupportsIPv6: " + e.ToString());
                }
            }

            if (ipv6)
            {
                client = new TcpClient(AddressFamily.InterNetworkV6);

                Console.WriteLine("IPv6 on!");
            }
            else
            {
                client = new TcpClient(AddressFamily.InterNetwork);
            }

            client.NoDelay = true;
            client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
            client.BeginConnect(remoteEndPoint.Address, remoteEndPoint.Port, new AsyncCallback(ConnectCallback), null);
#endif
        }