Ausgabe eines Threads an Socket weiterleiten



  • Hallo zusammen,

    ich möchte gerne mit einem Thread ein Kommandozeilenprogramm aufrufen, und dessen Ausgabe dann über einen Socket bereitstellen. D. h. das Kommandozeilen-Programm läuft und wenn ich mich von einem anderen PC per telnet verbinde soll die Ausgabe des Threads auf dem Bildschirm erscheinen. Sollte doch möglich sein, oder?

    Ich weiß nur nich wie ich den Standardoutput des Prozesses innerhalb des Threads dann an den TCP-Socket weiterleite...

    Hier mal der Code den ich mir ausm I-Net zusammengesucht hab. Vllt. fällt ja jemandem was ein, wie man das am besten und elegantesten hinbekommt.

    Edit:

    Verbinden per Telnet funktioniert auch, ich kann auch dann z. B. Strings an den Client senden, aber Streams funktionieren iwie nicht...

    using System;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    using System.Diagnostics;
    using System.IO;
    
    public class serv
    {
    
       static  StreamReader mystreamreader;
        public static void Main()
        {
            try
            {
                IPAddress ipAd = IPAddress.Parse("127.0.0.1");
                // use local m/c IP address, and 
    
               /* Initializes the Listener */
                TcpListener myList = new TcpListener(ipAd, 8001);
    
                /* Start Listeneting at the specified port */
                myList.Start();
    
                Console.WriteLine("The server is running at port 8001...");
                Console.WriteLine("The local End point is  :" +myList.LocalEndpoint);
                Console.WriteLine("Waiting for a connection.....");
    
                Socket s = myList.AcceptSocket();
                Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
    
                Thread th = new Thread(new ThreadStart(aufgabe)); // Thread, dessen Ausgabe weitergeleítet werden soll
                th.Start();
    
                ASCIIEncoding asen = new ASCIIEncoding();
                s.Send(asen.GetBytes(Convert.ToString(mystreamreader.Read())));
    
                s.Close();
                myList.Stop();
    
            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
        }
    
        public static void aufgabe()
        {
            Console.WriteLine("Ping-Thread gestartet.");
    
            Process process = new Process();
    
            process.StartInfo.FileName = "c:/windows/system32/ping.exe";
            process.StartInfo.Arguments = "google.de -t";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();
    
            mystreamreader = process.StandardOutput;
        }
    
    }
    

    Grüße,
    Markus



  • Jetzt funkionierts. Ohne den zusätzlichen Thread.

    using System;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    using System.Diagnostics;
    using System.IO;
    
    public class serv
    {
    
       static  StreamReader mystreamreader;
        public static void Main()
        {
            try
            {
                /* Initializes the Listener */
                TcpListener myList = new TcpListener(System.Net.IPAddress.Any, 8001);
    
                /* Start Listeneting at the specified port */
                myList.Start();
    
                Console.WriteLine("The server is running at port 8001...");
                Console.WriteLine("The local End point is  :" +
                                  myList.LocalEndpoint);
    
                while (true)
                {
                    Console.WriteLine("Waiting for a connection.....");
                    Socket s = myList.AcceptSocket();
                    Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
    
                    Process p = aufgabe();
    
                    mystreamreader = p.StandardOutput;
    
                    ASCIIEncoding asen = new ASCIIEncoding();
    
                    try
                    {
                        while (true)
                        {
                            byte[] arr = new byte[1];
                            arr[0] = (byte)mystreamreader.BaseStream.ReadByte();
    
                            s.Send(arr);
                        }
                    }
    
                    catch (Exception ex)
                    {
                        Console.WriteLine("Fehler. Vermutlich hat Client die Verbindung getrennt.");
                    }
                }
            }
    
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
    
        public static Process aufgabe()
        {  
            Process process = new Process();
    
            process.StartInfo.FileName = "c:/windows/system32/ping.exe";
            process.StartInfo.Arguments = "google.de -t";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();
    
            Console.WriteLine("Ping-Process gestartet.");
    
            return process;                
        }
    
    }
    

    Gruß,
    Markus


Anmelden zum Antworten