与 ftp 的套接字连接未收到应答
本文关键字:应答 连接 ftp 套接字 | 更新日期: 2023-09-27 17:55:47
我正在使用C#,Visual Studio 2008,Framework 2.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();
尝试先在客户端中阅读。