11using System ;
2+ using System . Diagnostics ;
23using System . Linq ;
34using System . Net ;
45using System . Net . Sockets ;
56using System . Threading ;
7+ using System . Threading . Tasks ;
68
79namespace One . Core . Helpers . NetHelpers
810{
@@ -23,7 +25,7 @@ public class AsyncTCPClient : BaseHelper
2325 public Action < byte [ ] > SendAction ;
2426 public Action < byte [ ] > OnConnected ;
2527 public Action < byte [ ] > OnDisConnected ;
26-
28+ public Action < bool > OnConnectedBool ;
2729 public CancellationToken cancellationToken = default ;
2830
2931 /// <summary> 暂时不起作用 </summary>
@@ -36,7 +38,7 @@ public AsyncTCPClient(Action<string> logAction) : base(logAction)
3638 /// <summary> 初始化作为客户端并连接 </summary>
3739 /// <param name="ip"> </param>
3840 /// <param name="port"> </param>
39- public bool InitClient ( IPAddress ip , int port )
41+ public void Connect ( IPAddress ip , int port )
4042 {
4143 try
4244 {
@@ -47,22 +49,23 @@ public bool InitClient(IPAddress ip, int port)
4749 SocketAsyncEventArgs e = new SocketAsyncEventArgs ( ) ;
4850 e . RemoteEndPoint = ipEndPoint ;
4951 e . UserToken = socket ;
52+
5053 e . Completed += new EventHandler < SocketAsyncEventArgs > ( ConnectComplete ) ;
51- e . SetBuffer ( System . Text . Encoding . UTF8 . GetBytes ( "Hello!" ) ) ;
54+ // e.SetBuffer(System.Text.Encoding.UTF8.GetBytes("Hello!"));//发这个会出意外,本地打印log就行
5255
53- var willRaiseEvent = socket . ConnectAsync ( e ) ;
56+ var pending = socket . ConnectAsync ( e ) ;
5457
55- if ( ! willRaiseEvent ) //暂时没发现有什么用
58+ var a = $ "{ socket . LocalEndPoint . ToString ( ) } connecting......";
59+ var info = System . Text . Encoding . UTF8 . GetBytes ( a ) ;
60+ OnConnected ? . Invoke ( info ) ;
61+ if ( ! pending ) //同步的走这里
5662 {
5763 ConnectComplete ( this , e ) ;
5864 }
59-
60- return true ;
6165 }
6266 catch ( Exception ex )
6367 {
6468 WriteLog ( ex . ToString ( ) ) ;
65- return false ;
6669 }
6770 }
6871
@@ -77,10 +80,16 @@ private void ConnectComplete(object sender, SocketAsyncEventArgs e)
7780 {
7881 //客户端自己的Socket,也可以直接用最开始的socket
7982 var localClientSocket = e . ConnectSocket ; //连接的 Socket 对象。
83+
8084 var addressFamily = localClientSocket . AddressFamily . ToString ( ) ;
81- // var a = localClientSocket.LocalEndPoint.ToString();
85+
86+ OnConnectedBool ? . Invoke ( localClientSocket . Connected ) ;
87+ Debug . WriteLine ( "Status:" + localClientSocket . Connected ) ;
88+
8289 var a = $ "{ localClientSocket . LocalEndPoint . ToString ( ) } connected!";
8390
91+ Debug . WriteLine ( localClientSocket . RemoteEndPoint . ToString ( ) ) ;
92+
8493 var info = System . Text . Encoding . UTF8 . GetBytes ( a ) ;
8594 OnConnected ? . Invoke ( info ) ;
8695
@@ -91,9 +100,13 @@ private void ConnectComplete(object sender, SocketAsyncEventArgs e)
91100 }
92101 catch ( Exception ex )
93102 {
94- WriteLog ( $ "连接失败 => { ex } " ) ;
103+ OnConnectedBool ? . Invoke ( false ) ;
95104
96- // throw;
105+ var a = $ "{ e . RemoteEndPoint . ToString ( ) } connect failed!";
106+
107+ var info = System . Text . Encoding . UTF8 . GetBytes ( a ) ;
108+ OnConnected ? . Invoke ( info ) ;
109+ WriteLog ( $ "连接失败 => { ex } ") ;
97110 }
98111 }
99112
@@ -104,6 +117,7 @@ public void ReleaseClient()
104117 try
105118 {
106119 socket . Shutdown ( SocketShutdown . Both ) ;
120+ OnConnectedBool ? . Invoke ( false ) ;
107121
108122 var a = $ "{ socket . LocalEndPoint } disconnected!";
109123 var info = System . Text . Encoding . UTF8 . GetBytes ( a ) ;
@@ -114,17 +128,20 @@ public void ReleaseClient()
114128 catch ( Exception ex )
115129 {
116130 WriteLog ( ex . ToString ( ) ) ;
117- throw ex ;
131+ throw ;
118132 }
119133 }
120134
121135 public async void SendData ( byte [ ] data )
122136 {
123137 try
124138 {
125- // int count = sckClient.Send(data);
126- //Console.WriteLine("发送数据长度为:" + count);
139+ if ( ! socket . Connected )
140+ {
141+ OnConnectedBool ? . Invoke ( false ) ;
127142
143+ return ;
144+ }
128145 int bytesSent = 0 ;
129146 while ( bytesSent < data . Length )
130147 {
@@ -161,7 +178,9 @@ private async void Receive()
161178 }
162179 catch ( Exception ex )
163180 {
181+ //不能上抛
164182 WriteLog ( ex . ToString ( ) ) ;
183+ //throw ex;
165184 }
166185 }
167186 }
0 commit comments