TcpClient and Threading problems

Posted on 2006-10-19
Medium Priority
Last Modified: 2012-05-05
I can't figure out why this only wants to receive once I login.. when there is new data, it just sits there and doesn't output anything. Even if I put some Console.WriteLine("Hello") under while ((inputLine = reader.ReadLine()) != null), nothing get's written. I know there is data because I'm sending something to myself from another place that I'm connected to.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.IO;

namespace Test
    class Program
        static void Main(string[] args)
            Thread someThread = new Thread((ThreadStart)delegate
                TcpClient pClient = new TcpClient("server", 1000);
                using (NetworkStream stream = pClient.GetStream())
                using (StreamReader reader = new StreamReader(stream))
                using (StreamWriter writer = new StreamWriter(stream))
                    writer.AutoFlush = true;
                    string[] lines =
                        new string[] {
                    foreach (string line in lines)
                        byte[] byteData = new byte[line.Length];
                        byteData = Encoding.ASCII.GetBytes(line);
                        Console.WriteLine("Sending string: " + Encoding.ASCII.GetString(byteData));

                        string inputLine;
                        // One reads once.. ?
                        while ((inputLine = reader.ReadLine()) != null)
                            Console.WriteLine("Received: " + inputLine);
                    catch (IOException ioE)
                        Console.WriteLine("IOException: " + ioE.Message);

Question by:valvet
LVL 13

Accepted Solution

joechina earned 1000 total points
ID: 17773738
I believe the problem is here:
                        while ((inputLine = reader.ReadLine()) != null)
                            Console.WriteLine("Received: " + inputLine);

If there is nothing to read(could be before you send anything), the thread just ended.

Can you try
     inputLine = reader.ReadLine();
     if (inputLine == null)
     else if (inputLine == "SomeEndFlag")
         Console.WriteLine("Received: " + inputLine);


Author Comment

ID: 17775069
Well, the thread doesn't exit because I never receive "Joined..." in my console. It simply just sits there. I've made another while around the if inputline == reader.ReadLine()) != null which contains while (Client.Connected).. that should work just as well as while (true).

Guess I have a weird sense of how threads are supposed to work.. I thought by launching a thread, it would be a loop itself and simply run until I shut down the thread or interrupt it.

Thank you.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question