Freigeben über


Automatic port number generation

It has been an age since I updated this, the reason is quite simple - My team has been working hard to get Peer Channel ready to ship.

One of my favourite of the features we have added recently is now available in the latest CTP (download here) is the ability to tell the framework to automagically assign a random port to a PeerNode.

There are several benefits with automatically generating the port number:

  1. It is easy to run multiple instances of the application on a single machine
  2. It is harder to attack an application if it is not on a fixed port, because an application can't tell just by observing what port's are open on a machine what applications are running on it.

To automatically assign is really quite easy the NetPeerTcpBinding instance has a Port property if you set that to zero before opening the channel then the system will automatically generate a port randomly and set the peernode to listen on that port.

This can also be easily done in a configuration file for example to modify the sample in the post Is there anybody out there? (Online /Offline and the Peer channel)

The configuration file simply changes to this:

<?xml version="1.0" encoding="utf-8" ?>

<configuration xmlns="https://schemas.microsoft.com/.NetConfiguration/v2.0">

<system.serviceModel>

<client>

<endpoint name="HelloEndpoint"

address="net.p2p://hellomesh/hellomesh"

binding="netPeerTcpBinding"

bindingConfiguration="Binding1"

contract="HelloMesh.IHelloMesh">

</endpoint>

</client>

<bindings>

<netPeerTcpBinding>

<binding name="Binding1" port="0">

<security mode="None"/>

</binding>

</netPeerTcpBinding>

</bindings>

</system.serviceModel>

</configuration>

Now you can run as many instances as you want on each computer without recieving the Address in use exception that indicates that you are trying to use the same port in two applications.

 

Until next time