namespace date_sender
{
public partial class Form1 : Form
{
private TcpListener tl;
private NetworkStream ns; public delegate void MyDelegate(string temp);
class ClientTcp
{ //设置网络流局部对象
private NetworkStream ns; //声明类型为MyDelegate的事件MyEvent
public event MyDelegate MyEvent; //构造函数中接收参数以初始化
public ClientTcp(NetworkStream ns)
{
this.ns = ns;
} //服务器端线程所调用的方法
public void TcpRead()
{
//listBox1.Items.Add("TcpRead Begin...");
Console.WriteLine("TcpRead Begin...");
//获得相关的封装流
while (true)
{
try
{
byte[] bytes = new byte[1024];
int bytesRead = ns.Read(bytes, 0, bytes.Length);
string pkt = BitConverter.ToString(bytes, 0, bytesRead);
//listBox1.Items.Add(Encoding.ASCII.GetString(bytes, 0, bytesRead));
Console.WriteLine(pkt);
MyEvent(pkt);
}
catch (Exception e)
{
Console.WriteLine("A Client Exit!");
ns.Close();
break;
}
}
} public void timeout(object source, System.Timers.ElapsedEventArgs e)
{
try
{
byte[] buffer = { 0xFF, 0xFF, 0x0, 0xFF };
//byte[] buffer = Encoding.ASCII.GetBytes(" ");
ns.Write(buffer, 0, buffer.Length);
}
catch (Exception ex)
{
System.Timers.Timer timer = source as System.Timers.Timer;
timer.Enabled = false;
timer.Close();
}
} public void TcpWrite()
{
//listBox1.Items.Add("TcpWrite Begin...");
Console.WriteLine("TcpWrite Begin...");
try
{
byte[] buffer = { 0xFF, 0xFF, 0x0, 0xFF };
//byte[] buffer = Encoding.ASCII.GetBytes(" ");
ns.Write(buffer, 0, buffer.Length);
}
catch (Exception e) { }
System.Timers.Timer timer = new System.Timers.Timer(10000);
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timeout);
timer.AutoReset = true;
timer.Enabled = true;
}
} public Form1()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IPAddress serverIp = IPAddress.Parse(textBox1.Text.ToString());
int port = int.Parse(textBox2.Text.ToString());
tl = new TcpListener(serverIp,port);
tl.Start(); Thread th = new Thread(new ThreadStart(listen));
th.IsBackground = true;
th.Start(); button1.Enabled = false;
}
private void listen()
{
while (true)
{
listBox1.Items.Add("Waiting for connection...");
//Console.WriteLine("Waiting for connection...");
Socket sock = tl.AcceptSocket();
listBox1.Items.Add("Connection accepted.");
//Console.WriteLine("Connection accepted.");
ns = new NetworkStream(sock);
ClientTcp ct = new ClientTcp(ns);
ct.MyEvent += new MyDelegate(ct_MyEvent); Thread th_write = new Thread(new ThreadStart(ct.TcpWrite));
th_write.IsBackground = true;
th_write.Start(); Thread th = new Thread(new ThreadStart(ct.TcpRead));
th.IsBackground = true;
th.Start();
}
}
void ct_MyEvent(string temp)
{
this.listBox2.Items.Add(temp);
} private void button3_Click(object sender, EventArgs e)
{
this.listBox1.Items.Clear();
} private void button2_Click(object sender, EventArgs e)
{
this.listBox2.Items.Clear();
}
}
}程序运行后发现当有多个客户端,并且客户端发送数据很频繁的时候,CPU占用到达90%左右
麻烦高手看看问题在哪
解决方案 »
- 如何获取xp系统默认字体
- CSLA中修改数据的问题
- 百分求outlook 于exchange通信问题~
- 求一個用do...while來設計一個反串數字
- 通过程序添加的datagridview列无法获取到列值
- 请高手帮忙!关于继承的问题
- 各位大哥!救救小弟吧!我现在在坐从一个表里提取数据,要产生多个DataTable,可是不知该如何下手。在线等!!!!
- DataSet问题
- 原来鬼佬们的程序写的还不如我们.现在信心真是倍增啊.
- DEVpress2011版,复选框显示问题
- 急急急!在启动debug的时候,提示查找更新失败远程服务器返回错误:404 未找到
- winform中listview换图片问题,大家来帮忙啊!
里面sleep一下,让出一点时间给cpu,你那个死循环,到那也得把cpu累死