Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make/simulate persistent TCP connection?

Tags:

c#

.net

wcf

It looks like WCF TCP connections are not persistent. First ping reply takes a while but subsequent processes take less time. After a while it takes long again -another re-connection?

SERVER> Started on net.tcp://0.0.0.0:999

CLIENT> Connection created to net.tcp://localhost:999 //Not a real connection, ready to connect

CLIENT> Ping reply in 1s163ms //First connection

CLIENT> Ping reply in 22ms //Already connected

CLIENT> Ping reply in 26ms

CLIENT> Ping reply in 24ms

CLIENT> Ping reply in 325ms //Re-connected

CLIENT> Ping reply in 19ms

CLIENT> Ping reply in 767ms //Re-connected

If it's true, what is the idle time value for a tcp connection before it will be disconnected? I need to keep the connection alive.

Update Modified code:

NetTcpBinding tcpBind = new NetTcpBinding();
tcpBind.ReliableSession.Enabled = true;
tcpBind.ReliableSession.Ordered = true;
tcpBind.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);

ServiceHost svh = new ServiceHost(typeof(ServiceImplementation));
svh.AddServiceEndpoint(
    typeof(WCFSimple.Contract.IService),
    //new NetTcpBinding(),
    tcpBind,
    String.Format("net.tcp://{0}:{1}", ip, port));
svh.Open();

Now I got another error:

The action http://tempuri.org/IService/Pong is not supported by this endpoint. Only WS-ReliableMessaging February 2005 messages are processed by this endpoint.

Update I modified only server side and it caused the error. Then I modified client side's TCP as reliable messaging.

like image 924
Nime Cloud Avatar asked Aug 13 '11 20:08

Nime Cloud


People also ask

How can you preserve a TCP connection of longer time when there is no data exchange?

No data is sent over the network to maintain TCP connections. You can send a network layer keep-alive simply by send()ing a zero byte packet from either peer or enable socket options to have the operating system periodically send them for you.

What is persistent TCP?

Persistent connections provide a mechanism by which a client and a server can signal the close of a TCP connection. This signaling takes place using the Connection header field (section 14.10). Once a close has been signaled, the client MUST NOT send any more requests on that connection.

How do I persist TCP connection?

To maintain a persistent connection, TCP keep-alive packets are sent to prevent the connection from timing out. An open connection is faster for frequent data exchanges. Communications overhead is saved by leaving a connection open rather than opening and closing sessions for each request.

How do I enable persistent connections?

Click Action > Configure Agents > Configure Persistent Connection. Note: The agent must be one for which Agent Manager appears in the Event Manager column. Do one of the following in the Configure Persistent Connection window: To enable the connection, select Enable Persistent Connections for selected agents.


1 Answers

I don't think Ping is a good tool to test a TCP connection. Because Ping uses ICMP as its underlying protocol instead of TCP.

I suggest you create a WCF client, connect to the service and then check the TCP connection via some network sniffering tools or simply use netstat for quick check.

like image 102
Eric Fan Avatar answered Oct 03 '22 20:10

Eric Fan