- 热门文章:
- · C#代码
- · 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(一)----基础类库部分
- · c#.net常用的小函数和方法集
- · RC2加密算法在C#的应用----完善版
- · 简单的c#文本文件读写
- · c#.net常用的小函数和方法集
- · 用Visual C#打造个性化的IE浏览器
- · 如何用UltraEdit编译C#源程序
- · .Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类 (C# DIY HttpWebClient)
- · Visual C#网络编程之TCP
- · C# Programming Guidelines
- · C#设计模式之原型(ProtoType)
上一篇:c#中ref和out参数使用时需要注意的问题 >>
在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
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());
就可以在客户端使用该编码器了。
四.报文解析器
与编码器同样的实现方法。
下一篇:C#代码 >>
相关文章:
- · C#设计模式之抽象工厂(AbstractFactory)
- · C# Coding Standard
- · c#.net常用的小函数和方法集
- · c#.net函数列表.txt
- · c#.net常用的小函数参考
- · C#中使用XML——编写XML
- · C#版ftp方法实现类库代码
- · Visual C# .NET 入门
- · 在C#后代码里使用IE WEB Control TreeView
- · C#里操作时间的例子!
- · 在C#中利用SharpZipLib进行文件的压缩和解压缩
- · C#中使用XML——基于DOM的案例分析
- · 远程重启计算机(C#)
- · C#中使用XML——实现DOM
- · C#中使用XML——编写XML
- · 用C#实现生成PDF文档
- · 使用C#编写的一个定时关机程序
- · 三层架构之数据库访问层完全篇(C#)
- · c#中使用 Win32 和其他库
- · C#执行存储过程的简化
- · 用C#写一个Web自定义控件
- · 在C#中用最简洁有效的代码执行存储过程并返回数据
- · C#里的InputBox
- · 关于C#数组初始化的效率测试
- · C#第2个程序,读取偶网站的数据!
- · 深入理解C#编程中的组件-事件-委托
- · 通过Visual C#.NET建一个DTS任务
- · 用C#的Raw Socket实现网络封包监视
- · 利用C#实现标准的 Dispose模式
- · 从 C++ 向 C# 迁移
- · 使用 C# 2.0 命令行编译器
- · 如何用C#语言构造蜘蛛程序
- · 在C#中使用SendMessage
- · C#封装的一个串口操作类(可用于Win CE)
- · 用C#制作PDF文件全攻略
- · C#.NET数据库操作记要
- · 使用API函数播放MP3等音频文件的类设计(C#)
- · 用Visual C#调用Windows API函数
