Solved

TcpClient and Threading problems

Posted on 2006-10-19
2
403 Views
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[] {
                            "LINEONE",
                            "LINETWO",
                            "LINETHREE"
                        };
                   
                    foreach (string line in lines)
                    {
                        byte[] byteData = new byte[line.Length];
                        byteData = Encoding.ASCII.GetBytes(line);
                        Console.WriteLine("Sending string: " + Encoding.ASCII.GetString(byteData));
                        writer.WriteLine(line);
                    }

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

            someThread.Start();
            someThread.Join();
            Console.WriteLine("Joined..");
        }
    }
}
0
Comment
Question by:valvet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
joechina earned 250 total points
ID: 17773738
I believe the problem is here:
                        while ((inputLine = reader.ReadLine()) != null)
                        {
                            Console.WriteLine("Received: " + inputLine);
                            Thread.Sleep(50);
                        }

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

Can you try
while(true)
{
     inputLine = reader.ReadLine();
     
     if (inputLine == null)
         Thread.Sleep(50);
     else if (inputLine == "SomeEndFlag")
         break;
     else
         Console.WriteLine("Received: " + inputLine);
}


0
 

Author Comment

by:valvet
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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