BytesIO | C# 超简洁的TCP服务端开发(完整源码+视频教程)

8 篇文章 63 订阅

在这里插入图片描述

C# 超简洁的TCP服务端开发


前言

在前一篇文章《BytesIO系列(一) 轻松实现TCP客户端》,我们实现了从零开始开发一个TCP通信客户端程序,利用BytesIO的便捷手写代码不超过三十行。
本章将继续利用BytesIO开发TCP的服务端,简洁明了依然是主旋律,我们要在三十行代码内除了实现一个TCP服务端以外,使其支持聊天室(消息转发)、连接数限制、心跳超时检测等功能。
现在,一起跟着视频敲一敲吧!

视频教程

【女朋友都能学会】C# TCP服务端


核心代码

服务端代码

Visual Studio已经帮我们完成了大部分工作,真正需要我们手敲的代码不超过30行:

using STTech.BytesIO.Core;
using STTech.BytesIO.Tcp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Demo.BytesIO.Server
{
    public partial class ServerForm : Form
    {
        private TcpServer server;
        public ServerForm()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;

            server = new TcpServer();
            server.Port =int.Parse(tbPort.Text);

            server.Started += Server_Started;
            server.Closed += Server_Closed;
            server.ClientConnected += Server_ClientConnected;
            server.ClientDisconnected += Server_ClientDisconnected;

            server.ClientConnectionAcceptedHandle = (s, e) => {
                if (server.Clients.Count() < 3)
                {
                    return true;
                }
                else
                {
                    Print($"服务器已满,关闭客户端[{e.ClientSocket.RemoteEndPoint}]的连接.");
                    return false;
                }
            };
        }

        private void Server_ClientDisconnected(object sender, STTech.BytesIO.Tcp.Entity.ClientDisconnectedEventArgs e)
        {
            Print($"客户端[{e.Client.Host}:{e.Client.Port}]断开连接");
        }

        private void Server_ClientConnected(object sender, STTech.BytesIO.Tcp.Entity.ClientConnectedEventArgs e)
        {
            Print($"客户端[{e.Client.Host}:{e.Client.Port}]连接成功");
            e.Client.OnDataReceived += Client_OnDataReceived;
            e.Client.UseHeartbeatTimeout(3000);
        }

        private void Client_OnDataReceived(object sender, STTech.BytesIO.Core.Entity.DataReceivedEventArgs e)
        {
            TcpClient tcpClient = (TcpClient)sender;
            Print($"来自客户端[{tcpClient.RemoteEndPoint}]的消息: {e.Data.EncodeToString("GBK")}");

            foreach(TcpClient client in server.Clients)
            {
                if(client != tcpClient)
                {
                    client.SendAsync(e.Data);
                }
            }
        }

        private void Server_Closed(object sender, EventArgs e)
        {
            Print("停止监听");
        }

        private void Server_Started(object sender, EventArgs e)
        {
            Print("开始监听");
        }

        private void tsmiStart_Click(object sender, EventArgs e)
        {
            server.StartAsync();
        }

        private void tsmiClose_Click(object sender, EventArgs e)
        {
            server.CloseAsync();
        }

        private void Print(string msg)
        {
            tbLog.AppendText($"[{DateTime.Now}] {msg}\r\n");
        }
    }
}

消息转发

可以看到源码中,实现聊天室功能的核心代码:

            foreach(TcpClient client in server.Clients)
            {
                if(client != tcpClient)
                {
                    client.SendAsync(e.Data);
                }
            }

心跳超时检测

在BytesIO中,进行心跳超时检测只需要一行代码:

<客户端对象>.UseHeartbeatTimeout(<超时时间/毫秒>);

源码下载

下载地址:https://download.csdn.net/download/lgj123xj/85513898

结束语

如果非IT行业的女朋友都能学会的话,应该就算0基础入门的教学视频了吧!
超简单的C#TCP服务端开发入门,短短的代码,完整的功能,掏出你的VS码一个试试手吧!

关于BytesIO还有很多的开发技巧可以分享 ,在其余教程未完成之前,如果有使用上的疑问可以加群讨论。
QQ群:738018341

  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猿长大人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值