仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 665|回复: 8
打印 上一主题 下一主题

[学习教程] ASP.NET网页编程之接纳UDP播送形式写复杂信息传输东西~

[复制链接]
活着的死人 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:31:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
我觉得很重要,一般所说的不重要应该指的是:你学好一种以后再学另一种就很容易了。(因为这样大家可能有一个错觉就是语言不是很重要,只要随便学一种就可以了,其实不是这样的。利用UDPClient类
这个是我写的测试代码,供参考~

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

namespaceUDPWinTest
{
///<summary>
///Form1的择要申明。
///</summary>
publicclassForm1:System.Windows.Forms.Form
{
privateSystem.Windows.Forms.ButtonbtnStart;
privateSystem.Windows.Forms.ButtonbtnJoin;
privateSystem.Windows.Forms.TextBoxtxtServerIP;
privateSystem.Windows.Forms.Buttonbutton1;
privateSystem.Windows.Forms.TextBoxtxtMsgSend;
privateSystem.Windows.Forms.TextBoxtxtMessageMain;
///<summary>
///必须的计划器变量。
///</summary>
privateSystem.ComponentModel.Containercomponents=null;

privatestaticUdpClientm_Client;

privatestaticintListenerPort=80;
privatestaticintSenderPort=80;
privatestaticintLocalPort;
privatestaticintRemotePort;

privatestaticstringm_szHostName;

privatestaticIPAddressm_GroupAddress;
privatestaticIPHostEntrym_LocalHost;
privatestaticIPEndPointm_RemoteEP;
privateSystem.Windows.Forms.ButtonbtnEnd;

privatestaticboolm_Done=false;
privateSystem.Windows.Forms.ButtonbtnClose;

privateThreadt=null;
privateThreadt2=null;

publicForm1()
{
InitializeComponent();
}

///<summary>
///清算一切正在利用的资本。
///</summary>
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

#regionWindows窗体计划器天生的代码
///<summary>
///计划器撑持所需的办法-不要利用代码编纂器修正
///此办法的内容。
///</summary>
privatevoidInitializeComponent()
{
this.btnStart=newSystem.Windows.Forms.Button();
this.btnJoin=newSystem.Windows.Forms.Button();
this.txtServerIP=newSystem.Windows.Forms.TextBox();
this.txtMessageMain=newSystem.Windows.Forms.TextBox();
this.txtMsgSend=newSystem.Windows.Forms.TextBox();
this.button1=newSystem.Windows.Forms.Button();
this.btnEnd=newSystem.Windows.Forms.Button();
this.btnClose=newSystem.Windows.Forms.Button();
this.SuspendLayout();
//
//btnStart
//
this.btnStart.Location=newSystem.Drawing.Point(16,16);
this.btnStart.Name="btnStart";
this.btnStart.TabIndex=0;
this.btnStart.Text="Start";
this.btnStart.Click+=newSystem.EventHandler(this.btnStart_Click);
//
//btnJoin
//
this.btnJoin.Location=newSystem.Drawing.Point(408,16);
this.btnJoin.Name="btnJoin";
this.btnJoin.TabIndex=1;
this.btnJoin.Text="Join";
this.btnJoin.Click+=newSystem.EventHandler(this.btnJoin_Click);
//
//txtServerIP
//
this.txtServerIP.Location=newSystem.Drawing.Point(304,16);
this.txtServerIP.Name="txtServerIP";
this.txtServerIP.Size=newSystem.Drawing.Size(96,21);
this.txtServerIP.TabIndex=2;
this.txtServerIP.Text="10.89.58.220";
//
//txtMessageMain
//
this.txtMessageMain.Location=newSystem.Drawing.Point(16,48);
this.txtMessageMain.Multiline=true;
this.txtMessageMain.Name="txtMessageMain";
this.txtMessageMain.Size=newSystem.Drawing.Size(464,184);
this.txtMessageMain.TabIndex=3;
this.txtMessageMain.Text="";
//
//txtMsgSend
//
this.txtMsgSend.Location=newSystem.Drawing.Point(16,240);
this.txtMsgSend.Name="txtMsgSend";
this.txtMsgSend.Size=newSystem.Drawing.Size(384,21);
this.txtMsgSend.TabIndex=4;
this.txtMsgSend.Text="";
//
//button1
//
this.button1.Location=newSystem.Drawing.Point(408,240);
this.button1.Name="button1";
this.button1.TabIndex=5;
this.button1.Text="Send";
this.button1.Click+=newSystem.EventHandler(this.Send_Click);
//
//btnEnd
//
this.btnEnd.Location=newSystem.Drawing.Point(112,16);
this.btnEnd.Name="btnEnd";
this.btnEnd.TabIndex=6;
this.btnEnd.Text="End";
this.btnEnd.Click+=newSystem.EventHandler(this.btnEnd_Click);
//
//btnClose
//
this.btnClose.Location=newSystem.Drawing.Point(208,16);
this.btnClose.Name="btnClose";
this.btnClose.TabIndex=7;
this.btnClose.Text="Close";
this.btnClose.Click+=newSystem.EventHandler(this.btnClose_Click);
//
//Form1
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.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]
staticvoidMain()
{
Application.Run(newForm1());
}

privatevoidbtnStart_Click(objectsender,System.EventArgse)
{
LocalPort=SenderPort;
RemotePort=ListenerPort;

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

AddToMain("LocalPort:"+LocalPort+",Remote:"+RemotePort);
AddToMain("Initializing...");

stringLocalHostIP=m_LocalHost.AddressList[0].ToString();
string[]ArrIP=LocalHostIP.Split(newchar[]{.});
LocalHostIP="224"+"."+ArrIP[1]+"."+ArrIP[2]+"."+ArrIP[3];

Initialize(LocalHostIP);

AddToMain("StartingListenerthread...");

//入手下手新线程
t=newThread(newThreadStart(Listener));//侦听线程
t.Start();

this.btnJoin.Enabled=false;
this.btnStart.Enabled=false;

}

privatevoidbtnJoin_Click(objectsender,System.EventArgse)
{
LocalPort=SenderPort;
RemotePort=ListenerPort;

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

AddToMain("LocalPort:"+LocalPort+",Remote:"+RemotePort);
AddToMain("Initializing...");

stringServerIP=this.txtServerIP.Text;
string[]ArrIP=ServerIP.Split(newchar[]{.});
ServerIP="224"+"."+ArrIP[1]+"."+ArrIP[2]+"."+ArrIP[3];

Initialize(ServerIP);

AddToMain("StartingListenerthread...");

//入手下手新线程
t=newThread(newThreadStart(Listener));//侦听线程
t.Start();

this.btnStart.Enabled=false;
this.btnJoin.Enabled=false;
}

privatevoidSend_Click(objectsender,System.EventArgse)
{
//入手下手新线程
t2=newThread(newThreadStart(Send));//侦听线程
t2.Start();
}

///<summary>
///
///</summary>
publicvoidSend()
{
Thread.Sleep(1000);

Byte[]buffer=null;

System.Text.EncodingASCII=System.Text.Encoding.ASCII;

Strings=this.txtMsgSend.Text;

if(s.Length==0)
return;

if(String.Compare(s,0,"@",0,1,true,CultureInfo.InvariantCulture)==0)
{
m_Done=true;
s=m_szHostName+":@";
Application.Exit();
}
else
{
s=m_szHostName+":"+s;
}


buffer=newByte[s.Length+1];

intlen=ASCII.GetBytes(s.ToCharArray(),0,s.Length,buffer,0);

intecode=m_Client.Send(buffer,len,m_RemoteEP);

if(ecode<=0)
{
AddToMain("Errorinsend:"+ecode);
}

t2.Abort();

}

publicstaticvoidTerminate()
{
m_Client.DropMulticastGroup(m_GroupAddress);
}

///<summary>
///初始化
///</summary>
publicvoidInitialize(stringIP)
{

m_Client=newUdpClient(LocalPort);

//设置播送组的收集地点
m_GroupAddress=IPAddress.Parse(IP);

try
{//到场播送组
m_Client.JoinMulticastGroup(m_GroupAddress,100);
}
catch(Exceptionex)
{
stringmm=ex.Message;
AddToMain("Unabletojoinmulticastgroup"+ex.Message);
}

//设置收集端点
m_RemoteEP=newIPEndPoint(m_GroupAddress,RemotePort);

}

///<summary>
///
///</summary>
publicvoidListener()
{

Thread.Sleep(2000);

System.Text.EncodingASCII=System.Text.Encoding.ASCII;


while(!m_Done)
{
IPEndPointendpoint=null;
Byte[]data=m_Client.Receive(refendpoint);

StringstrData=ASCII.GetString(data);

if(strData.IndexOf(":@")>0)
{
Char[]separators={:};
String[]vars=strData.Split(separators);

if(vars[0]==m_szHostName)//主体系已封闭
{
AddToMain("shuttingdownListenerthread...");
m_Done=true;
}
else
{
AddToMain(vars[0]+"haslefttheconversation");
}
}
else
{
if(strData.IndexOf(":")>0)
{
Char[]separators={:};
String[]vars=strData.Split(separators);

if(vars[0]!=m_szHostName)
{
AddToMain(strData);
}
else
{
AddToMain(strData);
}
}
}
}
t.Abort();

AddToMain("Listenerthreadfinished...");
return;
}

privatevoidAddToMain(string_Message)
{
this.txtMessageMain.Text=this.txtMessageMain.Text+_Message+"
";
}

privatevoidbtnEnd_Click(objectsender,System.EventArgse)
{
this.btnStart.Enabled=true;
this.btnJoin.Enabled=true;

try
{
t.Abort();
t.Interrupt();
t2.Abort();
t2.Interrupt();

AddToMain("Closingconnection...");

m_Client.DropMulticastGroup(m_GroupAddress);
}
catch
{}


//Terminate();
}

privatevoidbtnClose_Click(objectsender,System.EventArgse)
{
try
{
t.Abort();
t.Interrupt();
t2.Abort();
t2.Interrupt();
Terminate();
}
catch
{}
Application.Exit();
}
}
}
那做企业软件是不是最好用J2EE?
再现理想 该用户已被删除
沙发
发表于 2015-1-19 16:04:09 | 只看该作者
ASP.NET:ASP.net是Microsoft.net的一部分,作为战略产品,不仅仅是ActiveServerPage(ASP)的下一个版本;它还提供了一个统一的Web开发模型,其中包括开发人员生成企业级Web应用程序所需的各种服务。ASP.NET的语法在很大程度上与ASP兼容,同时它还提供一种新的编程模型和结构,可生成伸缩性和稳定性更好的应用程序,并提供更好的安全保护。
不帅 该用户已被删除
板凳
发表于 2015-1-25 21:59:27 | 只看该作者
ASP.net1.1和2.0在程序上的语法也有很大不同,现在2.0属于新出来的,不知道半年后会不会有3.0(说笑一下)。Windows2003系统自动支持ASP和ASP.net环境,不用安装任何程序。Asp.net属于编译语言。ASP的最大不同(ASP属于解释语言)。
愤怒的大鸟 该用户已被删除
地板
发表于 2015-2-4 06:51:08 | 只看该作者
逐步缩小出错代码段的范围,最终确定错误代码的位置。
山那边是海 该用户已被删除
5#
发表于 2015-2-9 18:04:12 | 只看该作者
现在主流的网站开发语言无外乎asp、php、asp.net、jsp等。
因胸联盟 该用户已被删除
6#
发表于 2015-2-27 15:11:51 | 只看该作者
主流网站开发语言之JSP:JSP和Servlet要放在一起讲,是因为它们都是Sun公司的J2EE(Java2platformEnterpriseEdition)应用体系中的一部分。
谁可相欹 该用户已被删除
7#
发表于 2015-3-9 07:56:55 | 只看该作者
那么,ASP.Net有哪些改进呢?
简单生活 该用户已被删除
8#
发表于 2015-3-16 21:12:21 | 只看该作者
以上是语言本身的弱点,在功能方面ASP同样存在问题,第一是功能太弱,一些底层操作只能通过组件来完成,在这点上是远远比不上PHP/JSP,其次就是缺乏完善的纠错/调试功能,这点上ASP/PHP/JSP差不多。
老尸 该用户已被删除
9#
发表于 2015-3-23 03:01:14 | 只看该作者
逐步缩小出错代码段的范围,最终确定错误代码的位置。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-6-19 00:40

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表