why my thread not working continuously

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;


namespace MultiClientServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        private void button1_Click(object sender, EventArgs e)
        {
            TcpListener listner = new TcpListener(new IPEndPoint(IPAddress.Loopback, 8000));
            listner.Start();
            textBox1.Text += "Started TCP Server"+Environment.NewLine;
            listner.BeginAcceptTcpClient(new AsyncCallback(Accept), listner);
        }

        void Accept(IAsyncResult result)
        {
            textBox1.Invoke(new MethodInvoker(delegate()
                {
                    textBox1.Text += "Client Request Arrived" + Environment.NewLine;
                }));
            TcpListener listner1 = (TcpListener)result.AsyncState;
            TcpClient client = listner1.EndAcceptTcpClient(result);
            textBox1.Invoke(new MethodInvoker(delegate()
                {
                    textBox1.Text += "Client Request Approved" + Environment.NewLine;
                }));
            Thread th = new Thread(new ParameterizedThreadStart(ContinueRcv));
            th.Start(client);
        }

        void ContinueRcv(object obj)
        {
            TcpClient client = (TcpClient)obj;
            StreamReader sr = new StreamReader(client.GetStream());
            textBox1.Invoke(new MethodInvoker(delegate()
                {
                    textBox1.Text += sr.ReadLine() + Environment.NewLine;
                }));
        }
    }
}

i was trying to make this app so that when a client is connected than a new thread will b created and it will b in continue receive.. bt it is not unfortunately.. plz give me solution using this code.. mean it is not a class requirement or anything.. i jux want to know how to do this in this or any related way..

Answers


Thread is not something that is being called continuously, your code in the thread block need to be called continuously, since your thread is responsible of calling ContinueRcv the thread ends with the end of this method,

If you want to continuously receive the data from Stream you need to call StreamReader's ReadLine() in some infinite loop,

void ContinueRcv(object obj)
{
    TcpClient client = (TcpClient)obj;
    StreamReader sr = new StreamReader(client.GetStream());
    while (true) 
    {
        if ( !connection ) { // when connection closed, abort, terminated
            break;
        }
        msg = sr.ReadLine();
        textBox1.Invoke(new MethodInvoker(delegate()
        {
            textBox1.Text += msg  + Environment.NewLine;
        }));
    }
}

Remember to break the loop when connection closed,


Need Your Help

Difference between cuda.h, cuda_runtime.h, cuda_runtime_api.h

cuda header-files

I'm starting to program with CUDA, and in some examples I find the include files cuda.h, cuda_runtime.h and cuda_runtime_api.h included in the code. Can someone explain to me the difference between...

How do I put a dictionary in the datastore?

python google-app-engine dictionary google-cloud-datastore

Is there a good way to store a Python dictionary in the datastore? I want to do something like the following: