Search code examples
c#p2ptcpclienttcplistener

how to make peer to peer confirmed communication C#


In my company we have an automatic station that run Iperf and other software across a LAN , Now I got the assignment to improve the communication . I'm pretty novice in C# and need some help . what we have now is : simple listener for other messages

public void Connect(int port, string Host)
    {
        if (!Connected)
        {
            Connected = true;
            SetIP(port, Host);
            t1 = new Thread(new ThreadStart(CreateListener));
            t1.Name = "Listener Thread";
            t1.Priority = ThreadPriority.AboveNormal;
            t1.Start();
        }

public void Disconnect()
{
    if (Connected)
    {
        t1.Abort();
        peerListener.Stop();
        Connected = false;
    }

}

    private void CreateListener()
    {
        try
        {
            int iLength = 0;
            Socket tc = null;
            peerListener = new TcpListener(IPAddress.Any, port);
            peerListener.Start();
            while (true)
            {
                if (!peerListener.Pending())
                {
                  //Read data
                }
            }
         }catch { }
     }

and a messenger :

    private void CreateClient(object message)
    {
        try
        {
            peerClient = new TcpClient();
            peerClient.Connect(host, port);


            netStream = peerClient.GetStream();
            StreamWriter sw = new StreamWriter(netStream);
            sw.Write((string)message);
            //sw.Write((string)messa
            sw.Flush();

            netStream.Close();
            peerClient.Close();
        }
        catch
        {
        }
    }

    internal void Write(string message)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(CreateClient), message);
    }

what we do Is to send a Simple Text message , and parse via _TextChanged event the screen for the text and DO SOMETHING

So far so good , what I'm searching is a way to confirm that once a message is being sent , I can confirm that the application in the other side got the message .

It all works with TcpListener so I'm pretty sure there is a way to sample it and get flag or something that will give me what I want

thank's in advance


Solution

  • When you send data with TCP you are not guaranteed delivery at first. Delivery can be delayed or not happen at all if someone pulls the cable. This is a major performance optimization.

    You can achieve guaranteed delivery in two ways:

    1. Call Shutdown(Send). This sends everything and waits for confirmation. You should always Shutdown sockets before closing for this reason.
    2. Make the remote party reply to you with an acknowledgement. You must modify the protocol to support this.

    To implement (1) it is enough to make the receiver send a single zero byte after it has received the message. The sender must read this byte. Only when it has been read is it known that the message was received.

    I see a lot of error swallowing in your code. This is very worrying to me. If an error happens you'll never find out. Maybe the code snippet is just abridged.

    Also, do not abort threads.