Note that there are some explanatory texts on larger screens.

plurals
  1. POC# TcpClient, Can following setup Deadlock (2 seperate programs communicating via TcpClient)
    primarykey
    data
    text
    <p>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).</p> <p>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.</p> <pre><code>ReceiveIncomingMessagesFromTcpClient() ParseAndDeliverIncomingMessagesInternaly() CollectNewOutgoingMessagesInternaly() DeliverOutgoingMessagesViaTcpClient() </code></pre> <p>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.</p> <p>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?)</p> <hr> <p>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.</p> <p>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.</p> <p>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.</p> <pre><code>private void DeliverOutgoingMessagesViaTcpClient() { for(int i = 0; i &lt; message.Count;i++) { //send request Byte[] data = System.Text.Encoding.ASCII.GetBytes(message[i]); NetworkStream stream = connection.GetStream(); stream.Write(data, 0, data.Length); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload