Solved

TcpClient and Threading problems

Posted on 2006-10-19
2
402 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
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Google Directions API to Map URL -C#? 3 38
Change to file doesn't show up 16 55
Problem to Office 1 16
Coding for the first time 9 27
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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