コード例 #1
0
        private async Task ConnectAsync()
        {
            TcpListener listener = new TcpListener(ClientIp, ClientPort);

            listener.Start();
            Client = await listener.AcceptTcpClientAsync();

            Client.NoDelay = true;
            Server         = new TcpClient
            {
                NoDelay = true
            };
            await Server.ConnectAsync(ServerIp, ServerPort);

            listener.Stop();
            IsConnected = true;

            if (Connected != null)
            {
                Delegate[] delegates = Connected.GetInvocationList();
                foreach (Func <Task> t in delegates)
                {
                    try
                    {
                        await t().ConfigureAwait(false);
                    }
                    catch { }
                }
            }

            ClientTask = Task.Factory.StartNew(() => InterceptAsync(Client, Server), TaskCreationOptions.LongRunning);
            ServerTask = Task.Factory.StartNew(() => InterceptAsync(Server, Client), TaskCreationOptions.LongRunning);
        }
コード例 #2
0
        private void ConnectThread()
        {
            IPEndPoint  endpoint;
            FTPResponse response;

            m_cmdsocket = new Socket(AddressFamily.InterNetwork,
                                     SocketType.Stream,
                                     ProtocolType.Tcp);

            try

            {
                IPAddress address = IPAddress.Parse(m_host);
                endpoint = new IPEndPoint(address, m_port);
            }

            catch (System.FormatException)
            {
                try
                {
                    IPAddress address = Dns.GetHostEntry(m_host).AddressList[0];
                    //IPAddress address = Dns.Resolve(m_host).AddressList[0];
                    endpoint = new IPEndPoint(address, m_port);
                }
                catch (SocketException)
                {
                    return;
                }
            }

            // make the connection
            try
            {
                m_cmdsocket.Connect(endpoint);
            }
            catch (Exception)
            {
                return;
            }

            // check the result
            response = ReadResponse();

            if (response.ID != 220)
            {
                m_cmdsocket.Close();
                m_cmdsocket = null;

                if (!m_exceptions)
                {
                    return;
                }
                else
                {
                    m_connected = false;
                    return;
                }
            }

            // set the user id
            response = SendCommand("USER " + m_uid, false);

            // check the response
            if (!((response.ID == 331) || (response.ID == 230)))
            {
                m_cmdsocket.Close();
                m_cmdsocket = null;
                Disconnect();

                if (!m_exceptions)
                {
                    return;
                }
                else
                {
                    m_connected = false;
                    return;
                }
            }

            // if a PWD is required, send it
            if (response.ID == 331)
            {
                response = SendCommand("PASS " + m_pwd, false);
                if (!((response.ID == 202) || (response.ID == 230)))
                {
                    m_cmdsocket.Close();
                    m_cmdsocket = null;
                    Disconnect();
                    m_connected = false;
                    return;
                }
            }

            // get the server type
            response = SendCommand("SYST", false);
            if (response.Text.ToUpper().IndexOf("UNIX") > -1)
            {
                m_server = FTPServerType.Unix;
            }
            else if (response.Text.ToUpper().IndexOf("WINDOWS") > -1)
            {
                m_server = FTPServerType.Windows;
            }
            else
            {
                m_server = FTPServerType.Unknown;
            }

            m_connected = true;

            if (Connected != null)
            {
                foreach (FTPConnectedHandler fh in Connected.GetInvocationList())
                {
                    fh(this);
                }
            }
        }