客户端被动接收服务器端信息,FastSocket客户端/服务端通讯示例 客户端被动接收...

示例代码参见  http://www.cnblogs.com/T-MAC/p/fastsocket-asyncbinary-usage.html

我这里只写一份客户端如何被动接收的代码。   先从AsyncBinarySocketClient继承定义一个客户端类,重载OnConnected,OnDisconnected,OnMessageReceived等方法。

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSodao.FastSocket.SocketBase;usingSodao.FastSocket.Client;namespaceMaxCode

{public classMyClient : AsyncBinarySocketClient

{public MyClient(intsocketBufferSize,intmessageBufferSize,intmillisecondsSendTimeout,intmillisecondsReceiveTimeout)

:base(socketBufferSize, messageBufferSize, millisecondsSendTimeout, millisecondsReceiveTimeout)

{

}protected Encoding encode = Encoding.GetEncoding("utf-8");protected override voidOnMessageReceived(IConnection connection, MessageReceivedEventArgs e)

{if(e.Buffer!=null && e.Buffer.Count>0)

MessageBox.Show(encode.GetString(e.Buffer.Array));base.OnMessageReceived(connection, e);

}protected override voidOnConnected(IConnection connection)

{

bConnected= true;base.OnConnected(connection);

}protected override voidOnDisconnected(IConnection connection, Exception ex)

{

bConnected= false;base.OnDisconnected(connection, ex);

}private bool bConnected = false;public boolConnected

{get { returnbConnected; }

}

}

}

然后在主程序中实例化本类

var client = new MaxCode.MyClient(8192, 8192, 3000, 3000);

//注册服务器节点,这里可注册多个(name不能重复)

client.RegisterServerNode("127.0.0.1:8401", new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 8401));

然后我们就可以通过重载的OnMessageReceived方法接收来自服务端的消息了。

希望对大家有所帮助。

原文:http://www.cnblogs.com/rufus/p/6518655.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值