How to create a TCP server program in WPF using C#?

Viewed 37

I am struggling to create a TCP server program in WPF using C#. The program must Serialize the object using a Json serializer

  • Send the json string using a TCP socket. I have an exe file which will echo back all received data. How do i implement it on my current code and how do i create status bar that must show the messages received from TCP server program? The code below works just need to implement these requirement from it.

    //Back end

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.Threading.Tasks;
     using System.Windows;
     using System.Windows.Controls;
     using System.Windows.Data;
     using System.Windows.Documents;
     using System.Windows.Input;
     using System.Windows.Media;
     using System.Windows.Media.Imaging;
     using System.Windows.Navigation;
     using System.Windows.Shapes;
     using System.Windows.Threading;
     using System.Diagnostics;
     using System.Timers;
     using System.ComponentModel;
    
     namespace PingApplication
     {
         /// <summary>
         /// Interaction logic for MainWindow.xaml
         /// </summary>
         public partial class WpfTimerWindow : INotifyPropertyChanged
         {
             public event PropertyChangedEventHandler? PropertyChanged;
    
             private readonly Timer timer = new Timer();
    
             private readonly Stopwatch stopwatch = new Stopwatch();
    
             public DateTime Now => DateTime.Now;
    
             public TimeSpan Elasped => stopwatch.Elapsed;
             public WpfTimerWindow()
             {
                 //InitializeComponent();
                 timer.Interval = 50;
                 timer.Elapsed += OnTick;
                 timer.Start();
                 stopwatch.Start();
             }
    
             private void OnTick(object? sender, ElapsedEventArgs e)
             {
                 if (PropertyChanged is PropertyChangedEventHandler propertyChanged)
                 {
                     propertyChanged(this, NowArgs);
                     propertyChanged(this, ElaspedArgs);
                 }
             }
    
             public static PropertyChangedEventArgs NowArgs { get; } = new PropertyChangedEventArgs(nameof(Now));
             public static PropertyChangedEventArgs ElaspedArgs { get; } = new PropertyChangedEventArgs(nameof(Elasped));
    
             public static RoutedUICommand StartCommand { get; } = new RoutedUICommand("Timer Start", "TimerStart", typeof(WpfTimerWindow));
    
             public static RoutedUICommand ResetCommand { get; } = new RoutedUICommand("Timer Stop", "TimerStop", typeof(WpfTimerWindow));
    
             public static ExecutedRoutedEventHandler ExecuteCommand { get; } = (_, e) =>
             {
                 if (e.Parameter is WpfTimerWindow timer)
                 {
                     if (e.Command == StartCommand)
                     {
                         timer.stopwatch.Start();
                     }
                     else if (e.Command == ResetCommand)
                     {
                         timer.stopwatch.Stop();
                     }
    
    
                     else return;
                     timer.OnTick(null, null);
                 }
             };
             public static CanExecuteRoutedEventHandler CanExecuteCommand { get; } = (_, e) =>
             {
                 if (e.Parameter is WpfTimerWindow timer)
                 {
                     if (e.Command == StartCommand)
                     {
                         e.CanExecute = !timer.stopwatch.IsRunning;
                     }
                     else if (e.Command == ResetCommand)
                     {
                         e.CanExecute = timer.stopwatch.IsRunning;
                     }
    
                 }
             };
    
             public static WpfTimerWindow Default { get; } = new WpfTimerWindow();
         }
     }
    

    // Front end

     <Window x:Class="PingApplication.MainWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:PingApplication"
             mc:Ignorable="d"
             Title="WpfTimerWindow" Height="450" Width="800">
    
         <Window.CommandBindings>
             <CommandBinding Command="{x:Static local:WpfTimerWindow.StartCommand}"
                             Executed="{x:Static local:WpfTimerWindow.ExecuteCommand}"
                             CanExecute="{x:Static local:WpfTimerWindow.CanExecuteCommand}"/>
             <CommandBinding Command="{x:Static local:WpfTimerWindow.ResetCommand}"
                             Executed="{x:Static local:WpfTimerWindow.ExecuteCommand}"
                             CanExecute="{x:Static local:WpfTimerWindow.CanExecuteCommand}"/>
    
    
         </Window.CommandBindings>
         <Grid>
             <Grid.RowDefinitions>
                 <RowDefinition Height="*" />
                 <RowDefinition Height="Auto" />
             </Grid.RowDefinitions>
             <StackPanel Grid.Row="1" Orientation="Horizontal">
                 <Button Padding="15 5" Margin="5" Content="Start"
                         Command="{x:Static local:WpfTimerWindow.StartCommand}"
                         CommandParameter="{x:Static local:WpfTimerWindow.Default}"/>
    
    
                 <Button Padding="15 5" Margin="5" Content="Stop"
                         Command="{x:Static local:WpfTimerWindow.ResetCommand}"
                         CommandParameter="{x:Static local:WpfTimerWindow.Default}"/>
             </StackPanel>
             <StackPanel>
                 <TextBlock FontSize="48" HorizontalAlignment="Center" VerticalAlignment="Center" 
                        Text="{Binding Elasped, Source={x:Static local:WpfTimerWindow.Default}}"/>
                 <TextBlock FontSize="48" HorizontalAlignment="Center" VerticalAlignment="Center" 
                        Text="{Binding Now, Source={x:Static local:WpfTimerWindow.Default}, StringFormat=T}"/>
             </StackPanel>
         </Grid>
     </Window>
    
0 Answers
Related