首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

(急)udpClient.Receive(ref remotePoint)一直揭示“在执行此操作前必须先调用 Bind 方法。”

2012-09-10 
(急)udpClient.Receive(ref remotePoint)一直提示“在执行此操作前必须先调用 Bind 方法。”using Systemusi

(急)udpClient.Receive(ref remotePoint)一直提示“在执行此操作前必须先调用 Bind 方法。”
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace UDP
{
  public class UDPClient
  {
  //设置一个timer,在发送或get数据的时候,判断5秒没有数据返回,则报连接不上
  System.Timers.Timer timer = new System.Timers.Timer();

  private IPEndPoint hostPoint;//服务器主机地址
  private IPEndPoint remotePoint;//目标主机地址

  private Thread listenThread;//监听线程
  private UdpClient udpClient;

  private bool bConnect = false;

  public bool BConnect
  {
  get { return bConnect; }
  set { bConnect = value; }
  }

  //类当前的状态
  public UDPRETURNTYPE currenttype = UDPRETURNTYPE.NULL;

  //singleton模式
  private static UDPClient theclient;

  //不可不带参数实例化
  private UDPClient()
  {

  }

  public static UDPClient instance(string ip)
  {
  theclient = new UDPClient(ip, "5006");
  return theclient;
  }
  ~UDPClient()
  {
  try
  {
  udpClient.Close();
  listenThread.Abort();

  }
  catch
  { }
  }

  public void Close()
  {
  try
  {
  udpClient.Close();
  listenThread.Abort();

  }
  catch
  { }
  }

  private UDPClient(string pserverIP, string pserverPort)
  {
  if (this.udpClient != null)
  {
  udpClient.Close();
  udpClient = null;
  }
  //Console.WriteLine("我是客户端,我开始运行啦11!");
  string serverIp = pserverIP;
  int Port = int.Parse(pserverPort);
  udpClient = new UdpClient();
  remotePoint = new IPEndPoint(IPAddress.Any, 0);//提供任何端口和IP,这里不需要指定
  hostPoint = new IPEndPoint(IPAddress.Parse(serverIp), Port);//实例化主机地址

  //=========================一开始测试发送==========================
  byte[] buffer;
  buffer = System.Text.Encoding.UTF8.GetBytes(Convert.ToString((int)UDPTYPE.connect));

  listenThread = new Thread(new ThreadStart(Run));
  listenThread.Start();
  this.timer.Interval = 500;
  this.timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
  }

  int itime = 0;
  void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {

  if (itime >= 3000 && this.currenttype == UDPRETURNTYPE.NULL)
  {
  itime = 0;
  timer.Stop();
  MessageBox.Show(" 与上级计算机无法正常连接,请检查\r“数据传输地址”是否错误或联系管理员!");

  }
  else if (this.currenttype == UDPRETURNTYPE.SUCCESS)
  {
  itime = 0;

  timer.Stop();
  MessageBox.Show("与上级同步数据成功!");



  }
  else if (this.currenttype == UDPRETURNTYPE.FALSE)
  {
  itime = 0;

  timer.Stop();
  MessageBox.Show("与上级同步数据失败!");
  }
  itime += 500;

  }

  //=================== 外部调用的方法
  public void GetMessage(string tbName, string sql)
  {
  string tempresult = "";
  tempresult += Convert.ToString((int)UDPTYPE.get) + "###" + tbName + "###" + sql + "###";
  try
  {
  byte[] buffer;
  //发送数据buffer
  buffer = System.Text.Encoding.UTF8.GetBytes(tempresult);
  udpClient.Send(buffer, buffer.Length, hostPoint);
  this.currenttype = UDPRETURNTYPE.NULL;
  timer.Start();
  //udpClient.Receive(ref hostPoint);
  }
  catch
  {
  //MessageBox.Show("信息获取失败,可能与上级连接计算机没连通,请检查IP或联系管理员!");
  //Console.WriteLine("数据发送异常,可能是服务器接收软件没开");
  }
  }

  public void ModifyMessage(DataTable dt, string dtname)
  {
  string tempresult = "";
  //加上记录数
  //tempresult += dt.Rows.Count.ToString() + "&&&";
  //
  tempresult += Convert.ToString((int)UDPTYPE.modify) + "###" + dtname + "###";
  ////////////type、dtname、cols以###分隔,rows以|rows|分隔,colname=colvalue|cols|colname===colvalue
  //type####dtname####colname===colvalue|cols|colname1===colvalue1|rows|colname===colvalue|cols|colname1===colvalue1
  tempresult += UDP.Base.DtToString(dt);
  try
  {
  byte[] buffer;
  //发送数据buffer
  buffer = System.Text.Encoding.UTF8.GetBytes(tempresult);
  udpClient.Send(buffer, buffer.Length, hostPoint);
  this.currenttype = UDPRETURNTYPE.NULL;
  timer.Start();
  //udpClient.Receive(ref hostPoint);
  }
  catch
  {
  //MessageBox.Show("信息获取失败,可能与上级连接计算机没连通,请检查IP或联系管理员!");
  //Console.WriteLine("数据发送异常,可能是服务器接收软件没开");
  }
  }

  /**/
  /// <summary>
  /// 监听线程
  /// </summary>
  private void Run()
  {
  byte[] buffer;
  while (true)
  {
  try
  {
  buffer = udpClient.Receive(ref remotePoint);//在这个地方提示在执行此操作前必须先调用 Bind 方法
  bConnect = true;
  if (buffer.Length > 0)
  {
  //Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, buffer.Length));
  string tempresult = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
  tempresult = UDP.Base.AnyData(tempresult, "client");
  //currenttype = 
  switch (tempresult)
  {
  case "1":


  currenttype = UDPRETURNTYPE.SUCCESS;
  break;
  case "2":
  currenttype = UDPRETURNTYPE.FALSE;
  break;
  case "3":
  currenttype = UDPRETURNTYPE.NULL;
  break;
  default:
  currenttype = UDPRETURNTYPE.SUCCESS;
  break;
  }
  //MessageBox.Show("数据更新,请刷新数据列表!");

  }

  }
  catch (Exception ex)
  {
  string aa = ex.ToString();
  bConnect = false;
  }
  Thread.Sleep(500);
  }
  }
  }
}


[解决办法]
都告诉你了,你肯定没有连接上服务端,你家就Receive了,没看到你代码中有connect的方法
[解决办法]
If you intend to receive multicast datagrams, you must call the Bind method with a multicast port number.
 

热点排行