From b93e11e986e2cf52e96cf8c056255e95bb12644a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Tue, 25 Mar 2014 20:36:22 +0100 Subject: [PATCH] =?UTF-8?q?Meine=20L=C3=B6sung=20f=C3=BCr=20Aufgabenblatt?= =?UTF-8?q?=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dummyclient_manuel/Program.cs | 17 +++++++ dummyclient_manuel/Receiver.cs | 24 +++++++++ dummyclient_manuel/Sender.cs | 26 ++++++++++ dummyclient_manuel/Terminal.cs | 91 ++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 dummyclient_manuel/Program.cs create mode 100644 dummyclient_manuel/Receiver.cs create mode 100644 dummyclient_manuel/Sender.cs create mode 100644 dummyclient_manuel/Terminal.cs diff --git a/dummyclient_manuel/Program.cs b/dummyclient_manuel/Program.cs new file mode 100644 index 0000000..d33c2f8 --- /dev/null +++ b/dummyclient_manuel/Program.cs @@ -0,0 +1,17 @@ +using System.Net.Sockets; +using System.Threading; + +public class Program +{ + public static void Main () + { + Terminal terminal = new Terminal (); + TcpClient client = new TcpClient ("localhost", 9999); + Receiver receiver = new Receiver (client.GetStream (), terminal); + Sender sender = new Sender (client.GetStream (), terminal); + Thread receiverThread = new Thread (new ThreadStart (receiver.Receive)); + receiverThread.Start (); + sender.Send (); + client.Close (); + } +} \ No newline at end of file diff --git a/dummyclient_manuel/Receiver.cs b/dummyclient_manuel/Receiver.cs new file mode 100644 index 0000000..89db337 --- /dev/null +++ b/dummyclient_manuel/Receiver.cs @@ -0,0 +1,24 @@ +using System; +using System.IO; +using System.Net.Sockets; + +class Receiver +{ + private StreamReader Reader; + private Terminal Term; + + public Receiver (NetworkStream stream, Terminal terminal) + { + this.Reader = new StreamReader(stream); + this.Term = terminal; + } + + public void Receive () + { + while (true) { + String line = Reader.ReadLine(); + Term.PrintLine(line); + } + } + +} diff --git a/dummyclient_manuel/Sender.cs b/dummyclient_manuel/Sender.cs new file mode 100644 index 0000000..eb71de9 --- /dev/null +++ b/dummyclient_manuel/Sender.cs @@ -0,0 +1,26 @@ +using System; +using System.IO; +using System.Net.Sockets; +using System.Text; + +public class Sender +{ + private NetworkStream NStream; + private Terminal Term; + + public Sender (NetworkStream stream, Terminal terminal) + { + this.Term = terminal; + this.NStream = stream; + } + + public void Send () + { + while (true) { + String line = Term.ReadLine (); + line += "\n"; + byte[] bytes = Encoding.UTF8.GetBytes(line); + NStream.Write (bytes, 0, bytes.Length); + } + } +} \ No newline at end of file diff --git a/dummyclient_manuel/Terminal.cs b/dummyclient_manuel/Terminal.cs new file mode 100644 index 0000000..5e9f25d --- /dev/null +++ b/dummyclient_manuel/Terminal.cs @@ -0,0 +1,91 @@ +using System; +using System.Threading; + +public class Terminal +{ + private Object TerminalLock = new Object (); + private Object ReadLineLock = new Object (); + private AutoResetEvent ResetEvent = new AutoResetEvent(false); + private String InputBuffer = ""; + private bool AcceptInput = false; + private int OutLinePos; + + public Terminal () + { + OutLinePos = Console.CursorTop; + Thread catchInputThread = new Thread(new ThreadStart(this.CatchInput)); + catchInputThread.Start (); + } + + private void CatchInput () + { + while (true) { + ConsoleKeyInfo key = Console.ReadKey (false); + lock (TerminalLock) { + if (!AcceptInput) + { + Console.CursorLeft--; + Console.Write(" "); + Console.CursorLeft--; + continue; + } + if (key.Key == ConsoleKey.Backspace) + { + if (InputBuffer.Length == 0) + continue; + InputBuffer = InputBuffer.Substring(0, InputBuffer.Length - 1); + ClearInputLine (); + Console.Write (InputBuffer); + } + else if (key.Key == ConsoleKey.Enter) + { + AcceptInput = false; + ResetEvent.Set (); + OutLinePos = Console.CursorTop; + } + else + { + InputBuffer += key.KeyChar; + ClearInputLine (); + Console.Write (InputBuffer); + } + } + } + } + + public void PrintLine (String s) + { + lock (TerminalLock) { + ClearInputLine(); + Console.WriteLine (s); + OutLinePos = Console.CursorTop; + Console.Write (InputBuffer); + } + } + + public String ReadLine () + { + lock (ReadLineLock) { + String result; + AcceptInput = true; + ResetEvent.WaitOne(); + result = InputBuffer; + InputBuffer = ""; + return result; + } + } + + public void ClearInputLine () + { + lock (TerminalLock) { + int top = Console.CursorTop; + int left = Console.CursorLeft; + Console.SetCursorPosition (0, OutLinePos); + while (left != Console.CursorLeft || top != Console.CursorTop) { + Console.Write (" "); + } + Console.SetCursorPosition (0, OutLinePos); + } + } +} +