WCF connection timeout when trying to connect to an unreachable host - c#

I set connection timeout the following way:
var binding = new NetTcpBinding(SecurityMode.None, true);
binding.OpenTimeout = TimeSpan.FromSeconds(5);
Then I try to connect to the service like this:
var client = new ServiceClient(new InstanceContext(eventClient), binding, address);
client.Subscribe();
Whenever the client attempts to connect to a reachable host but without the service running it will timeout in 5 seconds like I expect it to. When the client attempts to connect to an unreachable host it freezes for 15-20 seconds or so.
Is there any way to make it timeout faster?

Related

C# ClientWebSocket retry after connection error

Is there any way to know whether ClientWebSocket connection is closed/disconnected? I am using slack RTM api in C# and sometimes due to connectivity issues connection is closed from server, then I want to retry connection.
I am connecting to websocket server like this
var client = new ClientWebSocket();
client.ConnectAsync(url, token);

Console app SignalR client Disconnect Timeout

I am trying to set Disconnect Timeout to higher value from the default 30s.
All examples on web are more JS oriented.
var hubConnection = new HubConnection("http://localhost:8087");
var testHubProxy = hubConnection.CreateHubProxy("TestHub");
Error: System.TimeoutException: Couldn't reconnect within the configured timeout of 00:00:30, disconnecting.
This did not work:
GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(35);
Update:
It looks like DisconnectTimeout needs to be set on the server side!?
What is the reason for disallowing different clients to have different Disconnect Timeout?
Disconnect Timeout is configured on server-side. The main reasons could be as follows:
We know the server may take some N-time units to respond so that the all clients may be well aware.
The server should be pinging the clients for connection at regular times. So the server is aware of clients connection and can manage other hubs and eradicate the expired connections from its connection pool.
The client is not supposed to set disconnect timeout because it does not know when could it shutdown e.g. the internet switched off accidentally on client side than the client is not able to tell server that I am not going to connect to you again. Yes but we have some events at client-side which tells it that it is not connected to the signalr hub anymore. Please see the reconnecting and disconnected events.
Summary:
Disconnect timeout is to inform the server that its client is not connected anymore even if it disconnects disgracefully .

SSH.NET > SSH client for port forwarding

I'm using SSH.NET for coding an GUI to create a connection to a VPS. My intention is to make a proxy server for playing online games.
I need to forward a port dynamically, to create a SOCKS Server and then, Proxifier (another program installed on my computer) can connect to this server.
When I use PuTTY, Proxifier works well, but when I use my software, it's not working.
This is my code:
using (client = new SshClient("VPS_IP", "USER", "PASSWORD"))
{
client.KeepAliveInterval = new TimeSpan(0, 0, 60);
client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 50);
port = new ForwardedPortDynamic("127.0.0.1", portNumber);
client.Connect();
client.AddForwardedPort(port);
port.Start();
if (client.IsConnected && port.IsStarted)
{
MessageBox.Show("Connected");
}
System.Threading.Thread.Sleep(20 * 1000);
}
And now, the log of Proxifier
[48:51] Testing Started.
Proxy Server
Address: 127.0.0.1:port
Protocol: SOCKS 5
Authentication: NO
[48:51] Starting: Test 1: Connection to the Proxy Server
[48:51] IP Address: 127.0.0.1
[48:51] Connection established
[48:51] Test passed.
[48:51] Starting: Test 2: Connection through the Proxy Server
[48:51] Authentication was successful.
[48:51] Error : connection to the proxy server was closed unexpectedly.
[48:51] Test failed.
[48:51] Testing Finished.
To debug problems with forwarded port, start by handling ForwardedPort.Exception event.
Once you find out what exception, if any, is raised, we can help you further.

System.TimeoutException in WCF service C#

I have a problem with my WCF service. I'm getting thw following exception after the program runs three hours perfectly.
System.TimeoutException: The request channel timed out while waiting for a reply after 00:00:59.9843998. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout
I do not think that increasing the timeout would be a solution. I think the just would throw the exception a couple of minutes later. Maybe I have to change any settings on my listener server. Could it be that my machine close the port after a certain period of time?
I call my service like that:
IServices service = null;
service = ChannelFactory<IServices>.CreateChannel(
new BasicHttpBinding(),
new EndpointAddress(
port));
result = service.addAppointments(appList);
My listener:
ServiceHost host = new ServiceHost(service);
host.AddServiceEndpoint(typeof(
IServices),
new BasicHttpBinding(), port);
host.Open();
Thats all. No further settings were required up to now.
You can add through config the tracer for WCF. This will help you to find out where the problem is.
http://msdn.microsoft.com/en-us/library/ms733025.aspx
http://msdn.microsoft.com/en-us/library/ms732023.aspx

How to make/simulate persistent TCP connection?

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.
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.
Normally, WCF connections are transient, though there is some connection pooling that goes on in the background.
Using net.tcp as the transport, persistent (long term) connections are possible. The code you've posted is one way to achieve that.
For more information, and some alternatives, check out What You Need To Know About One-Way Calls, Callbacks, And Events, an MSDN magazine article from October 2006.
Also of use are the IsInitiating and IsTerminating properties available on the OperationContract attribute.

Categories