Click here to Skip to main content
15,512,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to do the following logic on my WPF Desktop application that will;

This is the TCP server program that echoes your sent data back to you.

Start echotool as follows:<br>
echotool /p tcp /s 12345


It must first have two buttons one start, stop, processed. The status bar must show these as details to the user.

What I have tried:

Client code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    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.IO;
    using System.Threading;
    using System.Net.NetworkInformation;
    using WPFclient;

    namespace WPFClient
      {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        protected ClientProp WpfClient_;
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Btncnnct1_Click(object sender, RoutedEventArgs e)
        {
            TxtBoxIp1.Text = "192.168.0.150";
            //TcpClient client = new TcpClient();
            this.WpfClient_ = new ClientProp(ipAddress: (this.TxtBoxIp1.Text), portNumber: 138);
            this.WpfClient_.BeginPrepareTCPClient();
            WpfClient_.Connect(IPAddress.Parse(this.TxtBoxIp1.Text), port: 137);
            TxtBoxInfo1.Text = "connected" + Environment.NewLine;
        }
        private void BtnSnd_Click(object sender, RoutedEventArgs e)
        {
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(TxtBoxMsg1.Text);
            NetworkStream networkStream = WpfClient_.GetStream();
            StreamWriter stream = new StreamWriter(TxtBoxMsg1.Text);
            stream.Write(TxtBoxMsg1.Text, 0, TxtBoxMsg1.Text.Length);
            TxtBoxInfo1.Text += $"Server : {TxtBoxMsg1.Text}{Environment.NewLine}";
          
        }


Server Code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    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.IO;

    namespace ServerWPF
     {
     /// <summary>
     /// Interaction logic for MainWindow.xaml
     /// </summary>
    public partial class MainWindow : Window
     {
        private string line;

        public MainWindow()
        {
            InitializeComponent();
        }    
        private void BtnStrt_Click(object sender, RoutedEventArgs e)
        {
            TxtBoxIp.Text = "192.168.0.150";
            TcpListener server = new TcpListener(IPAddress.Parse(TxtBoxIp.Text), port: 137);
            server.Start();
            TcpClient client = server.AcceptTcpClient();
            TxtBoxInfo.Text = "Server Connected" + Environment.NewLine;
            BtnStrt.IsEnabled = false;
            BtnSend.IsEnabled = true;
            NetworkStream stream = client.GetStream();
            StreamReader reader = new StreamReader(client.GetStream(), Encoding.UTF8);         
        }
        private void BtnSend_Click(object sender, RoutedEventArgs e)
        {
        
        }
Posted
Comments
Graeme_Grant 2-Sep-22 23:50pm    
You know we are not code monkeys that write code... right? There is Fiver and other websites for that.

What is not working? What line? What is the exact error message... We are here to help once you have exhausted every avenue.
Dave Kreskowiak 2-Sep-22 23:56pm    
You haven't described any problem you're having or any questions, so it's pretty much impossible to give you an answer.
Gcobani Mkontwana 6-Sep-22 10:13am    
@Dave Kreskowiak and @Graeme_Grant, i want to have two buttons, one start button to start process for TCP, two stop button process to stop process for TCP and status that show all of these as details for user. That isStarted = timestamp when this echo process started , Processed = timestamp after deserialization into an object , Elapsed = milliseconds elapsed from Started to Processed)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900