Solved

TcpClient and Threading problems

Posted on 2006-10-19
2
397 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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now