上面介紹了通訊的基類,下面就是使用那個(gè)類進(jìn)行發(fā)送和接收的部分:
二、發(fā)送部分:
發(fā)送咱們使用了多線程,可以同時(shí)進(jìn)行多個(gè)任務(wù),比如發(fā)送文件、發(fā)送文本等,互不影響:
發(fā)送文本方法:
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)) ;
}
下面是他調(diào)用用到的一些方法:
開(kāi)始一個(gè)線程
private void startthread(threadstart target)
{
thread dostep = new thread(target) ;
dostep.isbackground = true ;
dostep.start() ;
}
發(fā)送一部分(本文設(shè)置的是1024字節(jié))成功后的回調(diào)方法
public void onsenddrawprogress(int itotal,int isending)
{
if (itotal != pbmain.maximum)
pbmain.maximum = itotal ;
pbmain.value = isending ;
}
因?yàn)槭褂玫氖蔷€程,所以發(fā)送文本使用的是一個(gè)發(fā)送文本類的方法,該類如下:
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
這樣就可以使用一個(gè)線程發(fā)送文本了。
發(fā)送文件的方法也類似:
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)) ;
}
發(fā)送文件的類:
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
當(dāng)然,你發(fā)送一個(gè)命令讓服務(wù)器端啟動(dòng)一個(gè)程序(靠,這不成木馬了嗎?)也可以:
俺這里只給出一部分代碼,其余的您自己可以發(fā)揮以下:
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() ;
}
}
}
三、下面是服務(wù)器端接受信息的代碼:
創(chuàng)建監(jiān)聽(tīng):
/// <summary>
/// 再給定的主機(jī)和端口上創(chuàng)建監(jiān)聽(tīng)程序
/// </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) ;
}
}
開(kāi)始監(jiān)聽(tīng):
/// <summary>
/// 開(kāi)始監(jiān)聽(tīng)
/// </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() ;
}
開(kāi)始監(jiān)聽(tīng)后,對(duì)于每一個(gè)監(jiān)聽(tīng)到的客戶端的連接都用一個(gè)單獨(dú)的線程來(lái)處理,處理通過(guò)類dealwithsocket來(lái)完成,下面是類代碼:
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
以上就是所用的代碼,希望大家批判指正.