Original Post
I'm attempting to write a P2P network with a binary tree structure (haven't decided yet if I will make it a B-tree or not). Network programming is not something I've done a lot with, so maybe I'm missing something basic here. Anyway, each node in the network creates a listener for 2 client connections and may or may not connect to a parent node. Really simple, binary tree. When a client connects to a parent node, the parent node is supposed to immediately send it's status, followed by receiving the client's status. Naturally, the client expects it in this order as well, receive the parent node's status, then send its own status. Unfortunately, it seems the client is missing the server's status. The server will send its status, and then proceed to waiting for the client's status, but the client will be stuck waiting for the server's status. okay, some code. when a parent node accepts a connection (i is the index of the first open slot, there are only two. It's done in a for loop in case I ever want to make this an n-ary tree): when a client connects to a parent (input is in the form "address:port")
Console.WriteLine("Accepting connection #{0}", i);
connClient = connServer.AcceptTcpClient();
NetworkStream stream = connClient.GetStream();
rdrClient = new StreamReader(stream);
wrtClient = new StreamWriter(stream);
Console.WriteLine("Sending server information");
wrtClient.Write("[name {0}]", userName);
wrtClient.Flush();
Console.WriteLine("Waiting for response from client");
strClient = rdrClient.ReadLine(); // <--- server stalls here
connectionMade = true;
Console.WriteLine("Connection made.");
string[] parts = input.Split(":".ToCharArray());
Console.WriteLine("Connecting to {0} on port {1}.", parts[0], parts[1]);
connClient[0] = new TcpClient(parts[0], int.Parse(parts[1]));
NetworkStream stream = connClient[0].GetStream();
rdrClient[0] = new StreamReader(stream);
wrtClient[0] = new StreamWriter(stream);
Console.WriteLine("Waiting for response from server.");
string temp = rdrClient[0].ReadLine(); // <--- client stalls here
Console.WriteLine("Received message from server:\"{0}\"", temp);
//snipped some special handling in case the parent node is full
strClient[0] = temp.Substring(temp.IndexOf(" ")+1, temp.IndexOf("]"));
Console.WriteLine("Connection made, server named \"{0}\".", strClient[0]);
wrtClient[0].Write(userName);
wrtClient[0].Flush();
return true;