Solved

TcpClient and Threading problems

Posted on 2006-10-19
2
407 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

628 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