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(); } }
标签:Console,string,void,client,WriteLine,new,客户端,服务端,SuperSocket From: https://www.cnblogs.com/mojiejushi/p/17288228.html