1+ using System ;
2+ using System . IO ;
3+ using System . Net ;
4+ using System . Net . Sockets ;
5+ using System . Text ;
6+ using Console = System . Diagnostics . Debug ;
7+
8+ namespace SharpConstraintLayout . Maui . DebugTool
9+ {
10+ /// <summary>
11+ /// Data stream
12+ /// </summary>
13+ /// <param name="stream"></param>
14+ internal delegate void AcceptStreamHandler ( MemoryStream stream ) ;
15+
16+ /// <summary>
17+ /// Accept message from server
18+ /// </summary>
19+ internal class MessageClient
20+ {
21+ private Socket m_sock ; // Server connection
22+ private byte [ ] m_byBuff = new byte [ 262144 ] ; // Recieved data buffer
23+
24+ internal event AcceptStreamHandler AcceptedStreamEvent ; // Add Message Event handler for Form
25+
26+ public string ServerIPAddressText ;
27+ public int ServerPort ;
28+
29+ /// <summary>
30+ /// 创建
31+ /// </summary>
32+ /// <param name="serverIp"></param>
33+ /// <param name="port"></param>
34+ public MessageClient ( string serverIp , int port )
35+ {
36+ ServerIPAddressText = serverIp ;
37+ ServerPort = port ;
38+ Connect ( ) ;
39+ }
40+
41+ /// <summary>
42+ /// 连接
43+ /// </summary>
44+ public void Connect ( )
45+ {
46+ try
47+ {
48+ // Close the socket if it is still open
49+ if ( m_sock != null && m_sock . Connected )
50+ {
51+ m_sock . Shutdown ( SocketShutdown . Both ) ;
52+ System . Threading . Thread . Sleep ( 10 ) ;
53+ m_sock . Close ( ) ;
54+ }
55+
56+ // Create the socket object
57+ m_sock = new Socket ( AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) ;
58+
59+ // Define the Server address and port
60+ IPEndPoint epServer = new IPEndPoint ( IPAddress . Parse ( ServerIPAddressText ) , ServerPort ) ;
61+
62+ // Connect to the server blocking method and setup callback for recieved data
63+ // m_sock.Connect( epServer );
64+ // SetupRecieveCallback( m_sock );
65+
66+ // Connect to server non-Blocking method
67+ m_sock . Blocking = true ; // false;
68+ AsyncCallback onconnect = new AsyncCallback ( OnConnect ) ;
69+ m_sock . BeginConnect ( epServer , onconnect , m_sock ) ;
70+ }
71+ catch ( Exception ex )
72+ {
73+ Console . WriteLine ( ex . Message + ",Server Connect failed!" ) ;
74+ }
75+ }
76+
77+ private void OnConnect ( IAsyncResult ar )
78+ {
79+ // Socket was the passed in object
80+ Socket sock = ( Socket ) ar . AsyncState ;
81+ // Check if we were sucessfull
82+ try
83+ {
84+ //sock.EndConnect( ar );
85+ if ( sock . Connected )
86+ {
87+ AsyncCallback recieveData = new AsyncCallback ( OnRecievedData ) ; //回调
88+ sock . BeginReceive ( m_byBuff , 0 , m_byBuff . Length , SocketFlags . None , recieveData , sock ) ; //接受数据,植入回调
89+ }
90+ else
91+ Console . WriteLine ( "Unable to connect to remote machine," + "Connect Failed!" ) ;
92+ }
93+ catch ( Exception ex )
94+ {
95+ Console . WriteLine ( ex . Message , ",Unusual error during Connect!" ) ;
96+ }
97+ }
98+
99+ /// <summary>
100+ /// Send the Message in the Message area. Only do this if we are connected
101+ /// </summary>
102+ public void SendMessage ( string messageText )
103+ {
104+ // Check we are connected
105+ if ( m_sock == null || ! m_sock . Connected )
106+ {
107+ //Console.WriteLine("Must be connected to Send a message");
108+ return ;
109+ }
110+
111+ // Read the message from the text box and send it
112+ try
113+ {
114+ // Convert to byte array and send.
115+ //Byte[] byteDateLine = Encoding.ASCII.GetBytes(NeedSendMessageText.ToCharArray());//改成支持中文
116+ byte [ ] byteDateLine = Encoding . UTF8 . GetBytes ( messageText . ToCharArray ( ) ) ;
117+ m_sock . Send ( byteDateLine , byteDateLine . Length , 0 ) ;
118+ }
119+ catch ( Exception ex )
120+ {
121+ Console . WriteLine ( ex . Message + " Send Message Failed!" ) ;
122+ }
123+ }
124+
125+ MemoryStream stream ;
126+ private void OnRecievedData ( IAsyncResult ar )
127+ {
128+ // Socket was the passed in object
129+ Socket sock = ( Socket ) ar . AsyncState ;
130+
131+ // Check if we got any data
132+ try
133+ {
134+ int nBytesRec = sock . EndReceive ( ar ) ;
135+ if ( nBytesRec > 0 )
136+ {
137+ // Wrote the data to the List
138+ string sRecieved = Encoding . ASCII . GetString ( m_byBuff , 0 , nBytesRec ) ;
139+ int length = int . Parse ( sRecieved ) ; //第一次接受的是数据总长度
140+
141+ //不每次回调,该次传输就在这里取完
142+ Console . WriteLine ( "*** Start accept dll at {0} ***" , DateTime . Now . ToString ( "G" ) ) ;
143+
144+ sock . Send ( Encoding . ASCII . GetBytes ( "OK" ) ) ; //告诉服务器准备接受
145+
146+ if ( stream != null )
147+ {
148+ stream . Close ( ) ;
149+ stream = null ;
150+ }
151+ stream = new MemoryStream ( ) ;
152+
153+ int acceptedCount = 0 ;
154+ int byteCount = 0 ;
155+ do
156+ {
157+ byteCount = sock . Receive ( m_byBuff , m_byBuff . Length , SocketFlags . None ) ; //接收数据长度
158+ stream . Write ( m_byBuff , 0 , byteCount ) ; //写入流
159+ //stream.Flush();//缓冲写入文件
160+
161+ acceptedCount = acceptedCount + byteCount ;
162+ }
163+ while ( byteCount > 0 && acceptedCount < length ) ;
164+ Console . WriteLine ( "接收长度:" + length ) ;
165+
166+ Console . WriteLine ( "*** Accept dll success at {0} ***" , DateTime . Now . ToString ( "G" ) ) ;
167+ AcceptedStreamEvent . Invoke ( stream ) ;
168+
169+ //下次回调
170+ // If the connection is still usable restablish the callback
171+ OnConnect ( ar ) ;
172+ }
173+ else
174+ {
175+ // If no data was recieved then the connection is probably dead
176+ Console . WriteLine ( "Client {0}, disconnected" , sock . RemoteEndPoint ) ;
177+ sock . Shutdown ( SocketShutdown . Both ) ;
178+ sock . Close ( ) ;
179+ }
180+ }
181+ catch ( Exception ex )
182+ {
183+ Console . WriteLine ( ex . Message , ",Unusual error druing Recieve!" ) ;
184+ }
185+ }
186+
187+ /// <summary>
188+ /// Close the Socket connection before going home
189+ /// </summary>
190+ public void Close ( )
191+ {
192+ if ( m_sock != null && m_sock . Connected )
193+ {
194+ m_sock . Shutdown ( SocketShutdown . Both ) ;
195+ m_sock . Close ( ) ;
196+ Console . WriteLine ( "连接关闭" ) ;
197+ }
198+ }
199+ }
200+ }
0 commit comments