CSharpUtils.Net.FTP.OpenDataSocket C# (CSharp) Метод

OpenDataSocket() приватный Метод

private OpenDataSocket ( ) : void
Результат void
		private void OpenDataSocket()
		{
			if (passive_mode)		// #######################################
			{
				string[] pasv;
				string server;
				int port;

				Connect();
				SendCommand("PASV");
				ReadResponse();
				if (response != 227)
					Fail();

				try
				{
					int i1, i2;

					i1 = responseStr.IndexOf('(') + 1;
					i2 = responseStr.IndexOf(')') - i1;
					pasv = responseStr.Substring(i1, i2).Split(',');
				}
				catch (Exception)
				{
					Disconnect();
					throw new Exception("Malformed PASV response: " + responseStr);
				}

				if (pasv.Length < 6)
				{
					Disconnect();
					throw new Exception("Malformed PASV response: " + responseStr);
				}

				server = String.Format("{0}.{1}.{2}.{3}", pasv[0], pasv[1], pasv[2], pasv[3]);
				port = (int.Parse(pasv[4]) << 8) + int.Parse(pasv[5]);

				try
				{
#if (FTP_DEBUG)
					Console.WriteLine("Data socket: {0}:{1}", server, port);
#endif
					CloseDataSocket();

#if (FTP_DEBUG)
					Console.WriteLine("Creating socket...");
#endif
					data_sock = new Socket(
						AddressFamily.InterNetwork,
						SocketType.Stream,
						ProtocolType.Tcp
					);
					SetSocketTimeout(data_sock);

#if (FTP_DEBUG)
					Console.WriteLine("Resolving host");
#endif


#if (FTP_DEBUG)
					Console.WriteLine("Connecting..");
#endif
					data_sock.Connect(server, port);

#if (FTP_DEBUG)
					Console.WriteLine("Connected.");
#endif
				}
				catch (Exception ex)
				{
					throw new Exception("Failed to connect for data transfer: " + ex.Message);
				}
			}
			else		// #######################################
			{
				Connect();

				try
				{
#if (FTP_DEBUG)
					Console.WriteLine("Data socket (active mode)");
#endif
					CloseDataSocket();

#if (FTP_DEBUG)
					Console.WriteLine("Creating listening socket...");
#endif
					listening_sock = new Socket(
						AddressFamily.InterNetwork,
						SocketType.Stream,
						ProtocolType.Tcp
					);
					SetSocketTimeout(listening_sock);

#if (FTP_DEBUG)
					Console.WriteLine("Binding it to local address/port");
#endif
					// for the PORT command we need to send our IP address; let's extract it
					// from the LocalEndPoint of the main socket, that's already connected
					string sLocAddr = main_sock.LocalEndPoint.ToString();
					int ix = sLocAddr.IndexOf(':');
					if (ix < 0)
					{
						throw new Exception("Failed to parse the local address: " + sLocAddr);
					}
					string sIPAddr = sLocAddr.Substring(0, ix);
					// let the system automatically assign a port number (setting port = 0)
					System.Net.IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(sIPAddr), 0);

					listening_sock.Bind(localEP);
					sLocAddr = listening_sock.LocalEndPoint.ToString();
					ix = sLocAddr.IndexOf(':');
					if (ix < 0)
					{
						throw new Exception("Failed to parse the local address: " + sLocAddr);
					}
					int nPort = int.Parse(sLocAddr.Substring(ix + 1));
#if (FTP_DEBUG)
					Console.WriteLine("Listening on {0}:{1}", sIPAddr, nPort);
#endif
					// start to listen for a connection request from the host (note that
					// Listen is not blocking) and send the PORT command
					listening_sock.Listen(1);
					string sPortCmd = string.Format("PORT {0},{1},{2}",
													sIPAddr.Replace('.', ','),
													nPort / 256, nPort % 256);
					SendCommand(sPortCmd);
					ReadResponse();
					if (response != 200)
						Fail();
				}
				catch (Exception ex)
				{
					throw new Exception("Failed to connect for data transfer: " + ex.Message);
				}
			}
		}