这是我们的代码。我想在循环中ftp文件?请建议适当的方法吗?您的回答将受到高度赞赏。有时候它可以很好地工作,但通常无法将文件FTP到FTP服务器。
delegate void mThread(ref bool isRunning);
delegate void AccptTcpClnt(ref TcpClient client, TcpListener listener);
public static void AccptClnt(ref TcpClient client, TcpListener listener)
{
if (client == null)
client = listener.AcceptTcpClient();
}
public void SendStream(Stream stream, string remoteFileName, FTPFileTransferType type, lblStatusDelegate olblStatusDelegate)
{
bool isRunning = true;
LockTcpClient();
TcpListener listner = null;
TcpClient client = null;
NetworkStream networkStream = null;
ArrayList tempMessageList = new ArrayList();
int returnValue = 0;
string returnValueMessage = "";
tempMessageList = new ArrayList();
SetTransferType(type);
if(this.mode == FTPMode.Active)
{
listner = CreateDataListner();
listner.Start();
}
else
{
client = CreateDataClient();
}
tempMessageList = SendCommand("STOR " + remoteFileName);
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(!(returnValue == 150 || returnValue == 125))
{
throw new Exception((string)tempMessageList[0]);
}
if(this.mode == FTPMode.Active)
{
AccptTcpClnt t = new AccptTcpClnt(AccptClnt);
Thread tt = new Thread(() => t(ref client, listner));
tt.IsBackground = true;
tt.Start();
while (isRunning && tt.IsAlive && client== null)
Thread.Sleep(500); //change the time as you prefer
}
if (client != null)
{
networkStream = client.GetStream();
Byte[] buffer = new Byte[BLOCK_SIZE];
int bytes = 0;
int totalBytes = 0;
string TotalLength = BytesFormat(stream.Length, "kb");
while (totalBytes < stream.Length)
{
bytes = (int)stream.Read(buffer, 0, BLOCK_SIZE);
totalBytes = totalBytes + bytes;
networkStream.Write(buffer, 0, bytes);
if (olblStatusDelegate != null)
{
olblStatusDelegate.Invoke("Uploaded " + BytesFormat(totalBytes, "kb") + " of " + TotalLength);
}
}
networkStream.Close();
client.Close();
if (this.mode == FTPMode.Active)
{
listner.Stop();
}
if (tempMessageList.Count == 1)
{
tempMessageList = Read();
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
returnValueMessage = (string)tempMessageList[0];
}
else
{
returnValue = GetMessageReturnValue((string)tempMessageList[1]);
returnValueMessage = (string)tempMessageList[1];
}
if (!(returnValue == 226))
{
throw new Exception(returnValueMessage);
}
}
UnlockTcpClient();
}
答案 0 :(得分:0)
似乎它正在提供连接超时异常。您是否通过调试进行了检查? 通常,超时异常发生在
中listener.AcceptTcpClient();