一、通讯基类
using
System;
using
System.net.Sockets;
using
System.
Net
;
using
System.IO ;
using
System.Windows.Forms;
using
System.Text;
namespace
BaseClass
{
///<summary>
///
传送
信息
的格式为 给定长度的命令部分+给定长度的命令注释部分+可变长度的长度信息+可变长度的信息部分
///</summary>
publicclass CommunClass
{
public CommunClass()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
///<summary>
/// 命令部分的长度
///</summary>
privatestaticreadonlyint CMDLEN = 50 ;
///<summary>
/// 命令注释部分的长度
///</summary>
privatestaticreadonlyint DESCLEN = 100 ;
///<summary>
/// 可变长度的长度信息部分所占的字节数
///</summary>
privatestaticreadonlyint DYNAMICLENGTHLEN = 10 ;
///<summary>
/// 每次处理可变信息部分的长度
///</summary>
privatestaticreadonlyint DEALLEN = 1024 ;
///<summary>
/// /应答的最大长度
///</summary>
privatestaticreadonlyint RESPONLEN = 20 ;
///<summary>
/// 用于填充命令或注释不足长度部分的字符
///</summary>
privatestaticreadonlychar FILLCHAR = '^' ;
///<summary>
///
成功发送一部分
数据
后的回调方法(也可以认为是触发的事件,但严格来说还不是)
///</summary>
publicdelegatevoid OnSend(int iTotal,int iSending) ;
///<summary>
/// 根据给定的服务器和端口号建立连接
///</summary>
///<param name="strHost">服务器名</param>
///<param name="iPort">端口号</param>
///<returns></returns>
publicstatic Socket ConnectToServer(string strHost,int iPort)
{
try
{
IPAddress ipAddress = Dns.Resolve(strHost).AddressList[0];
IPEndPoint ipPoint = new IPEndPoint(ipAddress,iPort) ;
Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp) ;
s.Connect(ipPoint) ;
return s ;
}
catch (Exception e)
{
throw (new Exception("建立到服务器的连接出错" + e.Message)) ;
}
}
///<summary>
/// 将文本写到Socket中
///</summary>
///<param name="s">要发送信息的Socket</param>
///<param name="strInfo">要发送的信息</param>
///<returns>是否成功</returns>
publicstaticbool WriteTextToSocket(Socket s,string strInfo)
{
byte [] buf = Encoding.UTF8.GetBytes(strInfo) ;
try
{
s.Send(buf,0,buf.Length,SocketFlags.None) ;
returntrue ;
}
catch(Exception err)
{
MessageBox.Show("发送文本失败!"+err.Message) ;
returnfalse ;
}
}
///<summary>
/// 将命令文本写到Socket中
///</summary>
///<param name="s">要发送命令文本的Socket</param>
///<param name="strInfo">要发送的命令文本</param>
///<returns>是否成功</returns>
publicstaticbool WriteCommandToSocket(Socket s,string strCmd)
{
if (strCmd == "")
strCmd = "NOP" ;
strCmd = strCmd.PadRight(CMDLEN,FILLCHAR) ;
return WriteTextToSocket(s,strCmd) ;
}
///<summary>
/// 将命令注释写到Socket中
///</summary>
///<param name="s">要发送命令注释的Socket</param>
///<param name="strInfo">要发送的命令注释</param>
///<returns>是否成功</returns>
publicstaticbool WriteCommandDescToSocket(Socket s,string strDesc)
{
if (strDesc == "")
strDesc = "0" ;
strDesc = strDesc.PadRight(DESCLEN,FILLCHAR) ;
return WriteTextToSocket(s,strDesc) ;
}
///<summary>
/// 发送可变信息的字节数
///</summary>
///<param name="s">要发送字节数的Socket</param>
///<param name="iLen">字节数</param>
///<returns>是否成功</returns>
publicstaticbool WriteDynamicLenToSocket(Socket s,int iLen)
{
string strLen = iLen.ToString().PadRight(DYNAMICLENGTHLEN,FILLCHAR) ;
return WriteTextToSocket(s,strLen) ;
}
///<summary>
/// 将缓存的指定部分发送到Socket
///</summary>
///<param name="s">要发送缓存的Socket</param>
///<param name="buf">要发送的缓存</param>
///<param name="iStart">要发送缓存的起始位置</param>
///<param name="iCount">要发送缓存的字节数</param>
///<param name="iBlock">每次发送的字节说</param>
///<param name="SendSuccess">每次发送成功后的回调函数</param>
///<returns>是否发送成功</returns>
publicstaticbool WriteBufToSocket(Socket s,byte [] buf,int iStart,int iCount,int iBlock,OnSend SendSuccess)
{
int iSended = 0 ;
int iSending = 0 ;
while(iSended<iCount)
{
if (iSended + iBlock <= iCount)
iSending = iBlock ;
else
iSending = iCount - iSended ;
s.Send(buf,iStart+iSended,iSending,SocketFlags.None) ;
iSended += iSending ;
if (ReadResponsionFromSocket(s)=="OK")
if (SendSuccess != null)
SendSuccess(iCount,iSended) ;
else
returnfalse;
}
returntrue ;
}
///<summary>
/// 将长度不固定文本发送到socket
///</summary>
///<param name="s">要发送文本的Socket</param>
///<param name="strText">要发送的文本</param>
///<param name="OnSendText">成功发送一部分文本后的回调函数</param>
///<param name="settextlen">得到文本长度的回调函数</param>
///<returns></returns>
publicstaticbool WriteDynamicTextToSocket(Socket s,string strText,
OnSend OnSendText)
{
byte [] buf = Encoding.UTF8.GetBytes(strText) ;
int iLen = buf.Length ;
try
{
WriteDynamicLenToSocket(s,iLen) ;
return WriteBufToSocket(s,buf,0,iLen,DEALLEN,OnSendText) ;
}
catch(Exception err)
{
MessageBox.Show("发送文本失败!"+err.Message) ;
returnfalse ;
}
}
///<summary>
/// 将文件写到Socket
///</summary>
///<param name="s">要发送文件的Socket</param>
///<param name="strFile">要发送的文件</param>
///<returns>是否成功</returns>
publicstaticbool WriteFileToSocket(Socket s,string strFile,
OnSend OnSendFile)
{
FileStream fs = new FileStream(strFile,FileMode.Open,FileAccess.Read,FileShare.Read) ;
int iLen = (int)fs.Length ;
WriteDynamicLenToSocket(s,iLen) ;
byte [] buf = newbyte[iLen] ;
try
{
fs.Read(buf,0,iLen) ;
return WriteBufToSocket(s,buf,0,iLen,DEALLEN,OnSendFile) ;
}
catch(Exception err)
{
MessageBox.Show("发送文件失败!"+err.Message) ;
returnfalse ;
}
finally
{
fs.Close() ;
}
}
///<summary>
///
对方对自己
消息
的简单回应
///</summary>
///<param name="s"></param>
///<returns></returns>
publicstaticstring ReadResponsionFromSocket( Socket s)
{
byte [] bufCmd = newbyte[RESPONLEN] ;
int iCount = s.Receive(bufCmd) ;
string strRespon = Encoding.UTF8.GetString(bufCmd,0,iCount) ;
return strRespon ;
}
///<summary>
/// 从Socket读取命令
///</summary>
///<param name="s">要读取命令的Socket</param>
///<returns>读取的命令</returns>
publicstaticstring ReadCommandFromSocket( Socket s)
{
byte [] bufCmd = newbyte[CMDLEN] ;
int iCount = s.Receive(bufCmd,0,CMDLEN,SocketFlags.Partial) ;
string strCommand = Encoding.UTF8.GetString(bufCmd,0,CMDLEN) ;
return strCommand = strCommand.TrimEnd(FILLCHAR) ;
}
///<summary>
/// 读取命令注释
///</summary>
///<param name="s">要读取命令注释的Socket</param>
///<returns>读取的命令注释</returns>
publicstaticstring ReadCommandDescFromSocket( Socket s)
{
byte [] bufCmd = newbyte[DESCLEN] ;
int iCount = s.Receive(bufCmd,0,DESCLEN,SocketFlags.Partial) ;
string strCommand = Encoding.UTF8.GetString(bufCmd,0,DESCLEN) ;
return strCommand = strCommand.TrimEnd(FILLCHAR) ;
}
///<summary>
/// 读取可变部分的长度
///</summary>
///<param name="s">要读取可变部分长度的Socket</param>
///<returns>读取的可变部分的长度</returns>
publicstaticint ReadDynamicLenFromSocket( Socket s)
{
byte [] bufCmd = newbyte[DYNAMICLENGTHLEN] ;
int iCount = s.Receive(bufCmd,0,DYNAMICLENGTHLEN,SocketFlags.Partial) ;
string strCommand = Encoding.UTF8.GetString(bufCmd,0,DYNAMICLENGTHLEN) ;
returnint.Parse(strCommand.TrimEnd(FILLCHAR)) ;
}
///<summary>
/// 读取文本形式的可变信息
///</summary>
///<param name="s">要读取可变信息的Socket</param>
///<returns>读取的可变信息</returns>
publicstaticstring ReadDynamicTextFromSocket( Socket s)
{
int iLen = ReadDynamicLenFromSocket(s) ;
byte [] buf = newbyte[iLen] ;
string strInfo = "" ;
int iReceiveded = 0 ;
int iReceiveing = 0 ;
while(iReceiveded<iLen)
{
if (iReceiveded + DEALLEN <= iLen)
iReceiveing = DEALLEN ;
else
iReceiveing = iLen - iReceiveded ;
s.Receive(buf,iReceiveded,iReceiveing,SocketFlags.None) ;
CommunClass.WriteTextToSocket(s,"OK") ;
iReceiveded+= iReceiveing ;
}
strInfo = Encoding.UTF8.GetString(buf,0,iLen) ;
return strInfo ;
}
///<summary>
/// 读取文件形式的可变信息
///</summary>
///<param name="s">要读取可变信息的Socket</param>
///<param name="strFile">读出后的文件保存位置</param>
///<returns>是否读取成功</returns>
publicstaticbool ReadDynamicFileFromSocket( Socket s,string strFile)
{
int iLen = ReadDynamicLenFromSocket(s) ;
byte [] buf = newbyte[iLen] ;
FileStream fs = new FileStream(strFile,FileMode.Create,FileAccess.Write) ;
try
{
int iReceiveded = 0 ;
int iReceiveing = 0 ;
while(iReceiveded<iLen)
{
if (iReceiveded + DEALLEN <= iLen)
iReceiveing = DEALLEN ;
else
iReceiveing = iLen - iReceiveded ;
s.Receive(buf,iReceiveded,iReceiveing,SocketFlags.None) ;
CommunClass.WriteTextToSocket(s,"OK") ;
iReceiveded+= iReceiveing ;
}
fs.Write(buf,0,iLen) ;
returntrue ;
}
catch(Exception err)
{
MessageBox.Show("接收文件失败"+err.Message) ;
returnfalse ;
}
finally
{
fs.Close() ;
}
}
}//end class
}//end namespace
上面介绍了通讯的基类,下面就是使用那个类进行发送和接收的部分:
二、发送部分:
发送咱们使用了多线程,可以同时进行多个任务,比如发送文件、发送文本等,互不影响:
发送文本方法:
private void StartSendText(string strHost,int iPort,string strInfo)
XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />
{
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