954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

wpf tcp thread problem

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

reza.adinata
Junior Poster in Training
54 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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

reza.adinata
Junior Poster in Training
54 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: