I am incredibly confused what is going on here. I set the break points, and I just canโt understand. Basically, I have a client and a server. I want the client to send two separate rows of data. From entering breakpoints, I noticed that my client really fills both lines with the appropriate data. However, the server never sees the second line. Why is this happening and how can I fix it? Any help at all would be greatly appreciated! Below is my code:
Server:
private static void HandleClientComm(object client) { List<DatabaseFile> theDatabase = new List<DatabaseFile>(); TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); StringBuilder response = new StringBuilder(); byte[] message = new byte[4096]; int bytesRead; do { bytesRead = 0; try {
when I change this commented code to bytesRead = clientStream.Read (message, 0, 4096); I get an IOException error that reads as follows: Unable to write data to the transport connection: The existing connection was forcibly closed by the remote host. Therefore, I changed it to a while loop. How to get around this IOException and accept the second line?
ASCIIEncoding encoder = new ASCIIEncoding(); String file = encoder.GetString(message, 0, bytesRead); Menu.Insert(theDatabase, file); } catch (Exception) { // A socket error has occured break; } if (bytesRead == 0) { // The client has disconnected from the server break; } } while (clientStream.DataAvailable); // Release connections clientStream.Close(); tcpClient.Close(); }
Customer:
static void Main(string[] args) { TcpClient client = new TcpClient(); IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888); client.Connect(serverEndPoint); NetworkStream clientStream = client.GetStream(); NetworkStream clientStream2 = client.GetStream(); ASCIIEncoding encoder = new ASCIIEncoding(); ASCIIEncoding encoder2 = new ASCIIEncoding(); String text = System.IO.File.ReadAllText("FirstNames.txt"); String text2 = System.IO.File.ReadAllText("LastNames.txt"); byte[] buffer = encoder.GetBytes(text); Console.ReadLine(); clientStream.Write(buffer, 0, buffer.Length); clientStream.Flush(); Console.ReadLine(); byte[] buffer2 = encoder2.GetBytes(text2); clientStream2.Write(buffer2, 0, buffer2.Length); clientStream2.Flush(); Console.ReadLine(); } }
source share