Problem:
First, set a simple server listener
1 delegate void SetTextEvent (Control ctl, string text );
2 void SetTextLine (Control ctl, string text)
3 {
4 if (ctl. InvokeRequired)
5 ctl. Invoke (new SetTextEvent (SetTextLine), new object [] {ctl, text });
6 else
7 ctl. Text = text + Environment. NewLine + ctl. Text;
8}
Thread thread = new Thread (doactions );
Thread. IsBackground = true;
Thread. Start ();
1 void doactions ()
2 {
3 Socket s = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. IP );
4 EndPoint ep = new IPEndPoint (new IPAddress (IPToUInt32 (""), 3000 );
5 SetTextLine (LogCtl, "bind port ");
6 try
7 {
8 s. Bind (ep );
9 s. Listen (1024 );
10}
11 catch (Exception ex)
12 {SetTextLine (LogCtl, ex. Message); return ;}
13 SetTextLine (LogCtl, "bound successfully! ");
14 bool done = true;
15 while (done)
16 {
17 SetTextLine (LogCtl, "waiting for connection ");
18 Socket client = s. Accept ();
19 SetTextLine (LogCtl, "connection successful ");
20
21 while (client. Connected)
22 {
23 try
24 {
25 byte [] B = new byte [1, 1024];
26 int size = client. Receive (B );
27
28 string receivedstr = Encoding. Default. GetString (B );
29 receivedstr = receivedstr. Trim ();
30 SetTextLine (MsgCtl, receivedstr );
31}
32 catch (Exception ex)
33 {
34 SetTextLine (MsgCtl, ex. Message );
35 SetTextLine (LogCtl, "Connection closed ");
36 client. Close ();
37}
38}
39}
40}
This is where the strange problem occurs. SetTextLine (MsgCtl, receivedstr); the ctl. Text = text + Environment. NewLine + ctl. Text cannot be achieved at all. The effect is changed
Ctl. Text = text. It works in other places. If you set the receivedstr character to a fixed value, for example, change receivedstr = receivedstr. Trim ();
Receivedstr = DateTime. Now. ToString (); then, it is the effect of ctl. Text = text + Environment. NewLine + ctl. Text. Reading from the byte array obtained from int size = client. Receive (B); has a problem, which is really strange. Hope you can tell me what you know! This has plagued me for a long time!
The following is my dumb solution, which is not very helpful.
Solution:
Set two processing methods.
Void doactions ()
{
Socket s = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. IP );
EndPoint ep = new IPEndPoint (new IPAddress (IPToUInt32 (""), 3000 );
SetTextLine (LogCtl, "bind port ");
Try
{
S. Bind (ep );
Seconds. Listen (1024 );
}
Catch (Exception ex)
{SetTextLine (LogCtl, ex. Message); return ;}
SetTextLine (LogCtl, "bound successfully! ");
Bool done = true;
While (done)
{
SetTextLine (LogCtl, "waiting for connection ");
Socket client = s. Accept ();
SetTextLine (LogCtl, "connection successful ");
While (client. Connected)
{
Try
{
Byte [] B = new byte [1024];
Int size = client. Receive (B );
String receivedstr = Encoding. Default. GetString (B );
Receivedstr = receivedstr. Trim ();
SetText (MsgCtl, receivedstr );
SetText (MsgCtl, Environment. NewLine );
}
Catch (Exception ex)
{
SetTextLine (MsgCtl, ex. Message );
SetTextLine (LogCtl, "Connection closed ");
Client. Close ();
}
}
}
}
Delegate void SetTextEvent (Control ctl, string text );
Void SetText (Control ctl, string text)
{
If (ctl. InvokeRequired)
Ctl. Invoke (new SetTextEvent (SetText), new object [] {ctl, text });
Else
Ctl. Text + = text;
}
Void SetTextLine (Control ctl, string text)
{
If (ctl. InvokeRequired)
Ctl. Invoke (new SetTextEvent (SetTextLine), new object [] {ctl, text });
Else
Ctl. Text = text + Environment. NewLine + ctl. Text;
}
Why use it after Receive?
SetText (MsgCtl, receivedstr );
SetText (MsgCtl, Environment. NewLine );
Line feed and ctl is unavailable. text = text + Environment. newLine + ctl. text; it's strange to connect in this way that the content of the space can be read by delegation in the thread, but cannot be connected at all. That's strange. The above code can only be regarded as a very unpleasant solution.