111,094
社区成员




可以把MessageBox.Show(System.Text.Encoding.ASCII.GetString(byteTime));
这个要Show的东西 让他在一个TextBox上显示 方法如下
delegate void SetTextBoxTextCallBack(string byteTime); //跨线程操作TextBox控件委托
private void UpdateTextBox1Text(string byteTime)
{
if (this.TextBox1.InvokeRequired)
{
SetTextBoxTextCallBack m_SetTextBoxTextCallBack = new SetTextBoxTextCallBack(UpdateTextBox1Text);
this.BeginInvoke(m_SetTextBoxTextCallBack, new object[] { byteTime});
}
else
{
this.TextBox1.Text += byteTime;
}
}
然后把MessageBox.Show()那里改成UpdateTextBox1Text( byteTime)
//引用集
using System.Threading;
/***********************************/
private Thread thThreadread;//创建线程,用以侦听端口号,接受信息
/*******************/
///接收按钮
private void ReceiveBTN_Click(object sender, EventArgs e)
{
thThreadread = new Thread(new ThreadStart(TcpTimeServer));
thThreadread.Start();//启动线程
}
///监听端口,进行连接
private void TcpTimeServer()
{
int portNum = 8000;
bool done = false;
TcpListener listener = new TcpListener(portNum);
listener.Start();
TcpClient client = listener.AcceptTcpClient();
NetworkStream ns = client.GetStream();
while (!done)
{
byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString());
byte[] buff=new byte[1000];
try
{
if (client==null)
{
client = listener.AcceptTcpClient();
ns = client.GetStream();
}
ns.Write(byteTime, 0, byteTime.Length);
UpdateListBoxText(System.Text.Encoding.ASCII.GetString(byteTime));
ns.Read(buff, 0, buff.Length);
UpdateListBoxText(System.Text.Encoding.ASCII.GetString(buff));
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
} }
client.Close();
listener.Stop();
}
//可能有错误,我是按照2楼的textbox进行的修改
//跨线程操作ListBox控件委托
delegate void SetListBoxCallback(string byteTime);
private void UpdateListBoxText(string byteTime)
{
if (this.ReceiveLB.InvokeRequired)
{
SetListBoxCallback m_SetListBoxCallBack = new SetListBoxCallback(UpdateListBoxText);
this.BeginInvoke(m_SetListBoxCallBack, new object[] { byteTime });
}
else
{
this.ReceiveLB.Items.Add(byteTime);
}
}
//1st
ManualResetEvent mre=new ManualResetEvent(false);
int portNum = 8000;
bool done = false;
TcpListener listener = new TcpListener(portNum);
listener.Start();
//TcpClient client = listener.AcceptTcpClient();
NetworkStream ns = client.GetStream();
while (!done)
{
//2nd
mre.Reset();
Console.Write("Waiting for connection...");
ReceiveLB.Items.Add("Waiting for connection...");
//3rd
TcpClient client = listener.AcceptTcpClient();
mre.WaitOne();
Console.WriteLine("Connection accepted.");
ReceiveLB.Items.Add("Connection accepted.");
byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString());
byte[] buff=new byte[1000];
try
{
if (client==null)
{
//???
client = listener.AcceptTcpClient();
ns = client.GetStream();
//4th
mre.Set();
}
//写数据
ns.Write(byteTime, 0, byteTime.Length);
//ReceiveLB为listbox,采用线程的话会报线程中无法调用
ReceiveLB.Items.Add("send:"+System.Text.Encoding.ASCII.GetString(byteTime));
MessageBox.Show(System.Text.Encoding.ASCII.GetString(byteTime));
//读数据
ns.Read(buff, 0, buff.Length);
ReceiveLB.Items.Add("receive:" + System.Text.Encoding.ASCII.GetString(buff));
MessageBox.Show(System.Text.Encoding.ASCII.GetString(buff));
//ns.Close();
//client.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
client.Close();
listener.Stop();