SanNiuSignal使用文档:
/*
* using SanNiuSignal;
* using SanNiuSignal.FileCenter;
* using SanNiuSignal.FileCenter.FileBase;
* SanNiuSignal学习笔记
* 1:ITxServer TCP服务器接口
* 2:ITxClient TCP客户端接口
* 3:IUdpTxUDP 接口
* 4:IFileSend 文件发送方接口
* 5:IFileReceive 文件接收方接口 前面三个接口是通过TxStart启动;后面二个文件接口是通过FileStart启动
* 前面三个接口是通过TxStart启动;
* 后面二个文件接口 是通过FileStart启动
*/
class Socket_yb : IFileSendMust
{
public void socket()
{
#region 服务端
ITxServer txServer = null;//服务器的所有方法和时间
#region ITxServer TCP服务端接口属性
txServer = TxStart.startServer(12000); //注册一个端口为12000的服务器,返回ITxServer类,再从ITxServer中的startServer一个方法启动服务器
txServer.ClientMax = 1000; //设置客户端最大连接数
txServer.BufferSize = 1024 * 10; //设置缓冲区大小
List<IPEndPoint> list = txServer.ClientAll; //得到所有客户端
int count = txServer.ClientNumber; //得到当前客户端数量
txServer.Ip = "192.168.1.192"; //启动IP地址
txServer.Port = 3306; //启动端口地址
txServer.FileLog = "C:\test.txt"; //释放记录引擎历史记录,为空记录
txServer.StartEngine(); //启动引擎
#endregion
#region ITxServer TCP服务端接口事件
txServer.AcceptString += TxServer_AcceptString; //接受字符串时触发
txServer.AcceptByte += TxServer_AcceptByte; //接受图片时触发
txServer.Connect += TxServer_Connect; //当有新客户端连接时触发
txServer.dateSuccess += TxServer_dateSuccess; //当数据发送成功且对方已接收时触发
txServer.Disconnection += TxServer_Disconnection; //当有客户端突然断开时触发
txServer.EngineClose += TxServer_EngineClose; //当引擎完全关闭释放资源是触发
txServer.EngineLost += TxServer_EngineLost; //当引擎非正常原因自动断开连接时触发事件
#endregion
#region 方法
txServer.clientAllClose(); //关闭所有客户端连接
txServer.clientCheck(list[0]); //检查某个客户端是否在线
txServer.clientClose(list[0]); //强制关闭一个客户端
txServer.CloseEngine(); //关闭引擎
txServer.ContinueFile(list[0], 100); //对文件进行续传,如果有不正确会抛出异常
txServer.sendMessage(list[0], Encoding.UTF8.GetBytes("")); //向客户端发送文本信息
txServer.StartEngine(); //启动引擎
txServer.SendFile(list[0], "path"); //发送文件;如果地址不正确会抛出异常
#endregion
#endregion
#region 文件
IFileReceive FileReceive = null; //文件接收接口
IFileSend FileSend = null; //文件发送接口,继承文件基础接口IFilebas
FileSend = FileStart.StartFileSend(this);
#region 文件接收
//属性
FileReceive.BufferSize = 1024 * 20; //设置文件缓冲区大小,默认=8192字节,UDP<=65507,发送和接收缓冲区大小需相同,缓冲区越大发送越快,内存消耗越大
int[] i = FileReceive.FileAllOn; //得到正在处理的文件总数
//方法
FileReceive.FileStop(1); //暂停发送文件
FileReceive.GetFileLenth(1); //得到文件总长度(单位:字节)
FileReceive.GetFileName(1); //得到文件名
FileReceive.GetFileOk(1); //得到文件已处理的进度,单位:字节
FileReceive.GetFileState(1); //得到文件状态:0:等待,1:传输,2:暂停
#endregion
#region 文件发送
//属性
FileSend.BufferSize = 1024 * 20; //设置文件缓冲区大小,默认=8192字节,UDP<=65507,发送和接收缓冲区大小需相同,缓冲区越大发送越快,内存消耗越大
int[] z = FileSend.FileAllOn; //得到正在处理的文件总数
//方法
FileReceive.FileStop(1); //暂停发送文件
FileReceive.GetFileLenth(1); //得到文件总长度(单位:字节)
FileReceive.GetFileName(1); //得到文件名
FileReceive.GetFileOk(1); //得到文件已处理的进度,单位:字节
FileReceive.GetFileState(1); //得到文件状态:0:等待,1:传输,2:暂停
#endregion
#endregion
#region 客户端
ITxClient TxClient = null; //创建客户端接口
TxClient = TxStart.startClient("ip", 8080);//注册一个客户端,返回一个ITxServer类,在从高ITxClient中的startClient一个方法启动客户端
TxClient.AcceptString += TxClient_AcceptString;//当收到文本数据的时候
TxClient.dateSuccess += TxClient_dateSuccess;//当对方已经成功收到我数据的时候
TxClient.EngineClose += TxClient_EngineClose;//当客户端引擎完全关闭释放资源的时候
TxClient.EngineLost += TxClient_EngineLost;//当客户端非正常原因断开的时候
TxClient.ReconnectionStart += TxClient_ReconnectionStart;//当自动重连开始的时候
TxClient.StartResult += TxClient_StartResult;//当登录完成的时候
#region 属性
TxClient.BufferSize = 1; //这里大小自己设置,默认为1KB,也就是1024个字节
bool b = TxClient.EngineStart; //引擎是否启动
TxClient.FileLog = "path"; //是否记录引擎历史日志,path保存路径,为空不记录
TxClient.Ip = "ip"; //启动ip地址
TxClient.OutTime = 20; //登录超时时间,单位:s
TxClient.Port = 8080; //启动端口
TxClient.ReconnectMax = 10; //设置断线时重连次数,默认为3次,为0不进行重连
#endregion
#region 方法
TxClient.CloseEngine(); //关闭引擎
TxClient.ContinueFile(1); //对文件进行续传,不正确跑出异常
TxClient.SendFile("fileName"); //发送文件,如果地址不正确抛出异常,首先要调用FIleStart启动文件系统
TxClient.sendMessage("byte[] buffer"); //客户端向服务端发送文本信息
TxClient.StartEngine(); //启动引擎
#endregion
#endregion
}
#region 客户端事件
private void TxClient_StartResult(bool object1, string object2)
{
throw new NotImplementedException();
}
private void TxClient_ReconnectionStart()
{
throw new NotImplementedException();
}
private void TxClient_EngineLost(string object1)
{
throw new NotImplementedException();
}
private void TxClient_EngineClose()
{
throw new NotImplementedException();
}
private void TxClient_dateSuccess(IPEndPoint object1)
{
throw new NotImplementedException();
}
private void TxClient_AcceptString(IPEndPoint object1, string object2)
{
throw new NotImplementedException();
}
#endregion
#region 服务端事件
private void TxServer_EngineLost(string object1)
{
throw new NotImplementedException();
}
private void TxServer_EngineClose()
{
throw new NotImplementedException();
}
private void TxServer_Disconnection(IPEndPoint object1, string object2)
{
throw new NotImplementedException();
}
private void TxServer_dateSuccess(IPEndPoint object1)
{
throw new NotImplementedException();
}
private void TxServer_Connect(IPEndPoint object1)
{
throw new NotImplementedException();
}
private void TxServer_AcceptByte(IPEndPoint object1, byte[] object2)
{
throw new NotImplementedException();
}
private void TxServer_AcceptString(IPEndPoint object1, string object2)
{
throw new NotImplementedException();
}
#endregion
#region 实现接口IFileSendMust方法
#region IFileSendMust成员
/// <summary>
/// 发送完成
/// </summary>
/// <param name="FileLabel">文件标签</param>
void IFileSendMust.SendSuccess(int FileLabel)
{
throw new NotImplementedException();
}
/// <summary>
/// 开始传输
/// </summary>
/// <param name="FileLabel">文件标签</param>
void IFileSendMust.FileStartOn(int FileLabel)
{
throw new NotImplementedException();
}
/// <summary>
/// 接收方拒绝接收文件
/// </summary>
/// <param name="FileLabel">文件标签</param>
void IFileSendMust.FileRefuse(int FileLabel)
{
throw new NotImplementedException();
}
#endregion
#region IFileMustBase成员
/// <summary>
/// 对方已经取消这个文件的传输,我方已经关闭这个传输
/// </summary>
/// <param name="FileLabel">文件标签</param>
void IFileMustBase.FileCancel(int FileLabel)
{
throw new NotImplementedException();
}
/// <summary>
/// 对方暂停;我方也已经暂停;等待着对方的再一次请求传输;会在FileOrNotContingue这里触发
/// </summary>
/// <param name="FileLabel">文件标签</param>
void IFileMustBase.FileStop(int FileLabel)
{
throw new NotImplementedException();
}
/// <summary>
/// 文件已中断;状态已自动改为暂停状态;等待对方上线的时候;进行续传;
/// </summary>
/// <param name="FileLabel">文件标签</param>
/// <param name="Reason">中断原因</param>
void IFileMustBase.FileBreak(int FileLabel, string Reason)
{
throw new NotImplementedException();
}
/// <summary>
/// 文件传输失败
/// </summary>
/// <param name="FileLabel">文件标签</param>
void IFileMustBase.FileFailure(int FileLabel)
{
throw new NotImplementedException();
}
/// <summary>
/// 文件开始续传;这时不会触发开始传输的方法
/// </summary>
/// <param name="FileLabel">文件标签</param>
void IFileMustBase.FileContinue(int FileLabel)
{
throw new NotImplementedException();
}
/// <summary>
/// 对方发过来的续传确认信息;你是否同意续传;
/// </summary>
/// <param name="FileLabel">文件标签</param>
/// <returns>同意或不同意</returns>
bool IFileMustBase.FileOrNotContingue(int FileLabel)
{
throw new NotImplementedException();
}
/// <summary>
/// 对方拒绝续传;文件又处于暂停状态;
/// </summary>
/// <param name="FileLabel">文件标签</param>
void IFileMustBase.FileNoContinue(int FileLabel)
{
throw new NotImplementedException();
}
/// <summary>
/// 得到文件的进度;每次缓冲区为单位折算成百分比输出进度;这样可以提高效率;
/// </summary>
/// <param name="FileLabel">文件标签</param>
/// <param name="Progress">进度</param>
void IFileMustBase.FileProgress(int FileLabel, int Progress)
{
throw new NotImplementedException();
}
#endregion
#endregion
}