首页 > 开发 > 综合 > 正文

使用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

以上就是所用的代码,希望大家批判指正.
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表