CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

udp通讯有点不懂

楼主townee(townee)2005-04-03 10:26:57 在 .NET技术 / C# 提问

private   static   readonly   IPAddress   add=IPAddress.Parse("192.9.100.146");  
  private   const   int   po=6090;  
  private   static   void   StartListener()  
  {  
  UdpClient   listener=new   UdpClient();  
  IPEndPoint   dz=new   IPEndPoint(add,po);  
  try  
  {  
  listener.JoinMulticastGroup(add);  
  listener.Connect(dz);  
  上面是一个程序一小段,  
  问题是:listener.Connect(dz);中dz已经包含了ip与端口号,  
  为什么还要把listener.JoinMulticastGroup(add);把这个add加进去,  
  这样不重复吗? 问题点数:50、回复次数:6Top

1 楼BigIdiot628(大笨蛋,谁叫你不努力!)回复于 2005-04-03 11:06:10 得分 0

帮你顶一下Top

2 楼hanyaocsdn()回复于 2005-04-03 11:40:50 得分 0

关注ingTop

3 楼townee(townee)回复于 2005-04-03 14:50:49 得分 0

哪位有一个最简单的udp通讯的例子,包括server和client  
  我书上的例子不管用,我给加分,谢谢  
  zhipukeji@sina.com写上你的名字我好给分Top

4 楼townee(townee)回复于 2005-04-04 09:06:59 得分 0

upTop

5 楼townee(townee)回复于 2005-04-04 09:55:16 得分 0

upTop

6 楼lampson123(微软)回复于 2005-04-04 10:14:30 得分 50

using   System   ;  
  using   System.Drawing   ;  
  using   System.Collections   ;  
  using   System.ComponentModel   ;  
  using   System.Windows.Forms   ;  
  using   System.Data   ;  
  using   System.Net   ;  
  using   System.Net.Sockets   ;  
  using   System.Threading   ;  
  //程序中使用到线程  
  using   System.Text   ;  
  namespace   UDP聊天  
  {  
  ///   <summary>  
  ///   Form1   的摘要说明。  
  ///   </summary>  
  public   class   Form1   :   System.Windows.Forms.Form  
  {  
  private   System.Windows.Forms.Label   label1   ;  
  private   System.Windows.Forms.ListBox   listBox1   ;  
  private   System.Windows.Forms.TextBox   textBox1   ;  
  private   System.Windows.Forms.Label   label2   ;  
  private   System.Windows.Forms.Button   button1   ;  
  private   System.Windows.Forms.Button   button2   ;  
  private   System.Windows.Forms.TextBox   textBox2   ;  
  private   System.Windows.Forms.Label   label3   ;  
  private   System.Windows.Forms.StatusBar   statusBar1   ;  
  private   bool   ReadFlag   =   true   ;  
  //设定侦听标示位,通过它来设定是否侦听端口号  
  private   Thread   th   ;  
  //定义一个线程,在线程接收聊天信息  
  private   IPEndPoint   remote   ;  
  //定义一个远程结点,用以获取远程计算机IP地址和发送的聊天信息  
  private   UdpClient   udpclient   ;  
  //创建一个UDP网络服务  
  ///   <summary>  
  ///   必需的设计器变量。  
  ///   </summary>  
  private   System.ComponentModel.Container   components   =   null   ;  
   
  public   Form1   (   )  
  {  
  //  
  //   Windows   窗体设计器支持所必需的  
  //  
  InitializeComponent   (   )   ;  
   
  //  
  //   TODO:   在   InitializeComponent   调用后添加任何构造函数代码  
  //  
  }  
   
  ///   <summary>  
  ///   清理所有正在使用的资源。  
  ///   </summary>  
  protected   override   void   Dispose   (   bool   disposing   )  
  {  
  /*try  
  {  
  ReadFlag   =   false   ;  
  th.Abort   (   )   ;  
  remote   =null   ;  
  udpclient.Close   (   )   ;  
  th   =   null   ;  
  }  
  catch  
  {}*/  
  if   (   disposing   )  
  {  
  if   (   components   !=   null   )    
  {  
  components.Dispose   (   )   ;  
  }  
  }  
  base.Dispose   (   disposing   )   ;  
  }  
   
  #region   Windows   Form   Designer   generated   code  
  ///   <summary>  
  ///   设计器支持所需的方法   -   不要使用代码编辑器修改  
  ///   此方法的内容。  
  ///   </summary>  
  private   void   InitializeComponent   (   )  
  {  
  this.label1   =   new   System.Windows.Forms.Label();  
  this.listBox1   =   new   System.Windows.Forms.ListBox();  
  this.textBox1   =   new   System.Windows.Forms.TextBox();  
  this.label2   =   new   System.Windows.Forms.Label();  
  this.button1   =   new   System.Windows.Forms.Button();  
  this.button2   =   new   System.Windows.Forms.Button();  
  this.textBox2   =   new   System.Windows.Forms.TextBox();  
  this.label3   =   new   System.Windows.Forms.Label();  
  this.statusBar1   =   new   System.Windows.Forms.StatusBar();  
  this.SuspendLayout();  
  //    
  //   label1  
  //    
  this.label1.Location   =   new   System.Drawing.Point(11,   6);  
  this.label1.Name   =   "label1";  
  this.label1.Size   =   new   System.Drawing.Size(91,   23);  
  this.label1.TabIndex   =   0;  
  this.label1.Text   =   "接收信息:";  
  //    
  //   listBox1  
  //    
  this.listBox1.Location   =   new   System.Drawing.Point(9,   28);  
  this.listBox1.Name   =   "listBox1";  
  this.listBox1.Size   =   new   System.Drawing.Size(318,   121);  
  this.listBox1.TabIndex   =   1;  
  //    
  //   textBox1  
  //    
  this.textBox1.Location   =   new   System.Drawing.Point(72,   200);  
  this.textBox1.Name   =   "textBox1";  
  this.textBox1.Size   =   new   System.Drawing.Size(183,   20);  
  this.textBox1.TabIndex   =   3;  
  this.textBox1.Text   =   "";  
  //    
  //   label2  
  //    
  this.label2.Location   =   new   System.Drawing.Point(26,   203);  
  this.label2.Name   =   "label2";  
  this.label2.Size   =   new   System.Drawing.Size(91,   23);  
  this.label2.TabIndex   =   4;  
  this.label2.Text   =   "信息:";  
  //    
  //   button1  
  //    
  this.button1.FlatStyle   =   System.Windows.Forms.FlatStyle.Flat;  
  this.button1.Location   =   new   System.Drawing.Point(272,   200);  
  this.button1.Name   =   "button1";  
  this.button1.Size   =   new   System.Drawing.Size(42,   28);  
  this.button1.TabIndex   =   5;  
  this.button1.Text   =   "发送";  
  this.button1.Click   +=   new   System.EventHandler(this.button1_Click);  
  //    
  //   button2  
  //    
  this.button2.FlatStyle   =   System.Windows.Forms.FlatStyle.Flat;  
  this.button2.Location   =   new   System.Drawing.Point(272,   160);  
  this.button2.Name   =   "button2";  
  this.button2.Size   =   new   System.Drawing.Size(42,   28);  
  this.button2.TabIndex   =   6;  
  this.button2.Text   =   "接收";  
  this.button2.Click   +=   new   System.EventHandler(this.button2_Click);  
  //    
  //   textBox2  
  //    
  this.textBox2.Location   =   new   System.Drawing.Point(73,   163);  
  this.textBox2.Name   =   "textBox2";  
  this.textBox2.Size   =   new   System.Drawing.Size(181,   20);  
  this.textBox2.TabIndex   =   8;  
  this.textBox2.Text   =   "";  
  //    
  //   label3  
  //    
  this.label3.Location   =   new   System.Drawing.Point(14,   164);  
  this.label3.Name   =   "label3";  
  this.label3.Size   =   new   System.Drawing.Size(52,   23);  
  this.label3.TabIndex   =   7;  
  this.label3.Text   =   "IP地址:";  
  //    
  //   statusBar1  
  //    
  this.statusBar1.Location   =   new   System.Drawing.Point(0,   251);  
  this.statusBar1.Name   =   "statusBar1";  
  this.statusBar1.Size   =   new   System.Drawing.Size(337,   22);  
  this.statusBar1.TabIndex   =   9;  
  this.statusBar1.Text   =   "程序不处于数据接收状态";  
  //    
  //   Form1  
  //    
  this.AutoScaleBaseSize   =   new   System.Drawing.Size(5,   13);  
  this.ClientSize   =   new   System.Drawing.Size(337,   273);  
  this.Controls.AddRange(new   System.Windows.Forms.Control[]   {  
      this.statusBar1,  
      this.textBox2,  
      this.label3,  
      this.button2,  
      this.button1,  
      this.textBox1,  
      this.listBox1,  
      this.label2,  
      this.label1});  
  this.MaximizeBox   =   false;  
  this.Name   =   "Form1";  
  this.Text   =   "UDP聊天";  
  this.ResumeLayout(false);  
   
  }  
  #endregion  
   
  ///   <summary>  
  ///   应用程序的主入口点。  
  ///   </summary>  
  [STAThread]  
  static   void   Main   (   )    
  {  
  Application.Run   (   new   Form1   (   )   )   ;  
  }  
  private   void   read   (   )  
  {  
  //侦听本地的8000端口号  
  udpclient   =   new   UdpClient   (   8000   )   ;  
  remote   =   null   ;  
  //设定编码类型  
  Encoding   enc   =   Encoding.Unicode   ;  
  while   (   ReadFlag   ==   true   )  
  {  
  Byte[]   data   =   udpclient.Receive   (   ref   remote   )   ;  
  //得到对方发送来的聊天信息  
  String   strData   =   enc.GetString   (   data   )   ;  
  //获取传送信息到本地端口号的远程计算机IP地址  
  string   remoteIP   =   remote.Address.ToString   (   )   ;  
  //显示接收信息以及传送信息的计算机IP地址  
  listBox1.Items.Add   (   remoteIP   +   ":"   +   strData   )   ;  
  }  
  }  
  private   void   button1_Click   (   object   sender   ,   System.EventArgs   e   )  
  {  
  //创建UDP网络服务  
  UdpClient   SendUdp   =   new   UdpClient   (   )   ;    
  IPAddress   remoteIP   ;  
  //判断IP地址的正确性  
  try  
  {  
  remoteIP   =   IPAddress.Parse   (   textBox2.Text   );  
  }  
  catch  
  {  
  MessageBox.Show   (   "请输入正确的IP地址!"   ,   "错误"   )   ;  
  return   ;  
  }  
  IPEndPoint   remoteep   =   new   IPEndPoint   (   remoteIP   ,   8000   )   ;  
  Byte   []   buffer   =   null   ;  
  Encoding   enc   =   Encoding.Unicode   ;    
  string   str   =   textBox1.Text   ;  
  buffer   =   enc.GetBytes   (   str.ToCharArray   (   )   )   ;  
  //传送信息到指定计算机的8000端口号  
  SendUdp.Send   (   buffer   ,   buffer.Length   ,   remoteep   )   ;  
  textBox1.Clear   (   )   ;  
  //关闭UDP网络服务  
  SendUdp.Close   (   )   ;  
  }  
   
   
  private   void   button2_Click   (   object   sender   ,   System.EventArgs   e   )  
  {  
  //创建一个线程  
  th   =   new   Thread   (   new   ThreadStart   (   read   )   )   ;  
  //启动线程  
  th.Start   (   )   ;  
  statusBar1.Text   ="程序处于数据接收状态!"   ;  
  }  
  }  
  }  
   
  一个很简单的例子,希望能帮你!Top

相关问题

  • 关于即时通讯与UDP、P2P
  • UDP协议通讯的问题?
  • 请教:TCP/UDP通讯的问题
  • UDP/TCP网络通讯安全问题
  • 快快,123分求解UDP通讯的例程!!!!
  • 请问在C#中如何使用UDP进行通讯?
  • 想学通讯编程,先从哪着手?Socket?UDP/?ISAPI?
  • 急!!!急需 Indy UDP 通讯的资料或者代码!
  • UDP通讯中数据包最大可以多少字节啊?!
  • asp使用udp通讯的问题?先谢过大虾

关键词

  • .net
  • 远程
  • 聊天
  • 计算机
  • 信息
  • udpclient
  • label
  • listbox
  • textbox
  • remoteip

得分解答快速导航

  • 帖主:townee
  • lampson123

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo