你能试试这个。我认为你的问题是编码。
当您发送消息时:
string message = input(textbox ex);
UTF8Encoding utf8 = new UTF8Encoding();
byte[] encodedbytes = utf8.GetBytes(message);
并保留您的代码,因为它是在接收。
我用以下方法对此进行了测试:
public partial class Form1 : Form
{
private Thread thread;
private byte[] encodedBytes2;
private byte[] encodedBytes;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
UTF8Encoding utf8 = new UTF8Encoding();
string unicodeString = "Quick brown fox";
encodedBytes = utf8.GetBytes(unicodeString);
encodedBytes2 = new byte[20];
encodedBytes2[0] = (byte)'r';
encodedBytes2[1] = (byte)'e';
for(int i = 0; i < 5; i++)
textBox1.AppendText("ENCODED " + Encoding.ASCII.GetString(encodedBytes) + Environment.NewLine);
for(int i = 0; i < 5; i++)
textBox1.AppendText(" NOT ENCODED " + Encoding.ASCII.GetString(encodedBytes2) + Environment.NewLine);
thread = new Thread(ThreadWork);
thread.IsBackground = true;
thread.Start();
}
private void ThreadWork()
{
for (int i = 0; i < 5; i++)
textBox1.Invoke(new Action(() => textBox1.AppendText("THREAD ENCODED " + Encoding.ASCII.GetString(encodedBytes) + Environment.NewLine)));
for (int i = 0; i < 5; i++)
textBox1.Invoke(new Action(() => textBox1.AppendText("THREAD NOT ENCODED " + Encoding.ASCII.GetString(encodedBytes2) + Environment.NewLine)));
}
}
输出在多行文本框中显示:
ENCODED
Quick brown fox ENCODED Quick brown fox ENCODED Quick brown fox ENCODED Quick brown fox ENCODED Quick brown fox NOT ENCODED re NOT ENCODED re NOT ENCODED re NOT ENCODED re NOT ENCODED reTHREAD ENCODED Quick brown fox THREAD ENCODED Quick brown fox
THREAD
ENCODED
Quick
brown
fox
THREAD编码 Quick brown fox线程 ENCODED
Quick brown fox
线程未编码 reTHREAD未编码 reTHREAD未编码 reTHREAD未编码 reTHREAD未编码 re