我使用C#,Visual Studio 2008,用于嵌入式设备的Framework 2.0。
我在使用Socket连接与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命令和响应之间不使用Sleep,有时候我没有得到正确的响应(空字符串)。 例如,发送命令" USER Andy"将导致回复""没有设置睡眠时。当睡眠设置为1秒时,我确实得到了正确的答案,即#34; 331 Andy"所需的密码。
我试图调试我的Socket而不与FTP交谈并选择像Hercules这样的软件。一切都按预期工作,我的接收方法电话只是挂起直到它收到答案。
我在这里做错了什么?
答案 0 :(得分:0)
我正在写一个ftp服务器,而我正在做的连接是:
streamWrite.WriteLine("220 'Any text here'");
streamWrite.Flush();
尝试先在您的客户端阅读。