diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e136998 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Daniel Duller + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 1deee43..c0bf9f3 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,11 @@ There is no installation required. Just download the **TCP_Server_Client_Tester. * [WPF](https://docs.microsoft.com/en-us/dotnet/framework/wpf/) - Framework used for creating the GUI * [TcpConnection_Lib](https://github.com/dadul96/TcpConnection_Lib) - C# library that contains the TCP connection handling +| **TcpConnection_Lib-versions** | | **TCP_Server_Client_Tester-versions** | +|--------------------------------------------------------------------------- |----- |---------------------------------------------------------------------------------- | +| [v1.0.0](https://github.com/dadul96/TcpConnection_Lib/releases/tag/v1.0.0) | <- | [v1.0.0](https://github.com/dadul96/TCP_Server_Client_Tester/releases/tag/v1.0.0) | +| [v2.0.0](https://github.com/dadul96/TcpConnection_Lib/releases/tag/v2.0.0) | <- | [v2.0.0](https://github.com/dadul96/TCP_Server_Client_Tester/releases/tag/v2.0.0) | + ### Author **Daniel Duller** - [dadul96](https://github.com/dadul96) diff --git a/TCP_Server_Client_Tester/MainWindow.xaml.cs b/TCP_Server_Client_Tester/MainWindow.xaml.cs index 9bec1b5..fc71899 100644 --- a/TCP_Server_Client_Tester/MainWindow.xaml.cs +++ b/TCP_Server_Client_Tester/MainWindow.xaml.cs @@ -1,4 +1,7 @@ -using System; +//################# +//version: 2.0.0 +//################# +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -17,7 +20,7 @@ using System.Threading; using System.Net; using System.Net.Sockets; -using TcpConnection_Lib; +using TcpConnection_Lib; //lib-version 2.0.0 used namespace TCP_Server_Client_Tester { @@ -26,15 +29,19 @@ namespace TCP_Server_Client_Tester /// public partial class MainWindow : Window { - static readonly TcpConnection connection = new TcpConnection(); + static readonly TcpConnection serverConnection = new TcpConnection(); + static readonly TcpConnection clientConnection = new TcpConnection(); enum Tcp { LISTEN, + INITIAL_CONNECT, CONNECT, IDLE, SENDING, RECEIVING, + DISCONNECT, + RECONNECT, CLOSE } @@ -85,32 +92,44 @@ void TcpServerLoop() switch (serverState) { case Tcp.LISTEN: - if (connection.Listen(port)) + Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { - serverState = Tcp.CONNECT; - } - else - { - serverState = Tcp.CLOSE; - } - break; - - case Tcp.CONNECT: - if (connection.TcpIsConnected == true) + serverStartButton.IsEnabled = false; + serverSendButton1.IsEnabled = false; + serverSendButton2.IsEnabled = false; + serverTextboxBig.AppendText("Waiting for a client..." + Environment.NewLine); + serverTextboxBig.ScrollToEnd(); + })); + + if (serverConnection.TryListen(port, out string RemoteEndpointAddress)) { Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { - serverTextboxBig.AppendText("Connected to " + connection.RemoteEndpointAddress + Environment.NewLine); + serverStartButton.IsEnabled = true; + serverSendButton1.IsEnabled = true; + serverSendButton2.IsEnabled = true; + serverTextboxBig.AppendText("Connected to " + RemoteEndpointAddress + Environment.NewLine); serverTextboxBig.ScrollToEnd(); })); - tcpConnectedFlag = true; - serverState = Tcp.IDLE; + if (serverConnection.TryReadingData()) + { + tcpConnectedFlag = true; + serverState = Tcp.IDLE; + } + else + { + serverState = Tcp.DISCONNECT; + } + } + else + { + serverState = Tcp.DISCONNECT; } break; case Tcp.IDLE: - if (connection.TcpIsConnected == true) + if (serverConnection.TcpIsConnected == true) { if (sendingStringFilledFlag) { @@ -123,81 +142,79 @@ void TcpServerLoop() } else { - serverState = Tcp.CLOSE; + serverState = Tcp.DISCONNECT; } break; case Tcp.SENDING: - if (connection.TcpIsConnected == true) + if (serverConnection.TrySend(sendingString)) { - if (connection.Send(sendingString)) + Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { - Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => - { - serverTextboxBig.AppendText("Sending: " + sendingString + Environment.NewLine); - serverTextboxBig.ScrollToEnd(); - })); + serverTextboxBig.AppendText("Sending: " + sendingString + Environment.NewLine); + serverTextboxBig.ScrollToEnd(); + })); - sendingString = ""; - sendingStringFilledFlag = false; - serverState = Tcp.IDLE; - } - else - { - serverState = Tcp.CLOSE; - } + sendingString = ""; + sendingStringFilledFlag = false; + serverState = Tcp.IDLE; } else { - serverState = Tcp.CLOSE; + serverState = Tcp.DISCONNECT; } break; case Tcp.RECEIVING: - if (connection.TcpIsConnected == true) + receivingString = serverConnection.GetReceivedString(); + + if (receivingString != null) { - try + Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { - receivingString = connection.GetReceivedString(); + serverTextboxBig.AppendText("Receiving: " + receivingString + Environment.NewLine); + serverTextboxBig.ScrollToEnd(); + })); - if (receivingString != "") - { - Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => - { - serverTextboxBig.AppendText("Receiving: " + receivingString + Environment.NewLine); - serverTextboxBig.ScrollToEnd(); - })); - - receivingString = ""; - } - } - catch { } - serverState = Tcp.IDLE; + receivingString = null; } - else + serverState = Tcp.IDLE; + break; + + case Tcp.DISCONNECT: + sendingString = ""; + receivingString = ""; + sendingStringFilledFlag = false; + tcpConnectedFlag = false; + + Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { - serverState = Tcp.CLOSE; - } + serverStartButton.IsEnabled = false; + serverSendButton1.IsEnabled = false; + serverSendButton2.IsEnabled = false; + serverTextboxBig.AppendText("Problem occurred! Reconnecting..." + Environment.NewLine); + serverTextboxBig.ScrollToEnd(); + })); + + serverConnection.Disconnect(); + + serverState = Tcp.LISTEN; break; case Tcp.CLOSE: - try - { - sendingString = ""; - receivingString = ""; - sendingStringFilledFlag = false; - tcpConnectedFlag = false; + sendingString = ""; + receivingString = ""; + sendingStringFilledFlag = false; + tcpConnectedFlag = false; - Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => - { - serverTextboxBig.AppendText("Disconnected..." + Environment.NewLine); - serverTextboxBig.ScrollToEnd(); - })); + Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => + { + serverTextboxBig.AppendText("Disconnected..." + Environment.NewLine); + serverTextboxBig.ScrollToEnd(); + })); - connection.Dispose(); - } - catch { } + serverConnection.Dispose(); closeFlag = true; break; @@ -205,11 +222,12 @@ void TcpServerLoop() serverState = Tcp.CLOSE; break; } + Thread.Sleep(1); //for decreasing the CPU usage } } - catch + catch (Exception Ex) { - MessageBox.Show("Server loop error. Please restart the Program!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); + MessageBox.Show("PLEASE RESTART! TcpServerLoop-thread error: \n\n" + Ex, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } @@ -226,7 +244,7 @@ void TcpClientLoop() clientTextboxBig.Text = ""; })); - clientState = Tcp.CONNECT; + clientState = Tcp.INITIAL_CONNECT; while (!closeFlag) { @@ -237,108 +255,164 @@ void TcpClientLoop() switch (clientState) { - case Tcp.CONNECT: - if (connection.Connect(ip, port)) + case Tcp.INITIAL_CONNECT: + Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => + { + clientStartButton.IsEnabled = false; + clientSendButton1.IsEnabled = false; + clientSendButton2.IsEnabled = false; + clientTextboxBig.AppendText("Trying to connect..." + Environment.NewLine); + clientTextboxBig.ScrollToEnd(); + })); + + if (clientConnection.TryConnect(ip, port)) { Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { + clientStartButton.IsEnabled = true; + clientSendButton1.IsEnabled = true; + clientSendButton2.IsEnabled = true; clientTextboxBig.AppendText("Connected" + Environment.NewLine); clientTextboxBig.ScrollToEnd(); })); - tcpConnectedFlag = true; - clientState = Tcp.IDLE; - } - break; - - case Tcp.IDLE: - if (connection.TcpIsConnected == true) - { - if (sendingStringFilledFlag) + if (clientConnection.TryReadingData()) { - clientState = Tcp.SENDING; + tcpConnectedFlag = true; + clientState = Tcp.IDLE; } else { - clientState = Tcp.RECEIVING; + clientState = Tcp.RECONNECT; } } else { - clientState = Tcp.CLOSE; + clientState = Tcp.RECONNECT; } break; - case Tcp.SENDING: - if (connection.TcpIsConnected == true) + case Tcp.CONNECT: + if (clientConnection.TryConnect(ip, port)) { - if (connection.Send(sendingString)) + if (clientConnection.TryReadingData()) { Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { - clientTextboxBig.AppendText("Sending: " + sendingString + Environment.NewLine); + clientStartButton.IsEnabled = true; + clientSendButton1.IsEnabled = true; + clientSendButton2.IsEnabled = true; + clientTextboxBig.AppendText("Connected" + Environment.NewLine); clientTextboxBig.ScrollToEnd(); })); - sendingString = ""; - sendingStringFilledFlag = false; + tcpConnectedFlag = true; clientState = Tcp.IDLE; } else { - clientState = Tcp.CLOSE; + clientState = Tcp.DISCONNECT; } } else { - clientState = Tcp.CLOSE; + clientState = Tcp.DISCONNECT; } break; - case Tcp.RECEIVING: - if (connection.TcpIsConnected == true) + case Tcp.IDLE: + if (clientConnection.TcpIsConnected == true) { - try + if (sendingStringFilledFlag) { - receivingString = connection.GetReceivedString(); - - if (receivingString != "") - { - Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => - { - clientTextboxBig.AppendText("Receiving: " + receivingString + Environment.NewLine); - clientTextboxBig.ScrollToEnd(); - })); - - receivingString = ""; - } + clientState = Tcp.SENDING; + } + else + { + clientState = Tcp.RECEIVING; } - catch { } - clientState = Tcp.IDLE; } else { - clientState = Tcp.CLOSE; + clientState = Tcp.RECONNECT; } + break; - case Tcp.CLOSE: - try + case Tcp.SENDING: + if (clientConnection.TrySend(sendingString)) { + Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => + { + clientTextboxBig.AppendText("Sending: " + sendingString + Environment.NewLine); + clientTextboxBig.ScrollToEnd(); + })); + sendingString = ""; - receivingString = ""; sendingStringFilledFlag = false; - tcpConnectedFlag = false; + clientState = Tcp.IDLE; + } + else + { + clientState = Tcp.RECONNECT; + } + break; + + case Tcp.RECEIVING: + receivingString = clientConnection.GetReceivedString(); + if (receivingString != null) + { Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { - serverTextboxBig.AppendText("Disconnected..." + Environment.NewLine); - serverTextboxBig.ScrollToEnd(); + clientTextboxBig.AppendText("Receiving: " + receivingString + Environment.NewLine); + clientTextboxBig.ScrollToEnd(); })); - connection.Dispose(); + receivingString = null; } - catch { } + clientState = Tcp.IDLE; + break; + + case Tcp.RECONNECT: + sendingString = ""; + receivingString = ""; + sendingStringFilledFlag = false; + tcpConnectedFlag = false; + + Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => + { + clientStartButton.IsEnabled = false; + clientSendButton1.IsEnabled = false; + clientSendButton2.IsEnabled = false; + clientTextboxBig.AppendText("Problem occurred! Reconnecting..." + Environment.NewLine); + clientTextboxBig.ScrollToEnd(); + })); + + clientConnection.Disconnect(); + + clientState = Tcp.CONNECT; + break; + + case Tcp.DISCONNECT: + clientConnection.Disconnect(); + + clientState = Tcp.CONNECT; + break; + + case Tcp.CLOSE: + sendingString = ""; + receivingString = ""; + sendingStringFilledFlag = false; + tcpConnectedFlag = false; + + Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => + { + clientTextboxBig.AppendText("Disconnected..." + Environment.NewLine); + clientTextboxBig.ScrollToEnd(); + })); + + clientConnection.Disconnect(); closeFlag = true; break; @@ -346,11 +420,12 @@ void TcpClientLoop() clientState = Tcp.CLOSE; break; } + Thread.Sleep(1); //for decreasing the CPU usage } } - catch + catch (Exception Ex) { - MessageBox.Show("Client loop error. Please restart the Program!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); + MessageBox.Show("PLEASE RESTART! TcpClientLoop-thread error: \n\n" + Ex, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } @@ -443,7 +518,10 @@ private void serverSendButton1_Click(object sender, RoutedEventArgs e) if (!sendingStringFilledFlag && tcpConnectedFlag) { sendingString = serverSendTextbox1.Text; - sendingStringFilledFlag = true; + if (sendingString != null && sendingString != "") + { + sendingStringFilledFlag = true; + } } } @@ -452,7 +530,10 @@ private void serverSendButton2_Click(object sender, RoutedEventArgs e) if (!sendingStringFilledFlag && tcpConnectedFlag) { sendingString = serverSendTextbox2.Text; - sendingStringFilledFlag = true; + if (sendingString != null && sendingString != "") + { + sendingStringFilledFlag = true; + } } } @@ -461,7 +542,10 @@ private void clientSendButton1_Click(object sender, RoutedEventArgs e) if (!sendingStringFilledFlag && tcpConnectedFlag) { sendingString = clientSendTextbox1.Text; - sendingStringFilledFlag = true; + if (sendingString != null && sendingString != "") + { + sendingStringFilledFlag = true; + } } } @@ -470,7 +554,10 @@ private void clientSendButton2_Click(object sender, RoutedEventArgs e) if (!sendingStringFilledFlag && tcpConnectedFlag) { sendingString = clientSendTextbox2.Text; - sendingStringFilledFlag = true; + if (sendingString != null && sendingString != "") + { + sendingStringFilledFlag = true; + } } } } diff --git a/screenshot.gif b/screenshot.gif index 22e7ac6..fdefb92 100644 Binary files a/screenshot.gif and b/screenshot.gif differ