C# Network Program hangs

I got a question about my first C# networking program. I have a server and a client program, and the server just sends out numbers to the client. I don't just want the client to receive but to send also. For example, to create commands to send only odd/even numbers, or numbers in some range.

My server program is multithreaded and it goes like this:

while true
{
   SocketComm socObj = new SocketComm();
   sockObj.dummySocket = tlsserver.AcceptSocket();
   Thread thd = new Thread(new ThreadStart(sockObj.ThreadProc));
   thr.IsBackground = true;
   thr.Start();
   thr.Sleep(750);
}

public void ThreadProc()
{
   NetworkStream serverSockStream = new NetworkStream(mySocket);
   StreamReader serverStreamReader = new StreamReader(serverSockStream);
   StreamWriter serverStreamWriter = new StreamWriter(serverSockStream);

   Random random = new Random();

   while(true)
   {
     int randomNumber.Next(1000);
   
     try
     {
        // This works fine and sends the numbers to client like expected
        serverStreamWriter.WriteLine(randomNumber);
        // Client times out when I add this line:
       //  string returndata = serverStreamReader.Readline();
     }
     catch
     { break; }


In the client, I connect to the server like this:

   private void ConnectToServer(string ServerIP, int ServerPort)
        {
            // Create a new instance of a TCP client
            tcpClient = new TcpClient();
            try
            {
                // Connect the TCP client to the specified IP and port
                tcpClient.Connect(ServerIP, ServerPort);
                NetworkStream clientSockStream = tcpClient.GetStream();
                clientStreamReader = new StreamReader(clientSockStream);
                clientStreamWriter = new StreamWriter(clientSockStream);

and when I click 'Get Number" in the form, these are the important lines:
...
string returndata = clientStreamReader.ReadLine();
lbparam.Items.Add(returndata);
...

And when I want to send something to the server, I have this line:
clientStreamWriter.WriteLine(myCommandToServer);


Now, the problem is, the client hangs if I add this line to read in the while loop of ThreadProc in the server:

string returndata = serverStreamReader.Readline();

Why does the client hang? How do I get the server to also read from a stream?

Thanks,
Igor
igor92128Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PockyMasterCommented:
I think it would be easier to use asynchronous reading on your networkStream instead.
I don't believe the ReadLine() is a blocking call, so you cannot be sure there is any data there when your code hits that point.

Have a read on the NetworkStream's BeginRead
http://msdn2.microsoft.com/en-us/library/system.net.sockets.networkstream.beginread.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.