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

首頁 > 開發 > 綜合 > 正文

使用C#進行點對點通訊和文件傳輸(發送接收部分)

2024-07-21 02:19:40
字體:
來源:轉載
供稿:網友
菜鳥學堂:
上面介紹了通訊的基類,下面就是使用那個類進行發送和接收的部分:

二、發送部分:

發送咱們使用了多線程,可以同時進行多個任務,比如發送文件、發送文本等,互不影響:

發送文本方法:

private void startsendtext(string strhost,int iport,string strinfo)

{

sendtext sttext = new sendtext(strhost,iport,strinfo,new communclass.onsend(onsenddrawprogress)) ;

startthread(new threadstart(sttext.send)) ;

}

下面是他調用用到的一些方法:

開始一個線程

private void startthread(threadstart target)

{

thread dostep = new thread(target) ;

dostep.isbackground = true ;

dostep.start() ;

}

發送一部分(本文設置的是1024字節)成功后的回調方法

public void onsenddrawprogress(int itotal,int isending)

{

if (itotal != pbmain.maximum)

pbmain.maximum = itotal ;

pbmain.value = isending ;

}

因為使用的是線程,所以發送文本使用的是一個發送文本類的方法,該類如下:

public class sendtext

{

private string host ;

private int port ;

private string info ;

private communclass.onsend onsend ;

public sendtext(string strhost,int iport,string strinfo,

communclass.onsend onsend)

{

host = strhost ;

port = iport ;

info = strinfo ;

onsend = onsend ;

}

public void send()

{

socket s = null ;

try

{

s = communclass.connecttoserver(host,port) ;



communclass.writecommandtosocket(s,"sendtext") ;

communclass.writecommanddesctosocket(s,"") ;

communclass.writedynamictexttosocket(s,info,onsend) ;

}

catch (exception e)

{

messagebox.show(e.message) ;

}

finally

{

if (s != null)

s.close() ;

}

}



}//end class



這樣就可以使用一個線程發送文本了。

發送文件的方法也類似:

private void startsendfile(string strhost,int iport,string strfile)

{

sendfile sffile = new sendfile(strhost,iport,strfile,this.pbmain) ;

pbmain.value = 0 ;

startthread(new threadstart(sffile.send)) ;

}

發送文件的類:

public class sendfile

{

private string host ;

private int port ;

private string filetosend ;

private progressbar pbar;



public sendfile(string strhost,int iport,string strfile,progressbar pbmain)

{

host = strhost ;

port = iport ;

filetosend = strfile ;

pbar = pbmain ;

}

public void send()

{

socket s = null ;

try

{

s = communclass.connecttoserver(host,port) ;



communclass.writecommandtosocket(s,"sendfile") ;

communclass.writecommanddesctosocket(s,"") ;



communclass.writefiletosocket(s,filetosend,new communclass.onsend(onsenddrawprogress)) ;

}

catch (exception e)

{

messagebox.show(e.message) ;

}

finally

{

if (s != null)

s.close() ;

}

}





public void onsenddrawprogress(int itotal,int isending)

{

if (itotal != pbar.maximum)

pbar.maximum = itotal ;



pbar.value = isending ;

}



}//end class

當然,你發送一個命令讓服務器端啟動一個程序(靠,這不成木馬了嗎?)也可以:

俺這里只給出一部分代碼,其余的您自己可以發揮以下:

public class executefile

{

private string host ;

private int port ;

private string filename ;

private string cmdparam ;



public executefile(string strhost,int iport,string strfilename,string strcmdparam)

{

host = strhost ;

port = iport ;

filename = strfilename ;

cmdparam = strcmdparam ;

}



public void send()

{

socket s = null ;

try

{

s = communclass.connecttoserver(host,port) ;



communclass.writecommandtosocket(s,"executefile") ;

communclass.writecommanddesctosocket(s,filename) ;

communclass.writedynamictexttosocket(s,"",null) ;

messagebox.show(communclass.readdynamictextfromsocket(s)) ;

}

catch (exception e)

{

messagebox.show(e.message) ;

}

finally

{

if (s != null)

s.close() ;

}



}

}

三、下面是服務器端接受信息的代碼:

創建監聽:

/// <summary>

/// 再給定的主機和端口上創建監聽程序

/// </summary>

/// <param name="straddress"></param>

/// <param name="iport"></param>

private void buildingserver(string straddress,int iport)

{

ipaddress ipaddress = dns.resolve(straddress).addresslist[0];



try

{

listener = new tcplistener(ipaddress, iport);

}

catch ( exception e)

{

addinfo(e.message) ;

}

}


開始監聽:


/// <summary>

/// 開始監聽

/// </summary>

private void startlisten()

{

bool done = false;



listener.start();

while (!done)

{

socket s = listener.acceptsocket() ;

if(s != null)

{

dealwithsocket dws = new dealwithsocket(s,this.tblog) ;

startthread(new threadstart(dws.dealwith)) ;

}

}

}



private void startthread(threadstart target)

{

thread dostep = new thread(target) ;

dostep.isbackground = true ;

dostep.start() ;

}



開始監聽后,對于每一個監聽到的客戶端的連接都用一個單獨的線程來處理,處理通過類dealwithsocket來完成,下面是類代碼:

public class dealwithsocket

{

private socket s = null ;

private textbox tblog = null ;

public dealwithsocket(socket newsocket,textbox tbinfo)

{

s = newsocket ;

tblog = tbinfo ;

}



public void dealwith()

{

string strcmd = communclass.readcommandfromsocket(s) ;

string strdesc = communclass.readcommanddescfromsocket(s) ;

addinfo(strcmd) ;

switch(strcmd)

{

case "sendfile" :

communclass.readdynamicfilefromsocket(s,"e://rrr.txt") ;

break ;

case "executefile" :

string strparam = communclass.readdynamictextfromsocket(s) ;

string strresult = executefile(strdesc,strparam) ;

communclass.writedynamictexttosocket(s,strresult,null) ;

break ;

default:

string strdetail = communclass.readdynamictextfromsocket(s) ;

addinfo(strdetail) ;

break ;

}

try

{

s.close() ;

}

catch (exception e)

{

addinfo(e.message) ;

}

}

private void addinfo(string strinfo)

{

string info = datetime.now.tolongtimestring() + " "+ strinfo +"/r/n" ;

tblog.text += info ;

tblog.refresh() ;

}

private string executefile(string strfilename,string strcmdparam)

{

system.diagnostics.process proc = new system.diagnostics.process() ;

proc.startinfo.filename = strfilename ;

proc.startinfo.arguments = strcmdparam ;

try

{

proc.start() ;

return "ok" ;

}

catch(exception err)

{

return err.message ;

}

}

}//end class

以上就是所用的代碼,希望大家批判指正.
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 义马市| 宜州市| 夏邑县| 新绛县| 温宿县| 莎车县| 东乡族自治县| 民乐县| 晋中市| 莱阳市| 百色市| 西畴县| 阳谷县| 青浦区| 仁寿县| 定边县| 泊头市| 潞城市| 麦盖提县| 板桥市| 广灵县| 大安市| 灵武市| 巴里| 海口市| 赞皇县| 兴仁县| 沅陵县| 买车| 安庆市| 泗水县| 富锦市| 诸暨市| 永福县| 民县| 岳普湖县| 南城县| 应用必备| 巴彦淖尔市| 厦门市| 商城县|