当前位置: 首页 > 工具软件 > Fastsocket > 使用案例 >

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

高慈
2023-12-01

示例代码参见  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

 类似资料: