socket usage permission
-
Hi I recieve this exception: "Only one usage of each socket address (protocol/network address/port) is normally permitted" I should get several files from an ftp server. I implemented it by sockets so: create the datasocket, recieve the file and then close data socket. But the next time I do this for another file(create data socket with Endpoint (the same local address/another port)), i recieve this exception message when Binding the socket. Can anyone tell me why? And the way to solve that? (the code, if necessary)
public void download(string remFileName, string locFileName)
{
if (!logined)
{
login();
}setBinaryMode(true); Console.WriteLine("Downloading file " + remFileName + " from " + remoteHost + "/" + remotePath); if (locFileName.Equals("")) { locFileName = remFileName; } if (!File.Exists(locFileName)) { Stream st = File.Create(locFileName); st.Close(); } FileStream output = new FileStream(locFileName, FileMode.Open); createDataSocket(true); if (dataSocket != null) { throw new SocketException(); } //\*\*\*\* sendCommand("RETR " + remFileName); if (!(retValue == 150 || retValue == 125)) { throw new IOException(reply.Substring(4)); } //\*\*\*\* dataSocket = listeningSocket.Accept(); listeningSocket.Close(); listeningSocket = null; if (dataSocket == null) { throw new Exception("Winsock error: " + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error())); } while (true) { bytes = dataSocket.Receive(buffer, buffer.Length, 0); output.Write(buffer, 0, bytes); if (bytes <= 0) { break; } } output.Close(); if (dataSocket.Connected) { //dataSocket.Shutdown(SocketShutdown.Both); dataSocket.Close(); dataSocket = null; } Console.WriteLine(""); readReply(); if (!(retValue == 226 || retValue == 250)) { throw new IOException(reply.Substring(4)); }
}
public void createDataSocket(bool flag)
{
try
{
listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
catch (Exception e)
{
ftpLog(e.Message);
throw new IOException(e.Message);
}string localAddress = clientSocket.LocalEndPoint.To