Socket多线程编程委托控件的奇怪问题

问题:

首先设置简单的服务端监听

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, "绑定端口");
 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, "绑定成功!");
14             bool done = true;
15             while (done)
16             {
17                 SetTextLine(LogCtl, "等待连接");
18                 Socket client = s.Accept();
19                 SetTextLine(LogCtl, "连接成功");
20 
21                 while (client.Connected)
22                 {
23                     try
24                     {
25                         byte[] b = new byte[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, "连接关闭");
36                         client.Close();
37                     }
38                 }
39             }
40         }

奇怪的问题就出现在这里。SetTextLine(MsgCtl, receivedstr);根本无法达到 ctl.Text = text + Environment.NewLine + ctl.Text; 这个目的,效果变成了
 ctl.Text  = text的效果。而其他地方都是可以的。偏偏,如果把receivedstr这个字符设置为固定值,比如把receivedstr =
 receivedstr.Trim();改成
receivedstr = DateTime.Now.ToString();那么,就是ctl.Text = text + Environment.NewLine + ctl.Text; 的效果。而从int size = client.Receive(b);得出的byte数组读出却出现问题,真是太奇怪了。还望知道的朋友能告知!困扰了我好久的问题

下面是我的笨蛋解决方案,用的不是很爽。

解决方法:

设置两个处理方法。

        void doactions()
        {
            Socket s 
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            EndPoint ep 
= new IPEndPoint(new IPAddress(IPToUInt32("")), 3000);
            SetTextLine(LogCtl, 
"绑定端口");
            
try
            {
                s.Bind(ep);
                s.Listen(
1024);
            }
            
catch (Exception ex)
            { SetTextLine(LogCtl, ex.Message); 
return; }
            SetTextLine(LogCtl, 
"绑定成功!");
            
bool done = true;
            
while (done)
            {
                SetTextLine(LogCtl, 
"等待连接");
                Socket client 
= s.Accept();
                SetTextLine(LogCtl, 
"连接成功");

                
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, 
"连接关闭");
                        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; 
        }

在Receive后为什么要用
  SetText(MsgCtl, receivedstr);
  SetText(MsgCtl, Environment.NewLine);
进行换行并且无法ctl.Text = text + Environment.NewLine + ctl.Text; 按照这种方式进行连接,真的很奇怪,在线程中也能够用委托读取出空间的内容,却根本无法连接。太奇怪了。上面的代码只能算是一种非常不爽的解决方案了。
原文地址:https://www.cnblogs.com/birdshover/p/1021835.html