我登上FTP然后用PORT连接,创建了一个套接字服务端,要求服务器来连接我,然而我用cSocket.Accept();
在服务端的socket里创建新的Socket的时候,就一直阻塞在这里了,有什么问题会造成这个原因?
创建套接字服务端,发送命令要求FTP服务器连接我。
private Socket createDataSocket()
{
//this.sendCommand("PASV");
IPAddress addr = ((IPEndPoint)this.clientSocket.LocalEndPoint).Address;
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
bool finded = false;
while (!finded)
{
try
{
Random random = new Random();
socket.Bind(new IPEndPoint(addr, random.Next(4000, 60000)));
finded = true;
}
catch
{
}
} socket.Listen(1);
IPEndPoint ep = (IPEndPoint)socket.LocalEndPoint;
string command = string.Format("PORT {0},{1},{2}", ep.Address.ToString().Replace(".", ","), ep.Port >> 8, ep.Port & 0xff);
this.sendCommand(command); if (this.resultCode != 200) throw new FtpException(this.result.Substring(4)); return socket;
} public void Download(string remFileName, string locFileName, Boolean resume)
{
if (!this.loggedin) this.Login(); this.BinaryMode = true; Debug.WriteLine("Downloading file " + remFileName + " from " + server + "/" + remotePath, "FtpClient"); if (locFileName.Equals(""))
{
locFileName = remFileName;
} FileStream output = null; if (!File.Exists(locFileName))
output = File.Create(locFileName); else
output = new FileStream(locFileName, FileMode.Open); Socket cSocket = createDataSocket(); try
{
long offset = 0; if (resume)
{
offset = output.Length; if (offset > 0)
{
this.sendCommand("REST " + offset);
if (this.resultCode != 350)
{
//Server dosnt support resuming
offset = 0;
Debug.WriteLine("Resuming not supported:" + result.Substring(4), "FtpClient");
}
else
{
Debug.WriteLine("Resuming at offset " + offset, "FtpClient");
output.Seek(offset, SeekOrigin.Begin);
}
}
} this.sendCommand("RETR " + remFileName); if (this.resultCode != 150 && this.resultCode != 125)
{
throw new FtpException(this.result.Substring(4));
} DateTime timeout = DateTime.Now.AddSeconds(this.timeoutSeconds); Socket tmp = cSocket.Accept();
while (timeout > DateTime.Now)
{
this.bytes = tmp.Receive(buffer, buffer.Length, 0);
output.Write(this.buffer, 0, this.bytes); if (this.bytes <= 0)
{
break;
}
}
if (tmp.Connected)
{
tmp.Close();
cSocket.Close();
}
output.Close();
}这里出了问题 Socket tmp = cSocket.Accept();
一直阻塞在这里!
在服务端的socket里创建新的Socket的时候,就一直阻塞在这里了,有什么问题会造成这个原因?
创建套接字服务端,发送命令要求FTP服务器连接我。
private Socket createDataSocket()
{
//this.sendCommand("PASV");
IPAddress addr = ((IPEndPoint)this.clientSocket.LocalEndPoint).Address;
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
bool finded = false;
while (!finded)
{
try
{
Random random = new Random();
socket.Bind(new IPEndPoint(addr, random.Next(4000, 60000)));
finded = true;
}
catch
{
}
} socket.Listen(1);
IPEndPoint ep = (IPEndPoint)socket.LocalEndPoint;
string command = string.Format("PORT {0},{1},{2}", ep.Address.ToString().Replace(".", ","), ep.Port >> 8, ep.Port & 0xff);
this.sendCommand(command); if (this.resultCode != 200) throw new FtpException(this.result.Substring(4)); return socket;
} public void Download(string remFileName, string locFileName, Boolean resume)
{
if (!this.loggedin) this.Login(); this.BinaryMode = true; Debug.WriteLine("Downloading file " + remFileName + " from " + server + "/" + remotePath, "FtpClient"); if (locFileName.Equals(""))
{
locFileName = remFileName;
} FileStream output = null; if (!File.Exists(locFileName))
output = File.Create(locFileName); else
output = new FileStream(locFileName, FileMode.Open); Socket cSocket = createDataSocket(); try
{
long offset = 0; if (resume)
{
offset = output.Length; if (offset > 0)
{
this.sendCommand("REST " + offset);
if (this.resultCode != 350)
{
//Server dosnt support resuming
offset = 0;
Debug.WriteLine("Resuming not supported:" + result.Substring(4), "FtpClient");
}
else
{
Debug.WriteLine("Resuming at offset " + offset, "FtpClient");
output.Seek(offset, SeekOrigin.Begin);
}
}
} this.sendCommand("RETR " + remFileName); if (this.resultCode != 150 && this.resultCode != 125)
{
throw new FtpException(this.result.Substring(4));
} DateTime timeout = DateTime.Now.AddSeconds(this.timeoutSeconds); Socket tmp = cSocket.Accept();
while (timeout > DateTime.Now)
{
this.bytes = tmp.Receive(buffer, buffer.Length, 0);
output.Write(this.buffer, 0, this.bytes); if (this.bytes <= 0)
{
break;
}
}
if (tmp.Connected)
{
tmp.Close();
cSocket.Close();
}
output.Close();
}这里出了问题 Socket tmp = cSocket.Accept();
一直阻塞在这里!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货