@@ -35,6 +35,11 @@ public partial class SocketClient:SocketBase
3535 /// </value>
3636 public EndPoint RemoteEndPoint => socket . RemoteEndPoint ;
3737
38+ /// <summary>
39+ /// Local Port
40+ /// </summary>
41+ public int LocalPort { get ; set ; } = 0 ;
42+
3843 /// <summary>
3944 /// Initializes a new instance of the <see cref="SocketClient"/> class.
4045 /// </summary>
@@ -43,7 +48,7 @@ public SocketClient()
4348 }
4449
4550 /// <summary>
46- /// Initializes a new instance of the <see cref="Client "/> class.
51+ /// Initializes a new instance of the <see cref="SocketClient "/> class.
4752 /// </summary>
4853 /// <param name="host">The host.</param>
4954 /// <param name="port">The port.</param>
@@ -56,6 +61,31 @@ public SocketClient(string host,int port)
5661 socket = new Socket ( AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) ;
5762 }
5863
64+ /// <summary>
65+ /// Initializes a new instance of the <see cref="SocketClient"/> class.
66+ /// </summary>
67+ /// <param name="host">The host.</param>
68+ /// <param name="port">The port.</param>
69+ /// <param name="localPort">The local port.</param>
70+ public SocketClient ( string host , int port , int localPort )
71+ {
72+ SetHost ( host ) ;
73+
74+ SetPort ( port ) ;
75+
76+ Exception ex = Utils . ValidatePort ( localPort ) ;
77+ if ( ex == null )
78+ {
79+ LocalPort = localPort ;
80+ }
81+ else
82+ {
83+ throw ex ;
84+ }
85+
86+ socket = new Socket ( AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) ;
87+ }
88+
5989 /// <summary>
6090 /// Connects this instance.
6191 /// </summary>
@@ -138,6 +168,14 @@ public void Connect(string host, int port)
138168
139169 SetPort ( port ) ;
140170
171+ // bind local port
172+ if ( LocalPort > 0 )
173+ {
174+ EndPoint endPoint = new IPEndPoint ( IPAddress . Any , LocalPort ) ;
175+
176+ socket . Bind ( endPoint ) ;
177+ }
178+
141179 socket . Connect ( Host , Port ) ;
142180
143181 listener ? . Stop ( ) ;
0 commit comments