一.tcpsvr的使用方法
a.测试程序:
using system;
using ibms.net.tcpcsframework;
using system.collections;
using system.net.sockets;
namespace ibms.test
{
/// <summary>
/// 测试tcpsvr的类
/// </summary>
public class testtcpsvr
{
public testtcpsvr()
{
}
public static void main()
{
try
{
console.writeline("begin to test tcpsvr class…");
testtcpsvr tts = new testtcpsvr();
//tcpsvr svr = new tcpsvr(9050,4);//默认使用encoding.default编码方式
tcpsvr svr = new tcpsvr(9050,4,new coder(coder.encodingmothord.utf8));
svr.resovlver = new datagramresolver("##");
//定义服务器的4个事件
//服务器满
svr.serverfull += new netevent(tts.serverfull);
//新客户端连接
svr.clientconn += new netevent(tts.clientconn);
//客户端关闭
svr.clientclose += new netevent(tts.clientclose);
//接收到数据
svr.recvdata += new netevent(tts.recvdata);
//命令控制循环
while(true)
{
console.write(">");
string cmd=console.readline();
//退出测试程序
if(cmd.tolower() == "exit")
{
break;
}
//停止服务器程序
if(cmd.tolower() == "stop")
{
svr.stop();
console.writeline("server is stop.");
continue;
}
//运行服务器程序
if(cmd.tolower() == "start")
{
svr.start();
console.writeline("server is listen…{0}",
svr.serversocket.localendpoint.tostring());
continue;
}
//察看服务器在线客户端数目和容量
if(cmd.tolower() == "count")
{
console.writeline("current count of client is {0}/{1}",
svr.sessioncount,svr.capacity);
continue;
}
//发送数据到客户端格式:send [session] [stringdata]
if(cmd.tolower().indexof("send") !=-1)
{
cmd = cmd.tolower();
string[] para = cmd.split( );
if(para.length ==3)
{
session client = (session)svr.sessiontable[ new sessionid( int.parse
(para[1]))];
if(client !=null)
{
svr.send(client, para[2]);
}
else
{
console.writeline("the session is null");
}
}
else
{
console.writeline("error command");
}
continue;
}
//从服务器上踢掉一个客户端
if(cmd.tolower().indexof("kick") !=-1)
{
cmd = cmd.tolower();
string[] para = cmd.split( );
if(para.length ==2)
{
session client = (session)svr.sessiontable[ new sessionid( int.parse
(para[1]))];
if(client !=null)
{
svr.closesession(client);
}
else
{
console.writeline("the session is null");
}
}
else
{
console.writeline("error command");
}
continue;
}
//列出服务器上所有的客户端信息
if(cmd.tolower() == "list")
{
int i=0;
foreach( session client in svr.sessiontable.values)
{
if(client !=null)
{
i++;
string info = string.format("{0} client:{1} connected server session:{2}. socket handle:{3}",
i,
client.clientsocket.remoteendpoint.tostring(),
client.id,
client.clientsocket.handle);
console.writeline( info );
}
else
{
i++;
string info = string.format("{0} null client", i);
console.writeline(info);
}
}
continue;
}
console.writeline("unkown command");
}//end of while
console.writeline("end service");
}
catch(exception ex)
{
console.writeline(ex.tostring());
}
}
void clientconn(object sender, neteventargs e)
{
string info = string.format("a client:{0} connect server session:{1}. socket handle:{2}",
e.client.clientsocket.remoteendpoint.tostring(),
e.client.id,e.client.clientsocket.handle);
console.writeline( info );
console.write(">");
}
void serverfull(object sender, neteventargs e)
{
string info = string.format("server is full.the client:{0} is refused",
e.client.clientsocket.remoteendpoint.tostring());
//must do it
//服务器满了,必须关闭新来的客户端连接
e.client.close();
console.writeline(info);
console.write(">");
}
void clientclose(object sender, neteventargs e)
{
string info ;
if( e.client.typeofexit == session.exittype.exceptionexit)
{
info= string.format("a client session:{0} exception closed.",
e.client.id);
}
else
{
info= string.format("a client session:{0} normal closed.",
e.client.id);
}
console.writeline( info );
console.write(">");
}
void recvdata(object sender, neteventargs e)
{
string info = string.format("recv data:{0} from:{1}.",e.client.datagram, e.client);
console.writeline( info );
tcpsvr svr = (tcpsvr) sender;
//测试把收到的数据返回给客户端
svr.send(e.client, e.client.datagram);
console.write(">");
}
}
}
b.说明:
使用命令来操作服务器
exit 退出
start 开始服务
kick 关闭客户端
send 发送数据
list 列出所有客户端的状态
count 客户端计数
先启动服务运行start,等待客户端连接。
然后可以使用list,count察看当前的连接状况
每个事件都有相应函数,客户就在事件处理函数中处理自己的业务逻辑
可以通过继承特化自己的服务器应用,基本的框架不变
二.tcpcli的使用方法
a.测试程序:
using system;
using ibms.net.tcpcsframework;
namespace ibms.test
{
/// <summary>
/// testtcpclient 的摘要说明。
/// </summary>
public class testtcpclient
{
public testtcpclient()
{
//
// todo: 在此处添加构造函数逻辑
//
}
public static void test()
{
console.writeline("begin to test tcpcli class..");
testtcpclient test = new testtcpclient();
tcpcli cli = new tcpcli( new coder(coder.encodingmothord.utf8));
cli.resovlver = new datagramresolver("##");
cli.receiveddatagram += new netevent(test.recvdata);
cli.disconnectedserver += new netevent(test.clientclose);
cli.connectedserver += new netevent(test.clientconn);
try
{
//命令控制循环
while(true)
{
console.write(">");
string cmd=console.readline();
if(cmd.tolower() == "exit")
{
break;
}
if(cmd.tolower() == "close")
{
cli.close();
continue;
}
if(cmd.tolower().indexof("conn")!=-1)
{
cmd = cmd.tolower();
string[] para = cmd.split( );
if(para.length ==3)
{
cli.connect(para[1],int.parse(para[2]));
}
else
{
console.writeline("error command");
}
continue;
}
if(cmd.tolower().indexof("send") !=-1)
{
cmd = cmd.tolower();
string[] para = cmd.split( );
if(para.length ==2)
{
cli.send(para[1]);
}
else
{
console.writeline("error command");
}
continue;
}
console.writeline("unkown command");
}//end of while
console.writeline("end service");
}
catch(exception ex)
{
console.writeline(ex.tostring());
}
}
void clientconn(object sender, neteventargs e)
{
string info = string.format("a client:{0} connect server :{1}",e.client,
e.client.clientsocket.remoteendpoint.tostring());
console.writeline( info );
console.write(">");
}
void clientclose(object sender, neteventargs e)
{
string info ;
if( e.client.typeofexit == session.exittype.exceptionexit)
{
info= string.format("a client session:{0} exception closed.",
e.client.id);
}
else
{
info= string.format("a client session:{0} normal closed.",
e.client.id);
}
console.writeline( info );
console.write(">");
}
void recvdata(object sender, neteventargs e)
{
string info = string.format("recv data:{0} from:{1}.",e.client.datagram, e.client);
console.writeline( info );
console.write(">");
}
}
}
b.说明:
先建立连接,conn 192.9.207.214 9050
然后可以send 数据
最后关闭连接close
三.编码器
如果你要加密你的报文,需要一个你自己的coder
从coder类继承一个如mycoder类,然后重载编码和解码函数。
使用方法:
tcpcli cli = new tcpcli( new mycoder());
就可以在客户端使用该编码器了。
四.报文解析器
与编码器同样的实现方法。