示例#1
0
        public void Connect()
        {
            if (State == RxState.Open) return;
            if (!IsInitialized)
            {
                throw new RxRemoteException("Unable to continue. Remoting client has not been initialized");
            }

            try
            {
                IPAddress address;
                if (!IPAddress.TryParse(Host, out address))
                {
                    IPHostEntry entry = Dns.GetHostEntry(Host);
                    if (entry.AddressList.Length > 0)
                    {
                        address = entry.AddressList[0];
                    }
                }

                client        = new TcpClient();

                IPEndPoint ep = new IPEndPoint(address, Port);
                client.Connect(ep);

                stream = client.GetStream();

                byte[] buf  = new byte[1024];
                int count   = stream.Read(buf, 0, buf.Length);
                string data = Encoding.ASCII.GetString(buf, 0, count);

                if (data.Substring(0, 4) == "v001")
                {
                    Version = RxVersion.Version1;
                }
                else if (data.Substring(0, 1) == "e")
                {
                    throw new RxRemoteException(data.Substring(1));
                }
                else
                {
                    throw new RxRemoteException("Remote2 server version unknown.",
                        new NotSupportedException(
                            "Server could be running a newer version of the protocol that this client doesn't support."));
                }

                encoding = new UTF8Encoding(false);
                reader   = new StreamReader(stream, encoding);
                writer   = new StreamWriter(stream, encoding);

                State = RxState.Open;

                Auth();

                reader.ReadLineAsync().ContinueWith(OnAsyncRead, source.Token);
            }
            catch (SocketException e)
            {
                State = RxState.Closed;

                throw new RxRemoteException("Unable to establish connection to remote server. See inner exception for details.", e);
            }
        }
示例#2
0
        public void Connect()
        {
            if (State == RxState.Open)
            {
                return;
            }
            if (!IsInitialized)
            {
                throw new RxRemoteException("Unable to continue. Remoting client has not been initialized");
            }

            try
            {
                IPAddress address;
                if (!IPAddress.TryParse(Host, out address))
                {
                    IPHostEntry entry = Dns.GetHostEntry(Host);
                    if (entry.AddressList.Length > 0)
                    {
                        address = entry.AddressList[0];
                    }
                }

                client = new TcpClient();

                IPEndPoint ep = new IPEndPoint(address, Port);
                client.Connect(ep);

                stream = client.GetStream();

                byte[] buf   = new byte[1024];
                int    count = stream.Read(buf, 0, buf.Length);
                string data  = Encoding.ASCII.GetString(buf, 0, count);

                if (data.Substring(0, 4) == "v001")
                {
                    Version = RxVersion.Version1;
                }
                else if (data.Substring(0, 1) == "e")
                {
                    throw new RxRemoteException(data.Substring(1));
                }
                else
                {
                    throw new RxRemoteException("Remote2 server version unknown.",
                                                new NotSupportedException(
                                                    "Server could be running a newer version of the protocol that this client doesn't support."));
                }

                encoding = new UTF8Encoding(false);
                reader   = new StreamReader(stream, encoding);
                writer   = new StreamWriter(stream, encoding);

                State = RxState.Open;

                Auth();

                reader.ReadLineAsync().ContinueWith(OnAsyncRead, source.Token);
            }
            catch (SocketException e)
            {
                State = RxState.Closed;

                throw new RxRemoteException("Unable to establish connection to remote server. See inner exception for details.", e);
            }
        }