频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

采用UDP广播模式写简单信息传输工具~

来源: 作者: 出处:巧巧读书 2006-09-25 进入讨论组

  使用UDPClient类
这个是我写的测试代码,供参考~

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;

namespace UDPWinTest
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btnJoin;
private System.Windows.Forms.TextBox txtServerIP;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txtMsgSend;
private System.Windows.Forms.TextBox txtMessageMain;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

private static UdpClient m_Client;

private static int ListenerPort = 80;
private static int SenderPort = 80;
private static int LocalPort;
private static int RemotePort;

private static string m_szHostName;

private static IPAddress m_GroupAddress;
private static IPHostEntry m_LocalHost;
private static IPEndPoint m_RemoteEP;
private System.Windows.Forms.Button btnEnd;

private static bool m_Done = false;
private System.Windows.Forms.Button btnClose;

private Thread t = null ;
private Thread t2 = null ;

public Form1()
{
InitializeComponent();
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnStart = new System.Windows.Forms.Button();
this.btnJoin = new System.Windows.Forms.Button();
this.txtServerIP = new System.Windows.Forms.TextBox();
this.txtMessageMain = new System.Windows.Forms.TextBox();
this.txtMsgSend = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.btnEnd = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(16, 16);
this.btnStart.Name = "btnStart";
this.btnStart.TabIndex = 0;
this.btnStart.Text = "Start";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btnJoin
//
this.btnJoin.Location = new System.Drawing.Point(408, 16);
this.btnJoin.Name = "btnJoin";
this.btnJoin.TabIndex = 1;
this.btnJoin.Text = "Join";
this.btnJoin.Click += new System.EventHandler(this.btnJoin_Click);
//
// txtServerIP
//
this.txtServerIP.Location = new System.Drawing.Point(304, 16);
this.txtServerIP.Name = "txtServerIP";
this.txtServerIP.Size = new System.Drawing.Size(96, 21);
this.txtServerIP.TabIndex = 2;
this.txtServerIP.Text = "10.89.58.220";
//
// txtMessageMain
//
this.txtMessageMain.Location = new System.Drawing.Point(16, 48);
this.txtMessageMain.Multiline = true;
this.txtMessageMain.Name = "txtMessageMain";
this.txtMessageMain.Size = new System.Drawing.Size(464, 184);
this.txtMessageMain.TabIndex = 3;
this.txtMessageMain.Text = "";
//
// txtMsgSend
//
this.txtMsgSend.Location = new System.Drawing.Point(16, 240);
this.txtMsgSend.Name = "txtMsgSend";
this.txtMsgSend.Size = new System.Drawing.Size(384, 21);
this.txtMsgSend.TabIndex = 4;
this.txtMsgSend.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(408, 240);
this.button1.Name = "button1";
this.button1.TabIndex = 5;
this.button1.Text = "Send";
this.button1.Click += new System.EventHandler(this.Send_Click);
//
// btnEnd
//
this.btnEnd.Location = new System.Drawing.Point(112, 16);
this.btnEnd.Name = "btnEnd";
this.btnEnd.TabIndex = 6;
this.btnEnd.Text = "End";
this.btnEnd.Click += new System.EventHandler(this.btnEnd_Click);
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(208, 16);
this.btnClose.Name = "btnClose";
this.btnClose.TabIndex = 7;
this.btnClose.Text = "Close";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(504, 273);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnEnd);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtMsgSend);
this.Controls.Add(this.txtMessageMain);
this.Controls.Add(this.txtServerIP);
this.Controls.Add(this.btnJoin);
this.Controls.Add(this.btnStart);
this.Name = "Form1";
this.Text = "UDPWinTest";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void btnStart_Click(object sender, System.EventArgs e)
{
LocalPort = SenderPort;
RemotePort = ListenerPort;

m_szHostName = Dns.GetHostName();
m_LocalHost = Dns.GetHostByName(m_szHostName);

AddToMain("Loc请保留地址 http://www.qqread.com/dotnet/w230867.html 更多文章 更多内容请看UDP协议专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章