How does I set my TCP connection to KeepAlive with SipSorcery? As I can see in my traces, the connection between my client and server sends a [FIN, ACK] packet after 30 seconds.

I need my connection to be kept alive for SUBSCRIBE on phone events.

Some code example for my TCP Channel:

    SIPTCPChannel channel_Tcp = new SIPTCPChannel(localEndPoint);
    Transport = new SIPTransport(SIPDNSManager.ResolveSIPService, new SIPTransactionEngine());
    Transport.AddSIPChannel(channel_Tcp);

Everything works fine, until my connection get closed by FIN ACK.

有帮助吗?

解决方案

Normally sending a few null bytes out on the connection will be enough to keep the connection open. Most likely it's your own router closing the TCP connection and as long as it sees some sort of traffic going out it should leave it up.

static void Main(string[] args)
{
    SIPTCPChannel channel_Tcp = new SIPTCPChannel(new IPEndPoint(IPAddress.Any, 5060));
    var Transport = new SIPTransport(SIPDNSManager.ResolveSIPService, new SIPTransactionEngine());
    Transport.AddSIPChannel(channel_Tcp);

    IPEndPoint sipServerEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 5060);
    Timer keepAliveTimer = new Timer(30000);

    keepAliveTimer.Elapsed += (sender, e) =>
    {
        // If the TCP channel still has a connection to the SIP server socket then send a dummy keep-alive packet.
        if(channel_Tcp.IsConnectionEstablished(sipServerEndPoint))
        {
            channel_Tcp.Send(sipServerEndPoint, new byte[] { 0x00, 0x00, 0x00, 0x00 });
        }
        else
        {
            keepAliveTimer.Enabled = false;
        }
    };

    keepAliveTimer.Enabled = true;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top