Meine Lösung für Aufgabenblatt 2
This commit is contained in:
17
dummyclient_manuel/Program.cs
Normal file
17
dummyclient_manuel/Program.cs
Normal file
@@ -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 ();
|
||||||
|
}
|
||||||
|
}
|
||||||
24
dummyclient_manuel/Receiver.cs
Normal file
24
dummyclient_manuel/Receiver.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
26
dummyclient_manuel/Sender.cs
Normal file
26
dummyclient_manuel/Sender.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
91
dummyclient_manuel/Terminal.cs
Normal file
91
dummyclient_manuel/Terminal.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user