Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 到ftp的套接字连接未接收应答_C#_Sockets_Ftp - Fatal编程技术网

C# 到ftp的套接字连接未接收应答

C# 到ftp的套接字连接未接收应答,c#,sockets,ftp,C#,Sockets,Ftp,我正在使用C#,VisualStudio2008,用于嵌入式设备的Framework2.0 我在使用套接字连接与FTP通信时遇到问题 为了你们能帮助我,我写了一段话来解释我的烦恼 IPEndPoint endpoint; Socket cmdsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress address

我正在使用C#,VisualStudio2008,用于嵌入式设备的Framework2.0

我在使用套接字连接与FTP通信时遇到问题

为了你们能帮助我,我写了一段话来解释我的烦恼

        IPEndPoint endpoint;
        Socket cmdsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        IPAddress address = IPAddress.Parse(args[0]);
        endpoint = new IPEndPoint(address, int.Parse(args[1]));   

        // Connection
        cmdsocket.Connect(endpoint);

        if (cmdsocket.Connected)
        {
            do
            {
                Console.WriteLine("Type the command to send : ");
                string l_sCommandToSend = Console.ReadLine();
                Console.WriteLine("Do you want a timeout to receive ? (Y/N (default))");
                string l_sReceiveTimeout = Console.ReadLine();

                // Sending
                byte[] bytes = Encoding.ASCII.GetBytes((l_sCommandToSend + "\r\n").ToCharArray());
                cmdsocket.Send(bytes, bytes.Length, 0);
                if (l_sReceiveTimeout == "Y")
                {
                    Thread.Sleep(1000);
                }

                // Receiving
                byte[] bufferToRead = new byte[512];
                string l_sResponsetext = "";
                int l_iByteReceivedCount = 0;
                while (cmdsocket.Available > 0)
                {
                    Console.WriteLine("Receiving...");
                    l_iByteReceivedCount = cmdsocket.Receive(bufferToRead, SocketFlags.None);
                    l_sResponsetext += Encoding.ASCII.GetString(bufferToRead, 0, l_iByteReceivedCount);
                    Console.WriteLine("Bytes Received : " + l_iByteReceivedCount);
                    Console.WriteLine("l_sResponsetext : " + l_sResponsetext);
                }
                Console.WriteLine("This is the answer : " + l_sResponsetext);
            } while (true);
        }


        Console.WriteLine("Job done.");
        Console.ReadLine();

        // Fermeture du socket
        cmdsocket.Close();
        cmdsocket = null;
上面是一个片段:

  • 连接到FTP(我在服务器端使用FileZilla服务器进行测试)
  • 询问用户要发送的命令
  • 询问用户是否希望在发送和接收之间睡眠(稍后详细信息)
  • 阅读并显示答案
我注意到,如果我不在FTP命令和响应之间使用睡眠,有时我就得不到正确的响应(空字符串)。 例如,当未设置睡眠时,发送命令“USER Andy”将导致响应“”。当睡眠设置为1秒时,我得到了正确的答案,即“安迪需要331密码”

我试图在不与FTP通信的情况下调试我的套接字,并选择了像Hercules这样的软件。一切正常,我的Receive方法调用只是挂起,直到收到应答为止


我在这里做错了什么?

我正在编写一个ftp服务器,在连接开始时,我正在做的是:

streamWrite.WriteLine("220 'Any text here'");
streamWrite.Flush();
试着先阅读你的客户