# smallchat **Repository Path**: dowhere/smallchat ## Basic Information - **Project Name**: smallchat - **Description**: 作为学习 socket 通信编程的示例 https://github.com/antirez/smallchat - **Primary Language**: C - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 13 - **Created**: 2023-11-03 - **Last Updated**: 2023-11-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 简单说明 Redis 创始人用 C 语言编写最小聊天服务器:Smallchat 这是一个简单的聊天服务器(chat server)的编程示例,使用C语言编写。这个聊天服务器的主要功能是接收一些聊天信息,并将其发送给所有其他客户端,这是一种被称为"fan-out"(扇出)的操作。这个聊天服务器的设计非常简单,甚至使用了内核缓冲区来欺骗系统,假设每次都能从客户端接收完整的行。 以下是这个故事的一些关键点: * 作者和他的朋友们讨论起IRC(Internet Relay Chat,互联网中继聊天)服务器,这让作者想起他曾经写过一个简单的TCL版本的IRC服务器。 * 讨论中,作者提到可以为朋友们展示一个非常简单的C语言版本的聊天服务器,并询问他们是否知道最简单的聊天服务器应该如何编写。 * 作者解释说,一个真正的最小化的聊天服务器应该能够使用telnet或nc(netcat)等工具进行测试,主要功能是接收聊天信息并将其发送给所有其他客户端。 * 由于这个任务需要处理客户端的状态并快速地访问这些状态,因此作者决定使用C语言来实现这个功能。 * 作者解释了他们如何利用内核缓冲区来欺骗系统,使得他们可以假设每次都能从客户端接收完整的行。 * 最后,作者决定将这个示例程序发布到GitHub上,让更多的人能够看到并学习这个简单的聊天服务器。 # 中文翻译 TLDR:这只是我的几个朋友的编程示例。我在YouTube频道上传了一段视频[放大代码](https://www.youtube.com/watch?v=eT02gzeLmF0),看看可以从一个如此简单而破碎的(故意的)例子中学到什么。 现在,完整的故事: 昨天,我和我的几个朋友聊了聊,他们大多是前端开发人员,他们离系统编程有点远。我们怀念IRC的旧时光。不可避免地,我说:编写一个非常简单的IRC服务器是每个人都应该做的一种体验(我向他们展示了我用TCL编写的实现;我在18年前写下它时感到非常震惊:时间过得很快)。在这样的节目中有一些非常有趣的部分。执行多路复用、获取客户端状态并在客户端有新数据后尝试快速访问该状态的单个进程,等等。 但后来讨论发生了变化,我想,我会在C中给你看一个非常简单的例子。你能写的最小的聊天服务器是什么?对于真正最小的初学者,我们不应该要求任何合适的客户。即使不是很好,它也应该与“telnet”或“nc”(netcat)配合使用。服务器的主要操作只是接收一些聊天线路并将其发送给所有其他客户端,这有时被称为扇出操作。但是,这需要一个适当的readline()函数,然后是缓冲,等等。我们希望它更简单:让我们使用内核缓冲区作弊,并假装每次都收到来自客户端的完整行(这是一种在实践中经常成立的假设,所以事情有点奏效)。 好吧,有了这些技巧,我们可以实现聊天,甚至可以 让用户在200行代码中设置自己的缺口(去掉空格 当然还有评论)。因为我写了这个小程序作为一个例子 我的朋友们,我决定也在Github上推送它。 # Smallchat原文 TLDR: This is just a programming example for a few friends of mine. I upload a video on my YouTube channel [zooming into the code](https://www.youtube.com/watch?v=eT02gzeLmF0), to see what can be learned from a so simple and broken (on purpose) example. And now, the full story: Yesterday I was talking with a few friends of mine, front-end developers mostly, that are a bit far from system programming. We were remembering the old times of IRC. And inevitably I said: to write a very simple IRC server is an experience everybody should do (I showed them my implementation written in TCL; I was a quite shocked that I wrote it 18 years ago: time passes fast). There are very interesting parts in a program like that. 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. 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 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. But yet, 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 the practice often true, so things kinda work). Well, with this 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. GitHub源码:https://github.com/antirez/smallchat 可以看到,Smallchat 的核心代码仅 300 多行。antirez 称删除空行和注释后其实只有 200 多行。