1

I use SignalR hub as self-hosted on a windows service. There are winforms clients which talk to the hub. The server runs on port 80 and it is working fine.

What I'm wondering is, on which port the client sends or receives the messages?

As far as I know the port we leave the client machine and the port we arrive at server are or can be different ports. The machines my client will run on will be short on ports(most ports are closed), so I'm thinking I need to predefine the port my client leaves the machine too.

  1. Am I right thinking like this?
  2. How can I create the connection on client side that client port is always same?

Here is my client code:

IHubProxy _TextCopierHub;
HubConnection _HubConnection { get; set; }

public TextCopierHub ( string url )
{
    _HubConnection = new HubConnection(url);
    _TextCopierHub = _HubConnection.CreateHubProxy("TextCopierHub");
    _HubConnection.Credentials = CredentialCache.DefaultCredentials;

    if ( !_HubConnection.Start().Wait(10000) )
        throw new TimeoutException("Hub didn't start in 10 seconds");

    RegisterEvents();
}

note: I am not able to simulate the client environment, hence I'm not even sure this will be a problem or not, but I don't want to surprise when we install the clients since the customer has pointed this requirement out.

Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91

1 Answers1

2

Regular TCP/IP communications take place from a random (or at least not fixed) source port. This is because you cannot have multiple clientip:sourceport-serverip:destport combos, so the TCP implementation will keep a list of used local ports and pick an unused one when establishing a new connection.

You can choose a source port the .NET TcpClient constructor overload, but your application will run into problems if that sourceport is already used.

Since SignalR automatically detects whether to use Websockets, http long polling or other transport mechanisms, I don't think you can set the source port for that.

(Diagram for transport method selection here: How does SignalR decide which transport method to be used?).

Community
  • 1
  • 1
RasmusW
  • 3,355
  • 3
  • 28
  • 46
  • So you are saying it's automatic and can't be handled explicitly. Then, let's say all ports are closed on firewall except few, would it be able to find and select one of those available ports, and work? Or will it randomly fail? – Tolga Evcimen Feb 17 '17 at 10:24
  • @TolgaEvcimen I'm not sure how your firewall is configured, but usually firewalls are blocking incoming ports, and less commonly outgoing ones. In this case outgoing from your client to the server – Oscar Siauw Feb 17 '17 at 10:35
  • These clients are exceptional clients, so yes firewalls are configured strictly. – Tolga Evcimen Feb 17 '17 at 10:38
  • 1
    In that case, no standard Windows applications will work on their client PCs. You can't force all applications to use predefined source ports. But I guess you already know that. – RasmusW Feb 17 '17 at 11:48
  • No I didn't know that. Actually that's one part of the question. Can you direct me to a formal documentation that I can hold as a proof? – Tolga Evcimen Feb 17 '17 at 12:18
  • I don't know any formal documentation, but check out http://security.stackexchange.com/questions/55747/how-source-port-field-in-firewall-rule-is-used and this answer http://serverfault.com/a/627889. You can't make all apps use a specific source port, so restricting all outbound traffic by source port is awkward, difficult or impossible. – RasmusW Feb 20 '17 at 06:58