一.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());
就可以在客户端使用该编码器了。
四.报文解析器
与编码器同样的实现方法。
相关推荐
在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架
在C#中使用异步Socket编程实现TCP网络服务的CS的通讯构架.
C#中使用异步Socket编程实现TCP网络服务的CS的通讯构架.cs
异步Socket实现TCP网络服务的CS的通讯构架,各种的C#的异步Socket的常用实例,给初学者和进阶者都能带来不小的好处。
1.在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架 2. 特点:(1).使用hash表保存所有已连接客户端的状态,收到数据时能实现快速查找.每当 有一个新的客户端连接就会产生一个新的会话(Session).该Session代表...
C#的TCP异步SOCKET通讯类 C# TCP 异步 SOCKET 通讯 类,非常实用的一个类
该压缩包中包含 (1).Net Socket编程基础;(2).NET 中 System.Diagnostics....(8)在C#中使用异步Socket编程实现TCP网络服务的CS的通讯构架;(9)重新研究socket传输自定义对象 9个doc帮助文档,已经两个Demo程序.请下载学习
C#实现Socket编程 (异步通讯,解决Tcp粘包)
C# Socket通讯/TCP通讯,完整代码demo
tcp/ip通讯,使用c#进行socket编程
C#中使用异步Socket编程实现TCP网络服务的C/S的通讯架构
基于TCP/IP协议的C/S异步通讯传输工具,开发环境采用的是C#平台
本人通过网络搜索,搜索到的一个感觉蛮不错的socket通讯demo示例。希望对想学习C#网络编程的朋友有所帮助! 对于想开发网络通讯程序的朋友具有不错的参考价值!
zip包里有两个程序,一个server端一个client端,采用了微软建议的socket异步模式,先启动server程序,client端修改app.config serverhost value值,运行即可。
C#.net同步异步SOCKET通讯和多线程总结~
这是一个使用c#语言C/S构架的TCP文件传输测试程序。解决方案下包含了Client和Server两个工程,需要同时运行才能传输文件。这个可以提供给C#语言的初学者作为TCP网络文件传输的一个参考。
C#基于Socket的TcpClient异步实现和基于Socket的TcpServer异步实现
Socket异步编程源代码与资料 Socket C#通讯
C#上位机基础学习_基于SOCKET实现与PLC服务器的TCP通信(一)
基于C#的TCP/IP同步以及异步通信实现方法,我已经把两种方法封装好了,直接调用就可以了,不仅可以实时获取连接的客户端,还可以实时刷新客户端连接,很好用的,如果有什么问题,欢迎联系我!