summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarcinzelent <zelent.marcin@protonmail.com>2017-10-10 11:21:01 +0200
committermarcinzelent <zelent.marcin@protonmail.com>2017-10-10 11:21:01 +0200
commita7b4fc7be6dac8c4ff9ae7924eb499d7e2ac1453 (patch)
treea9a560ccfa0fb990ed766d1a1d225115c16ffa55 /Client/TcpChatClient.cs
parent9ce162142924b6f3c8c977624ed2b2c1916f9be2 (diff)
Removed packages, added Client project properly.HEADmaster
Diffstat (limited to 'Client/TcpChatClient.cs')
-rw-r--r--Client/TcpChatClient.cs69
1 files changed, 69 insertions, 0 deletions
diff --git a/Client/TcpChatClient.cs b/Client/TcpChatClient.cs
new file mode 100644
index 0000000..e9d4b54
--- /dev/null
+++ b/Client/TcpChatClient.cs
@@ -0,0 +1,69 @@
+using System;
+using System.ComponentModel;
+using System.IO;
+using System.Net;
+using System.Net.Sockets;
+using System.Runtime.CompilerServices;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Client
+{
+ public class TcpChatClient : INotifyPropertyChanged
+ {
+ public int Port { get; set; }
+
+ private string log;
+ public string Log
+ {
+ get
+ {
+ return log;
+ }
+ set
+ {
+ log = value;
+ OnPropertyChanged();
+ }
+ }
+ public bool Status { get; set; }
+ private StreamReader sr;
+ private StreamWriter sw;
+
+ public TcpChatClient()
+ {
+ }
+
+ public async Task Connect(string ipAddress, int port)
+ {
+ TcpClient clientSocket = new TcpClient(ipAddress, port);
+ NetworkStream ns = clientSocket.GetStream();
+ sr = new StreamReader(ns);
+ sw = new StreamWriter(ns);
+ sw.AutoFlush = true;
+ Status = true;
+ await ReceiveMessageAsync();
+ }
+
+ public void SendMessage(string message)
+ {
+ sw.WriteLine(message);
+ }
+
+ private async Task ReceiveMessageAsync()
+ {
+ while (Status)
+ {
+ string message = await sr.ReadLineAsync();
+ Log += $"{message}\n";
+ }
+ }
+
+ public event PropertyChangedEventHandler PropertyChanged;
+
+ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
+ {
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+ }
+ }
+}