Ryan

Ryan

  • NA
  • 2
  • 0

Check if ftp is running

May 27 2009 11:31 PM

Hi

I am looking a code VB or C# that check whether a ftp is running on another machine.

I have this from internet

 

public void ConnectFtpSite(string host, int port)
        {
            try
            {
                socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPHostEntry iphost = Dns.GetHostByName(host);
                IPAddress ip = iphost.AddressList[0];//ip of the host name
                IPEndPoint iep = new IPEndPoint(ip, port);

                s.Connect(iep);

                string response;

                SendRequest(s, "USER anonymous\r\n");
                response = GetResponse(s);
                //Console.WriteLine(response);               
            }

            catch (Exception e)
            {
                // Handle exception
            }
        }

        private void SendRequest(socket s, string cmd)
        {
            s.Send(Encoding.ASCII.GetBytes(cmd), cmd.Length, 0);
        }

        private string GetResponse(socket s)
        {
            int sizeReceived;
            string serverMessage;

            Byte[] readBytes = new Byte[1024];
            sizeReceived = s.Receive(readBytes);
            serverMessage = Encoding.ASCII.GetString(readBytes, 0, sizeReceived);
            return serverMessage;
        }
    }

    class program
    {
        static void Main(string[] args)
        {
            FTPClient fc = new FTPClient();
            // The request will be made on port 21, because on that port the FTP Server will listen for TCP conections
            fc.ConnectFtpSite("127.0.0.1", 21);
            Console.Read();
        }
    }

But hit an error saying type or namespace socket is not found.

Any one can help on this?

Thank You.