Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TcpClient and Threading problems

Posted on 2006-10-19
2
Medium Priority
?
421 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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

971 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