Skip to content

Commit 885f4b9

Browse files
committed
fix tcp server stop error.
1 parent 4f79a97 commit 885f4b9

4 files changed

Lines changed: 76 additions & 8 deletions

File tree

build/version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup Condition="'$(AssemblyName)'=='GodSharp.Socket'">
33
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
4-
<Version>1.0.2-preview5</Version>
4+
<Version>1.0.2-preview6</Version>
55
</PropertyGroup>
66
</Project>

sample/Tcp/Chat/GodSharp.Chat/UI/Server/FormServer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private void FormServer_Load(object sender, EventArgs e)
3232

3333
private void btnStart_Click(object sender, EventArgs e)
3434
{
35-
if (server?.Running==true)
35+
if (server?.Running == true)
3636
{
3737
return;
3838
}
@@ -58,8 +58,8 @@ private void btnStop_Click(object sender, EventArgs e)
5858
server.Stop();
5959
}
6060

61-
btnStart.Enabled = server.Running;
62-
btnStop.Enabled = !server.Running;
61+
btnStart.Enabled = !server.Running;
62+
btnStop.Enabled = server.Running;
6363
}
6464

6565
private void OnConnected(TcpSender sender)

src/GodSharp.Socket/Tcp/Client/SocketClient.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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();

src/GodSharp.Socket/Tcp/Server/SocketServer.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,36 @@ public override void Stop()
182182
{
183183
try
184184
{
185+
Running = false;
186+
185187
foreach (var item in listeners)
186188
{
187-
item.Value?.Stop();
189+
try
190+
{
191+
item.Value?.Stop();
192+
}
193+
catch (Exception ex)
194+
{
195+
#if DEBUG
196+
Console.WriteLine(ex.Message);
197+
#endif
198+
}
188199
}
189200

190201
clients.Clear();
191202
clientMap.Clear();
192203
listeners.Clear();
193204

194-
if (socket.Connected)
205+
try
195206
{
196207
socket.Close();
197208
}
209+
catch (Exception ex)
210+
{
211+
#if DEBUG
212+
Console.WriteLine(ex.Message);
213+
#endif
214+
}
198215
}
199216
catch (Exception ex)
200217
{
@@ -232,7 +249,19 @@ private void AcceptFun()
232249
{
233250
try
234251
{
235-
Socket _socket = socket.Accept();
252+
Socket _socket = null;
253+
try
254+
{
255+
_socket = socket.Accept();
256+
}
257+
catch (Exception ex)
258+
{
259+
#if DEBUG
260+
Console.WriteLine(ex.Message);
261+
#endif
262+
}
263+
264+
if (!Running) break;
236265

237266
// keep alive
238267
if (KeepAlive)
@@ -280,6 +309,7 @@ private void AcceptFun()
280309
throw new Exception(ex.Message, ex);
281310
}
282311
}
312+
283313
Running = false;
284314
}
285315

0 commit comments

Comments
 (0)