采用UDP广播模式写简单信息传输工具~
2024-07-21 02:30:05
供稿:网友
使用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("local port: " + localport + ", remote: " + remoteport);
addtomain("initializing...");
string localhostip = m_localhost.addresslist[0].tostring();
string[] arrip = localhostip.split(new char[]{'.'});
localhostip = "224" + "." + arrip[1] + "." + arrip[2] + "." + arrip[3];
initialize(localhostip);
addtomain("starting listener thread..." );
//开始新线程
t = new thread(new threadstart(listener)); //侦听线程
t.start();
this.btnjoin.enabled = false;
this.btnstart.enabled = false;
}
private void btnjoin_click(object sender, system.eventargs e)
{
localport = senderport;
remoteport = listenerport;
m_szhostname = dns.gethostname();
m_localhost = dns.gethostbyname(m_szhostname);
addtomain("local port: " + localport + ", remote: " + remoteport);
addtomain("initializing...");
string serverip = this.txtserverip.text;
string[] arrip = serverip.split(new char[]{'.'});
serverip = "224" + "." + arrip[1] + "." + arrip[2] + "." + arrip[3];
initialize(serverip);
addtomain("starting listener thread...");
//开始新线程
t = new thread(new threadstart(listener)); //侦听线程
t.start();
this.btnstart.enabled = false;
this.btnjoin.enabled = false;
}
private void send_click(object sender, system.eventargs e)
{
//开始新线程
t2 = new thread(new threadstart(send)); //侦听线程
t2.start();
}
/// <summary>
///
/// </summary>
public void send()
{
thread.sleep(1000);
byte [] buffer = null;
system.text.encoding ascii = system.text.encoding.ascii;
string s = 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 = new byte[s.length + 1];
int len = ascii.getbytes( s.tochararray(), 0, s.length, buffer, 0);
int ecode = m_client.send(buffer, len, m_remoteep);
if(ecode <= 0)
{
addtomain("error in send : " + ecode);
}
t2.abort();
}
public static void terminate()
{
m_client.dropmulticastgroup(m_groupaddress);
}
/// <summary>
/// 初始化
/// </summary>
public void initialize(string ip)
{
m_client = new udpclient(localport);
//设置广播组的网络地址
m_groupaddress = ipaddress.parse(ip);
try
{ //加入广播组
m_client.joinmulticastgroup(m_groupaddress, 100);
}
catch(exception ex)
{
string mm = ex.message ;
addtomain("unable to join multicast group" + ex.message);
}
//设置网络端点
m_remoteep = new ipendpoint( m_groupaddress, remoteport );
}
/// <summary>
///
/// </summary>
public void listener()
{
thread.sleep(2000);
system.text.encoding ascii = system.text.encoding.ascii;
while(!m_done)
{
ipendpoint endpoint = null;
byte[] data = m_client.receive(ref endpoint);
string strdata = ascii.getstring(data);
if( strdata.indexof(":@") > 0 )
{
char [] separators = {':'};
string [] vars = strdata.split(separators);
if( vars[0] == m_szhostname ) //主系统已关闭
{
addtomain("shutting down listener thread...");
m_done = true;
}
else
{
addtomain( vars[0] + " has left the conversation");
}
}
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("listener thread finished...");
return;
}
private void addtomain(string _message)
{
this.txtmessagemain.text = this.txtmessagemain.text + _message + "/r/n";
}
private void btnend_click(object sender, system.eventargs e)
{
this.btnstart.enabled = true;
this.btnjoin.enabled = true;
try
{
t.abort();
t.interrupt();
t2.abort();
t2.interrupt();
addtomain("closing connection...");
m_client.dropmulticastgroup(m_groupaddress);
}
catch
{}
//terminate();
}
private void btnclose_click(object sender, system.eventargs e)
{
try
{
t.abort();
t.interrupt();
t2.abort();
t2.interrupt();
terminate() ;
}
catch
{}
application.exit();
}
}
}
网站运营seo文章大全提供全面的站长运营经验及seo技术!