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;

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

   Random random = new Random();

     int randomNumber.Next(1000);
        // This works fine and sends the numbers to client like expected
        // Client times out when I add this line:
       //  string returndata = serverStreamReader.Readline();
     { 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();
                // 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();

And when I want to send something to the server, I have this line:

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?

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.

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

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

From novice to tech pro — start learning today.