/// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainStationForm_Load(object sender, EventArgs e)
{
IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.189"), 12717); //启动一个线程来接受
this.myThread = new Thread(new ThreadStart(
delegate()
{
//监听
TcpListener tempTcpListener = new TcpListener(iPEndPoint); //开启监听
tempTcpListener.Start(20); while (true)
{
//获得tcpClient
this.tcpClient = tempTcpListener.AcceptTcpClient(); //实例化流
this.receiveNetworkStream = this.tcpClient.GetStream(); //设置接受缓存区大小
this.receiveBytes = new byte[this.tcpClient.Available]; if (receiveBytes.Length != 0)
{
//异步方法读取数据
this.receiveNetworkStream.BeginRead(receiveBytes, 0, receiveBytes.Length, new AsyncCallback(RecvCallback), this.receiveNetworkStream);
}
}
}
));
this.myThread.IsBackground = true;
this.myThread.Start();
} /// <summary>
/// 回调函数
/// </summary>
/// <param name="ar"></param>
private void RecvCallback(IAsyncResult ar)
{ NetworkStream ns = (NetworkStream)ar.AsyncState; //调用数据流的读取方法
ns.EndRead(ar);
}为什么 while (true) 里面的东西只执行一次?就执行我连接的那一次...客户端再发消息过来就不再执行和理会了......
解决方案 »
- 谁用.Net的C/S结构做过电子拣货系统 怎样用程序写LED灯开启/熄灭/闪烁的程序 急
- 关于session
- 如何在table中定位Label的位置,用了Align无效
- 如何在应用程序中建立索引
- 关于控制台的问题,在线等
- \0表示什么意思?怎么用stringbuilder连接两个字符串.前一个的最后带一个\0后面那个就加不上去了呢?
- 如何对DataSet中的表进行select查询??
- C#中组件有关父窗体,拥用者,所有者是个何概念?
- HttpClient用异步委托和线程池报错问题
- 如何用C#做一个邮件接收和发送的东西?
- 这是什么算法加密的?
- C# 二进制序列化进遇到问题 未找到反序列化“TypeName”类型对象的构造函数。
this.myThread = new Thread(new ThreadStart(
delegate()
{
//监听
TcpListener tempTcpListener = new TcpListener(iPEndPoint); //开启监听
tempTcpListener.Start(20);
try
{
while (true)
{
//获得tcpClient
this.tcpClient = tempTcpListener.AcceptTcpClient(); //实例化流
this.receiveNetworkStream = this.tcpClient.GetStream(); //设置接受缓存区大小
this.receiveBytes = new byte[this.tcpClient.Available]; if (receiveBytes.Length != 0)
{
//异步方法读取数据
this.receiveNetworkStream.BeginRead(receiveBytes, 0, receiveBytes.Length, new AsyncCallback(RecvCallback), this.receiveNetworkStream);
}
}
}
catch (Exception ex)
{
}
}
));
修改后的代码...也没有抓到异常......
/// 回调函数
/// </summary>
/// <param name="ar"></param>
private void RecvCallback(IAsyncResult ar)
{ NetworkStream ns = (NetworkStream)ar.AsyncState; //调用数据流的读取方法
ns.EndRead(ar);
//处理接收到的消息 //再次调用异步接收 ns.BeginRead(receiveBytes, 0, receiveBytes.Length, new AsyncCallback(RecvCallback), this.receiveNetworkStream); }
/// 回调函数
/// </summary>
/// <param name="ar"></param>
private void RecvCallback(IAsyncResult ar)
{ NetworkStream ns = (NetworkStream)ar.AsyncState; //调用数据流的读取方法
ns.EndRead(ar);
//处理接收到的消息 //再次调用异步接收 ns.BeginRead(receiveBytes, 0, receiveBytes.Length, new AsyncCallback(RecvCallback), ns); }