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
以上就是所用的代码,希望大家批判指正.