异步socket通信问题

rocker1218 2006-01-11 11:03:19
我的代码来自:
C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)


namespace webgis00
{ #region BzAgent define
/// <summary>
/// 服务器 的摘要说明。
/// 服务器代理类:负责与保持与服务器的长连接,负责从服务器接收数据,负责向服务器发送控制命令;
/// </summary>
public class BzAgent
{
public string bzServerIP;
public int bzPort;
public string bzUserName;
public string bzUserPwd;
public TcpCli _bzClient;
private bool _isLoginBz=false;

//构造函数
public BzAgent(string bzServerIP,int bzPort,string bzUserName,string bzUserPwd)
{

this.bzServerIP = bzServerIP;
this.bzPort = bzPort;
this.bzUserName = bzUserName;
this.bzUserPwd = bzUserPwd;

//创建与BZ的连接客户端对象
_bzClient = new TcpCli();
//订阅事件
_bzClient.ConnectedServer += new NetEvent(_bzClient_ConnectedServer);
_bzClient.DisConnectedServer +=new NetEvent(_bzClient_DisConnectedServer);
_bzClient.ReceivedDatagram +=new NetEvent(_bzClient_ReceivedDatagram);
//启动连接
_bzClient.Connect(this.bzServerIP,this.bzPort);
}
public BzAgent()
{
//1:先判断是否socket正常连接。
//2:处理订阅事件,引发发送第一条指令。
//请问:这个函数该怎么来写?
}
public void _BzLocationSendAgent(object sender, NetEventArgs e)
{
//发第一条指令
BzLocationMsg msg = new BzLocationMsg();
_bzClient.Send(msg.toXml());
}

private void _bzClient_ConnectedServer(object sender, NetEventArgs e)
{
//发送登录请求
BzLoginMsg msg = new BzLoginMsg(this.bzUserName,this.bzUserPwd);
this._bzClient.Send(msg.toXml());
}

private void _bzClient_DisConnectedServer(object sender, NetEventArgs e)
{
this._isLoginBz = false;
}

private void _bzClient_ReceivedDatagram(object sender, NetEventArgs e)
{
//从事件参数获取接收到的数据包
Session clientSession = e.Client;
string strMsg = clientSession.Datagram;
}
public bool isLoginBz()
{
return this._isLoginBz;
}
}

/// <summary>
/// 网络通讯事件模型委托
/// </summary>
public delegate void NetEvent(object sender, NetEventArgs e);

/// <summary>
/// 服务器程序的事件参数,包含了激发该事件的会话对象
/// </summary>
public class NetEventArgs:EventArgs
{
#region 字段
/// <summary>
/// 客户端与服务器之间的会话
/// </summary>
private Session _client;
#endregion

#region 构造函数
/// <summary>
/// 构造函数
/// </summary>
/// <param name="client">客户端会话</param>
public NetEventArgs(Session client)
{
if( null == client)
{
throw(new ArgumentNullException());
}

_client = client;
}
#endregion

#region 属性

/// <summary>
/// 获得激发该事件的会话对象
/// </summary>
public Session Client
{
get
{
return _client;
}
}
#endregion
}

/// <summary>
/// 提供Tcp网络连接服务的客户端类
///
/// 版本: 1.0
///
/// 特征:
/// 原理:
/// 1.使用异步Socket通讯与服务器按照一定的通讯格式通讯,请注意与服务器的通
/// 讯格式一定要一致,否则可能造成服务器程序崩溃,整个问题没有克服,怎么从byte[]
/// 判断它的编码格式
/// 2.支持带标记的数据报文格式的识别,以完成大数据报文的传输和适应恶劣的网
/// 络环境.
/// 用法:
/// 注意:
/// </summary>
public class TcpCli
{
/// <summary>
/// 客户端与服务器之间的会话类
/// </summary>
private Session _session;

/// <summary>
/// 客户端是否已经连接服务器
/// </summary>
public bool _isConnected = false;

/// <summary>
/// 接收数据缓冲区大小64K
/// </summary>
public const int DefaultBufferSize = 64*1024;

private byte[] _recvDataBuffer = new byte[DefaultBufferSize];

/// <summary>
/// 报文解析器
/// </summary>
private DatagramResolver _resolver;
...全文
176 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiezhi 2006-01-11
  • 打赏
  • 举报
回复
参考:
http://jiezhi.cnblogs.com/archive/2005/08/15/215419.html
lidong6 2006-01-11
  • 打赏
  • 举报
回复
什么问题?

这么多代码一看就晕了, 能不能挑重点说.
lostowner 2006-01-11
  • 打赏
  • 举报
回复
参考:
http://ltp.cnblogs.com/archive/2005/12/03/289680.html

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧