SuperSocket 服务端 和 SuperSocket.ClientEngine 客户端及普通客户端
生活随笔
收集整理的這篇文章主要介紹了
SuperSocket 服务端 和 SuperSocket.ClientEngine 客户端及普通客户端
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
internal class Program
{
//static void Main(string[] args)
//{ // byte[] arr = new byte[1024]; // 1.創建socket對象
// Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // IPAddress address = IPAddress.Parse("127.0.0.1");
// int port = 8000;
// int port = 40410;
// socket.Connect(address, port); // while (true)
// {
// string v = Guid.NewGuid().ToString() + "\r\n"; // byte[] bytes = Encoding.UTF8.GetBytes(v); // socket.Send(bytes); // Console.WriteLine($"svr2 輸出:{v}"); // Thread.Sleep(3000); // 接收
// socket.Receive(arr);
// string v2 = Encoding.UTF8.GetString(arr);
// Console.WriteLine($"svr2 收到:{v2}"); // } //} private static AsyncTcpSession client;
static System.Timers.Timer timer = null;
private int port = 40410; static void Main(string[] args)
{
new Program().Link();
string input = Console.ReadLine();
while (input != "exit" && !string.IsNullOrEmpty(input))
{
if (client != null && client.IsConnected)
{
var msg = System.Text.Encoding.Default.GetBytes("echo hello " + input + "\r\n");
client.Send(msg, 0, msg.Length);
input = Console.ReadLine();
} }
}
private void Link()
{
client = new AsyncTcpSession();
client.Connected += OnClientConnected;
client.Error += OnClientError;
client.Closed += OnClientClosed;
// 收到服務器數據事件
client.DataReceived += client_DataReceived; //每10s發送一次心跳或嘗試一次重連
timer = new System.Timers.Timer(10000);
timer.Elapsed += new System.Timers.ElapsedEventHandler((s, x) =>
{
//心跳包
if (client.IsConnected)
{
var heartMsg = System.Text.Encoding.Default.GetBytes("echo heart 2\r\n");
client.Send(heartMsg, 0, heartMsg.Length);
}
//斷線重連
else if (!client.IsConnected)
{
client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port));
}
});
timer.Enabled = true;
timer.Start(); }
private void OnClientConnected(object sender, EventArgs e)
{
Console.WriteLine("已連接" + '\n');
}
private void OnClientClosed(object sender, EventArgs e)
{
Console.WriteLine("已斷開" + '\n');
}
private void OnClientError(object sender, ErrorEventArgs e)
{
Console.WriteLine($"錯誤:{e.Exception.Message}" + '\n');
}
void client_DataReceived(object sender, DataEventArgs e)
{
string msg = Encoding.UTF8.GetString(e.Data);
Console.WriteLine("客戶端接收:"+msg.Trim('\0'));
} }
}
internal class Program
{
private static async Task Main(string[] args)
{
var host = SuperSocketHostBuilder
.Create<StringPackageInfo, CommandLinePipelineFilter>()
.UsePackageHandler(async (session, package) =>
{
var result = 0; string? v = package.Key.ToString();
string? body = package.Body.ToString();
Console.WriteLine($"服務端 接收 key: {v}");
Console.WriteLine($"服務端 接收 value: {body}"); string v1 = $"hello 我收到: {v} {body}";
Console.WriteLine(v1);
await session.SendAsync(Encoding.UTF8.GetBytes(v1));
}) //.ConfigureLogging((hostCtx, loggingBuilder) =>
// {
// loggingBuilder.AddConsole();
// })
.ConfigureSuperSocket(options =>
{
options.Name = "Echo Server";
options.Listeners = new List<ListenOptions>() {
new ListenOptions
{
Ip = "Any",
Port = 40410
}
};
})
.Build(); await host.RunAsync();
}
}
總結
以上是生活随笔為你收集整理的SuperSocket 服务端 和 SuperSocket.ClientEngine 客户端及普通客户端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RedHat 6.5中建立Raid5卷(
- 下一篇: Tomcat发布网页的方法记录