MinimalisticTelnet

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using MinimalisticTelnet;

public class GetSocket
{

    // This method requests the home page content for the specified server.
    private static string SocketSendReceive(string server, int port)
    {
        //string request = "GET / Telnet\r\nHost: " + server + 
        //"\r\nConnection: Close\r\n\r\n";
        string request = "o gayanb";
        Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
        Byte[] bytesReceived = new Byte[256];

        // Create a socket connection with the specified server and port.
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        //Socket s = ConnectSocket(server, port);
        s.Connect("GayanB", 23);

        if (s == null)
            return ("Connection failed");

        // Send request to the server.
        s.Send(bytesSent, bytesSent.Length, 0);

        // Receive the server home page content.
        int bytes = 0;
        string page = "";


        // The following will block until te page is transmitted.

        bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
        page = page + Encoding.ASCII.GetString(bytesReceived);



        return page;
    }

    public static void Main(string[] args)
    {
        string host;
        int port = 23;

        if (args.Length == 0)
            // If no server name is passed as argument to this program, 
            // use the current host name as the default.
            host = Dns.GetHostName();
        else
            host = args[0];

        //string result = SocketSendReceive(host, port);
        mycoinnect();

        //Console.WriteLine(result);
        Console.Read();
    }

    private static void mycoinnect()
    {
        //create a new telnet connection to hostname "gobelijn" on port "23"

        TelnetConnection tc = new TelnetConnection("192.168.0.23", 23);

        //login with user "root",password "rootpassword", using a timeout of 100ms, 

        //and show server output

        string s = tc.Login(@"mydomain\gayanb", "mypassword", 1000);
        Console.Write(s);

        // server output should end with "$" or ">", otherwise the connection failed

        string prompt = s.TrimEnd();
        prompt = s.Substring(prompt.Length - 1, 1);
        if (prompt != "$" && prompt != ">")
            throw new Exception("Connection failed");

        prompt = "";

        // while connected

        while (tc.IsConnected && prompt.Trim() != "exit")
        {
            // display server output

            Console.Write(tc.Read());

            // send client input to server

            prompt = Console.ReadLine();
            tc.WriteLine(prompt);

            // display server output

            Console.Write(tc.Read());
        }

        Console.WriteLine("***DISCONNECTED");
        Console.ReadLine();

    }
}
 

0 ප්‍රතිචාර:

Post a Comment