エフェメラルポートの範囲を変更する

クライアントとサーバの間で TCP 通信を行う際、クライアント側が使用するポート番号をエフェメラルポートとかダイナミックポートとか呼んだりする。

Windows ではエフェメラルポートとして 1024 から 5000 までを使用するが、何らかの理由でポートの範囲を拡大したい場合、以下の技術情報を参考にレジストリの編集を行う必要がある。

When you try to connect from TCP ports greater than 5000 you receive the error 'WSAENOBUFS (10055)'

具体的には、HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters キー以下に MaxUserPort という DWORD 値を作成し、エフェメラルポートの最大値を指定すればよい。

  1. Start Registry Editor.
  2. Locate the following subkey in the registry, and then click Parameters:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
  3. On the Edit menu, click New, and then add the following registry entry:
    Value Name: MaxUserPort
    Value Type: DWORD
    Value data: 65534
    Valid Range: 5000-65534 (decimal)
    Default: 0x1388 (5000 decimal)
    Description: This parameter controls the maximum port number that is used when a program requests any available user port from the system. Typically , ephemeral (short-lived) ports are allocated between the values of 1024 and 5000 inclusive.
  4. Quit Registry Editor.

数値の範囲が 5000 から 65534 までと決まっているので注意。なお、範囲外の数値を入れた場合、5000 よりも小さい数値なら 5000、65534 よりも大きい数値なら 65534 が採用されるらしい。