Gcobani Mkontwana

Gcobani Mkontwana

  • 1k
  • 1.5k
  • 238k

Connection target to machine refused: WPF window application

Sep 25 2022 12:01 PM

Hi Team

I need some help, my problem i have two WPF windows application one is TCPClient that trying to connect to TCPServer. TCPServer must be able to receive data back as json, so can telnet using putty to this IP address but gets connection to target machine is refused, no firewall nor ports conflict. The issue is on line on my code await using NetworkStream stream = Client.GetStream();. How do i make two windows to connect to another both TCPClient and TCPServer?

127.0.0.1 port 12345

 

 

// TCPClient code

//Back end

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


namespace PingServerApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

           
        }

        // declaring some local variables and method for clicking buttons.

        bool eco = false;
        async void OnClick1(object sender, RoutedEventArgs e)
        {
            // add to datagrid
            dataGrid.Items.Clear();
            await TcpRequest();
        }

       

        void OnClick2(object sender, RoutedEventArgs e)
        {
            eco = false;
        }
        class Body { public Guid Id { get; set; } }

        // class for model

        public class TCPConnectionModel
        {
            public TimeOnly Started { get; set; }
            public TimeOnly Processed { get; set; }
            public TimeSpan Elapsed { get { return Processed - Started; } set { } }
        }

        // TCPRequest for body and serialize and deserialize json data

        async Task TcpRequest()
        {
            try
            {

                // initiate tcp
                IPHostEntry IpHostInfo = Dns.GetHostEntry("127.0.0.1");
                IPAddress IpAddress = IpHostInfo.AddressList[0];

                // initiate endPoint
                var endPoint = new IPEndPoint(IpAddress, 12345);

                // starting tcp client
                using TcpClient Client = new TcpClient();
                await Client.ConnectAsync(endPoint);
                await using NetworkStream stream = Client.GetStream();
                eco = true;
                while (eco)
                {

                    // start timer
                    TCPConnectionModel displayData = new() { Started = TimeOnly.FromDateTime(DateTime.Now) };

                    // initiate send data
                    Body requestBody = new() { Id = Guid.NewGuid() };

                    //initiate recieve data
                    var buffer = new byte[1_024];

                    // Json Serialize
                    string jsonString = JsonSerializer.Serialize(requestBody);

                    // tcp send
                    await stream.WriteAsync(Encoding.ASCII.GetBytes(jsonString));

                    // tcp recieve and decode
                    int received = await stream.ReadAsync(buffer);
                    var message = Encoding.UTF8.GetString(buffer, 0, received);

                    // json Deserialize
                    Body? recievedBody = JsonSerializer.Deserialize<Body>(message);

                    // stop timer
                    displayData.Processed = TimeOnly.FromDateTime(DateTime.Now);

                    // delay for 500ms
                    Thread.Sleep(500);

                    // add to datagrid
                    dataGrid.Items.Add(displayData);

                    // display result in status bar
                    resultveiw.Text = message;
                }
            }
            catch (Exception ex)
            {
                resultveiw.Text = ex.Message;
            }
        }
    }
}

 

// TCPServer 

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

namespace GUI
{
    /// <summary>
    /// Interaction logic for EchoWindowTest.xaml
    /// </summary>
    public partial class EchoWindowTest : Window
    {
        public EchoWindowTest()
        {
            InitializeComponent();
        }

        SimpleTcpServer server;


        public class DisplayData
        {
            public TimeOnly Started { get; set; }
            public TimeOnly Processed { get; set; }
            public TimeSpan Elapsed { get { return Processed - Started; } set { } }
        }
        private void MainWindow_Load(object sender, EventArgs e)
        {
            try
            {

                DisplayData displayData = new() { Started = TimeOnly.FromDateTime(DateTime.Now) };
                displayData.Processed = TimeOnly.FromDateTime(DateTime.Now);
                server = new SimpleTcpServer();
                server.Delimiter = 0 * 13;
                server.StringEncoder = Encoding.UTF8;
                server.DataReceived += Server_DataReceived;


                Thread.Sleep(500);
                dataGrid.Items.Add(displayData);

            }
            catch (Exception ex)
            {
                txtStatus.Text = ex.Message;
            }
        }
        private void Server_DataReceived(object sender, SimpleTCP.Message e)
        {
            txtStatus.Dispatcher.Invoke(new Action(delegate
            {
                txtStatus.Text += e.MessageString;

            }));
        }

        public void btnStart_Click(object sender, RoutedEventArgs e) /*Start button*/
        {
            txtStatus.Text += "Server starting ...";
            System.Net.IPAddress ip = new System.Net.IPAddress(long.Parse(txtHost.Text));
            server.Start(ip, Convert.ToInt32(txtPort.Text));

        }

        public void btnStop_Click(object sender, RoutedEventArgs e) /*Stop button*/
        {
            if (server.IsStarted)
                server.Stop();

        }

    }
}