C# Socket 问题
我想问一下,C#里面有ftp的控件吗?如果我自己写的话,用Socket里面的ip地址,如果不在一个局域网里面,ip地址要如何获得???如果要实现多线程下载,是不是当下载一个文件时,要每个线程一个Socket和一个端口读文件发送出去???先谢谢大家了
[解决办法]
private void Download(string filePath, string fileName)
{
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
[解决办法]
ftp 还是基于 客服端服务器模式的
客服端连接你主机下载文件 来一个客服端就开启一个线程 不在一个局域网的 只要你联网了 就没事了
服务器应该有个固定的ip