国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

采用UDP廣播模式寫簡(jiǎn)單信息傳輸工具~

2024-07-21 02:30:05
字體:
供稿:網(wǎng)友
使用udpclient類
這個(gè)是我寫的測(cè)試代碼,供參考~

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>
/// 必需的設(shè)計(jì)器變量。
/// </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 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </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>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </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);

//設(shè)置廣播組的網(wǎng)絡(luò)地址
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);
}

//設(shè)置網(wǎng)絡(luò)端點(diǎn)
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 ) //主系統(tǒng)已關(guān)閉
{
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();
}
}
}
  • 網(wǎng)站運(yùn)營(yíng)seo文章大全
  • 提供全面的站長(zhǎng)運(yùn)營(yíng)經(jīng)驗(yàn)及seo技術(shù)!
  • 發(fā)表評(píng)論 共有條評(píng)論
    用戶名: 密碼:
    驗(yàn)證碼: 匿名發(fā)表
    主站蜘蛛池模板: 卢龙县| 噶尔县| 普定县| 兴安盟| 博白县| 漳州市| 梁河县| 江川县| 拉萨市| 库伦旗| 石台县| 株洲市| 马公市| 衡水市| 任丘市| 贺兰县| 当雄县| 措美县| 团风县| 于都县| 蒙城县| 昆明市| 绥滨县| 揭阳市| 嘉义市| 金平| 泗洪县| 潮州市| 阳东县| 横峰县| 嘉祥县| 依兰县| 景德镇市| 嘉定区| 长泰县| 读书| 开化县| 潮安县| 吴旗县| 江安县| 长治县|