# antirez_smallcha **Repository Path**: ljc8/antirez_smallcha ## Basic Information - **Project Name**: antirez_smallcha - **Description**: 中文同步https://github.com/antirez/smallchat - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-04-13 - **Last Updated**: 2025-04-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README https://github.com/antirez/smallcha # Smallchat小聊天 TLDR: This is just a programming example for a few friends of mine. It somehow turned into a set of programming videos, continuing one project I started some time ago: Writing System Software videos series. > TLDR:这只是我的几个朋友的一个编程示例。它以某种方式变成了一组编程视频,继续我不久前开始的一个项目:编写系统软件视频系列。 1. [First episode](https://www.youtube.com/watch?v=eT02gzeLmF0), how the basic server works. > [第一集](https://www.youtube.com/watch?v=eT02gzeLmF0) ,基本服务器是如何工作的。 2. [Second episode](https://youtu.be/yogoUJ2zVYY), writing a simple client with raw terminal handling. > [第二集](https://youtu.be/yogoUJ2zVYY) ,写一个简单的客户端和原始的终端处理。 Likely more will follow, stay tuned. > 可能会有更多的后续,敬请期待。 **IMPORTANT: a warning about PRs**: please note that most pull requests adding features will be refused, because the point of this repository is to improve it step by step in the next videos. We will do refactoring during live coding sessions (or explaining how the refactoring was needed in the video), introducing more libraries to improve the program inner working (linenoise, rax, and so forth). So if you want to improve the program as an exercise, go ahead! It's a great idea. But I will not merge new features here since the point of the program is to evolve it step by step during the videos. > **重要:关于 PR 的警告** :请注意,大多数添加功能的 pull 请求将被拒绝,因为这个仓库的目的是在接下来的视频中逐步改进它。我们将在实时编码会话中进行重构(或在视频中解释如何需要重构),引入更多的库来改善程序内部工作(linenoise,rax 等)。所以,如果你想把这个程序作为一个练习来改进,那就继续吧!这是个好主意但我不会在这里合并新功能,因为该计划的重点是在视频中逐步发展它。 ## And now, the full story: 现在,完整的故事: Yesterday I was talking with a few friends of mine, front-end developers mostly, who are a bit far from system programming. We were remembering the old times of IRC. And inevitably I said: that writing a very simple IRC server is an experience everybody should do (I showed them my implementation written in TCL; I was quite shocked that I wrote it 18 years ago: time passes fast). There are very interesting parts in such a program. A single process doing multiplexing, taking the client state and trying to access such state fast once a client has new data, and so forth. > 昨天我和我的几个朋友聊天,他们大多是前端开发人员,他们离系统编程有点远。我们回忆起 IRC 的旧时光。我不可避免地说:写一个非常简单的 IRC 服务器是每个人都应该做的经历(我向他们展示了我用 TCL 写的实现;我很震惊,我在 18 年前写的:时间过得很快)。在这样一个节目中有非常有趣的部分。单个进程执行多路复用,获取客户端状态,并在客户端有新数据时尝试快速访问此类状态,等等。 But then the discussion evolved and I thought, I'll show you a very minimal example in C. What is the smallest chat server you can write? For starters to be truly minimal we should not require any proper client. Even if not very well, it should work with `telnet` or `nc` (netcat). The server's main operation is just to receive some chat line and send it to all the other clients, in what is sometimes called a fan-out operation. However, this would require a proper `readline()` function, then buffering, and so forth. We want it simpler: let's cheat using the kernel buffers, and pretending we every time receive a full-formed line from the client (an assumption that is in practice often true, so things kinda work). > 但后来讨论有所进展,我想,我会给你们看一个用 C 语言写的非常小的例子。你能写的最小的聊天服务器是什么?对于初学者来说,我们不应该要求任何适当的客户端。即使不是很好,它应该与 `telnet` 或 `nc`(netcat)一起工作。服务器的主要操作只是接收一些聊天行并将其发送给所有其他客户端,有时称为扇出操作。但是,这需要一个适当`的 readline()` 函数,然后是缓冲,等等。我们希望它更简单:让我们使用内核缓冲区来欺骗,并假设我们每次都从客户端接收到一个完整的行(这个假设在实践中通常是正确的,所以事情有点工作)。 Well, with these tricks we can implement a chat that even has the ability to let the user set their nick in just 200 lines of code (removing spaces and comments, of course). Since I wrote this little program as an example for my friends, I decided to also push it here on GitHub. > 好吧,有了这些技巧,我们可以实现一个聊天,甚至有能力让用户设置他们的尼克在短短 200 行代码(删除空格和注释,当然)。因为我写了这个小程序作为我的朋友的例子,我决定也把它推到 GitHub 上。 ## Future work 今后工作 In the next few days, I'll continue to modify this program in order to evolve it. Different evolution steps will be tagged according to the YouTube episode of my series on _Writing System Software_ covering such changes. This is my plan (may change, but more or less this is what I want to cover): > 在接下来的几天里,我将继续修改这个程序,以使它进化。不同的进化步骤将根据我的_写作系统软件_系列的 YouTube 片段进行标记。这是我的计划(可能会改变,但或多或少这是我想涵盖的): * Implementing buffering for reading and writing. > 实现阅读和写的缓冲。 * Avoiding the linear array, using a dictionary data structure to hold the client state. > 避免线性数组,使用字典数据结构来保存客户端状态。 * Writing a proper client: line editing able to handle asynchronous events. > 写一个合适的客户端:行编辑能够处理异步事件。 * Implementing channels. > 实施渠道。 * Switching from select(2) to more advanced APIs. > 从 select(2)切换到更高级的 API。 * Simple symmetric encryption for the chat. > 简单的对称加密聊天。 Different changes will be covered by one or more YouTube videos. The full commit history will be preserved in this repository. > 不同的更改将由一个或多个 YouTube 视频覆盖。完整的提交历史将保存在此存储库中。