I have 2 seperate program instances running on different computers. They are connected to each other other via a TcpClient (well 2 connections, an ingoing and outgoing, I am aware a TcpClient allows messages both ways, but for code simplicity it is easier to establish 2 connections).
Both are sending a lot of messages, and will be sending more data then there is in the buffer. Both threads run through a cycle of reading then writing.
ReceiveIncomingMessagesFromTcpClient()
ParseAndDeliverIncomingMessagesInternaly()
CollectNewOutgoingMessagesInternaly()
DeliverOutgoingMessagesViaTcpClient()
My program seems to run fine, and then stops sending messages, so I fear that the reason for this is that both are trying to Write Outgoing messages to their respective streams, both streams are full, and as nothing is there to empty the streams I have a deadlocked program.
If that is the case verify if there is room in the buffer before I start sending the message, and only send what is room for? (to avoid blocking a send, and due to this enter the deadlock?)
I have this function that sends the data, it can be called a lot of times in a row. The buffer size of the connection is the default 8kb.
So if this action gets repeated enough times in a row the 8kb buffer will get filled, and the stream.Write() is a blocking statement, until the other side pulls the data.
As the other side is a perfect mirror, if it has pulled a lot of data to send, both sides can be trying to send at the same time, causing the deadlock.
private void DeliverOutgoingMessagesViaTcpClient()
{
for(int i = 0; i < message.Count;i++)
{
//send request
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message[i]);
NetworkStream stream = connection.GetStream();
stream.Write(data, 0, data.Length);
}
}