Start New Discussion within our Software Development Community

Hi all,

I am trying to build a ui application with thread for tcp server. I managed to build the tcp server itself using console (giving a welcome message when I telnet it), but when I copy the exact source code in WPF , I can not telnet it. The form load just fine, but somehow the server is not working *host actively refused connection when I telnet it*

below is the source code that I tried to make

namespace WpfApplication7
{
     public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            Thread newThread = new Thread(new ThreadStart(Counter));
        }
    
         public static void Counter()
            {
            int recv;
            byte[] data = new byte[1024];
            TcpListener newsock = new TcpListener(3000);
            newsock.Start();
            TcpClient client = newsock.AcceptTcpClient();
            NetworkStream ns = client.GetStream();
            string welcome = "Welcome telnet client";
            data = Encoding.ASCII.GetBytes(welcome);
            ns.Write(data, 0, data.Length);

            while (true)
            {
                data = new byte[1024];
                recv = ns.Read(data, 0, data.Length);
                if (recv == 0)
                    break;
            }

            ns.Close();
            client.Close();
            newsock.Stop();
            }



}


}

I tried several combinations to initialize the thread object, but it does not work. From some books that I read, I am supposed to put initialize thread in the main, but I think that I should put it under window 1 method. Am I having a wrong concept? How can I get to thread for this server to work?

Please guide me in this matter.

thank you

agh.. stupid me. and it took me days to realize this..
I only need to add newThread.Start(); in the window method..

This question has already been answered. Start a new discussion instead.