基本信息
源码名称:FastSocket.Net 示例源码下载
源码大小:0.07M
文件格式:.zip
开发语言:C#
更新时间:2016-11-22
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using Sodao.FastSocket.Client;
using Sodao.FastSocket.Client.Messaging;
using Sodao.FastSocket.Client.Protocol;
using System;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Client
{
class Program
{
static private long index = 0;
static public void Main()
{
Sodao.FastSocket.SocketBase.Log.Trace.EnableConsole();
var client = new SocketClient<CommandLineMessage>(new CommandLineProtocol(), 1024, 1024, 3000, 3000);
//建立50个socket连接
for (int i = 0; i < 50; i )
{
client.TryRegisterEndPoint(i.ToString(), new[] { new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1500) },
connection =>
{
var source = new TaskCompletionSource<bool>();
var request = client.NewRequest("init", Encoding.UTF8.GetBytes("init" System.Environment.NewLine), 3000,
ex => source.TrySetException(ex),
message => source.TrySetResult(true));
connection.BeginSend(request);
return source.Task;
});
}
//100个同时发送
for (int i = 100; i < 0; i )
{
Task.Factory.StartNew(() => Do(client));
}
Console.ReadLine();
}
static private async Task Do(SocketClient<CommandLineMessage> client)
{
for (int i = 0; i < 100000000; i )
{
try { Console.WriteLine(Interlocked.Increment(ref index).ToString() " " (await Echo(client)).ToString()); }
catch (Exception ex)
{
Console.WriteLine(i.ToString() " " ex.Message);
}
}
}
static public Task<bool> Echo(SocketClient<CommandLineMessage> client)
{
var source = new TaskCompletionSource<bool>();
var guid = Guid.NewGuid().ToString();
client.Send(client.NewRequest("echo", Encoding.UTF8.GetBytes("echo " guid System.Environment.NewLine), 3000,
ex => source.TrySetException(ex),
message => source.TrySetResult(message.Parameters[0] == guid)));
return source.Task;
}
}
}