国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

在C#中使用異步Socket編程實現(xiàn)TCP網(wǎng)絡(luò)服務(wù)的C/S的通訊構(gòu)架(二)----使用方法

2024-07-21 02:18:08
字體:
供稿:網(wǎng)友


收集最實用的網(wǎng)頁特效代碼!

一.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);//默認(rèn)使用encoding.default編碼方式
tcpsvr svr = new tcpsvr(9050,4,new coder(coder.encodingmothord.utf8));

svr.resovlver = new datagramresolver("##");

//定義服務(wù)器的4個事件

//服務(wù)器滿
svr.serverfull += new netevent(tts.serverfull);

//新客戶端連接
svr.clientconn += new netevent(tts.clientconn);

//客戶端關(guān)閉
svr.clientclose += new netevent(tts.clientclose);

//接收到數(shù)據(jù)
svr.recvdata += new netevent(tts.recvdata);

//命令控制循環(huán)
while(true)
{
console.write(">");

string cmd=console.readline();

//退出測試程序
if(cmd.tolower() == "exit")
{
break;
}

//停止服務(wù)器程序
if(cmd.tolower() == "stop")
{
svr.stop();

console.writeline("server is stop.");

continue;
}

//運行服務(wù)器程序
if(cmd.tolower() == "start")
{
svr.start();

console.writeline("server is listen...{0}",
svr.serversocket.localendpoint.tostring());

continue;
}

//察看服務(wù)器在線客戶端數(shù)目和容量
if(cmd.tolower() == "count")
{
console.writeline("current count of client is {0}/{1}",
svr.sessioncount,svr.capacity);
continue;
}

//發(fā)送數(shù)據(jù)到客戶端格式: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;
}

//從服務(wù)器上踢掉一個客戶端
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;

}

//列出服務(wù)器上所有的客戶端信息
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
//服務(wù)器滿了,必須關(guān)閉新來的客戶端連接
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;

//測試把收到的數(shù)據(jù)返回給客戶端
svr.send(e.client, e.client.datagram);

console.write(">");

}

}
}

b.說明:
使用命令來操作服務(wù)器
exit 退出
start 開始服務(wù)
kick 關(guān)閉客戶端
send 發(fā)送數(shù)據(jù)
list 列出所有客戶端的狀態(tài)
count 客戶端計數(shù)

先啟動服務(wù)運行start,等待客戶端連接。
然后可以使用list,count察看當(dāng)前的連接狀況


每個事件都有相應(yīng)函數(shù),客戶就在事件處理函數(shù)中處理自己的業(yè)務(wù)邏輯
可以通過繼承特化自己的服務(wù)器應(yīng)用,基本的框架不變

二.tcpcli的使用方法
a.測試程序:
using system;
using ibms.net.tcpcsframework;

namespace ibms.test
{
/// <summary>
/// testtcpclient 的摘要說明。
/// </summary>
public class testtcpclient
{
public testtcpclient()
{
//
// todo: 在此處添加構(gòu)造函數(shù)邏輯
//
}
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
{
//命令控制循環(huán)
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 數(shù)據(jù)
最后關(guān)閉連接close

三.編碼器
如果你要加密你的報文,需要一個你自己的coder
從coder類繼承一個如mycoder類,然后重載編碼和解碼函數(shù)。
使用方法:

tcpcli cli = new tcpcli( new mycoder());

就可以在客戶端使用該編碼器了。
四.報文解析器
與編碼器同樣的實現(xiàn)方法。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 察哈| 榆社县| 都安| 封丘县| 陆河县| 阿巴嘎旗| 射阳县| 富民县| 潜山县| 米易县| 黎平县| 聂荣县| 南京市| 德令哈市| 江孜县| 桂平市| 广宗县| 壶关县| 定襄县| 永德县| 古交市| 剑阁县| 凌海市| 巴林右旗| 嵊泗县| 嘉鱼县| 惠来县| 陆丰市| 夏邑县| 望城县| 郯城县| 新乡县| 承德市| 德保县| 鄂伦春自治旗| 汕尾市| 彭州市| 芮城县| 兖州市| 杭锦旗| 合肥市|