一、通讯基类
using System;
using System.Net.Sockets;
using System.Net ;
using System.IO ;
using System.Windows.Forms;
using System.Text;
namespace BaseClass
{
/// <summary
/// 传送信息的格式为 给定长度的命令部分+给定长度的命令注释部分+可变长度的长度信息+可变长度的信息部分
/// </summary
public class CommunClass
{
public CommunClass()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary
/// 命令部分的长度
/// </summary
private static readonly int CMDLEN = 50 ;
/// <summary
/// 命令注释部分的长度
/// </summary
private static readonly int DESCLEN = 100 ;
/// <summary
/// 可变长度的长度信息部分所占的字节数
/// </summary
private static readonly int DYNAMICLENGTHLEN = 10 ;
/// <summary
/// 每次处理可变信息部分的长度
/// </summary
private static readonly int DEALLEN = 1024 ;
/// <summary
/// /应答的最大长度
/// </summary
private static readonly int RESPONLEN = 20 ;
/// <summary
/// 用于填充命令或注释不足长度部分的字符
/// </summary
private static readonly char FILLCHAR = '^' ;
/// <summary
/// 成功发送一部分数据后的回调方法(也可以认为是触发的事件,但严格来说还不是)
/// </summary
public delegate void OnSend(int iTotal,int iSending) ;
/// <summary
/// 根据给定的服务器和端口号建立连接
/// </summary
/// <param name="strHost"服务器名</param
/// <param name="iPort"端口号</param
/// <returns</returns
public static 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
public static bool WriteTextToSocket(Socket s,string strInfo)
{
byte [] buf = Encoding.UTF8.GetBytes(strInfo) ;
try
{
s.Send(buf,0,buf.Length,SocketFlags.None) ;
return true ;
}
catch(Exception err)
{
MessageBox.Show("发送文本失败!"+err.Message) ;
return false ;
}
}
/// <summary
/// 将命令文本写到Socket中
/// </summary
/// <param name="s"要发送命令文本的Socket</param
/// <param name="strInfo"要发送的命令文本</param
/// <returns是否成功</returns
public static bool 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
public static bool 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
public static bool 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
public static bool 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.Sen