Question

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..

Was it helpful?

Solution

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,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top