Socket multi-thread programming delegate controls

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.