Code Monkey home page Code Monkey logo

smallchat's Introduction

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.

  1. First episode, how the basic server works.
  2. Second episode, writing a simple client with raw terminal handling.

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.

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.

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).

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.

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):

  • 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.
  • 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.

smallchat's People

Contributors

antirez avatar ekkosun avatar eltociear avatar how2051 avatar juniorohanyere avatar pauloxnet avatar zhangkai803 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

smallchat's Issues

anyone can provide python implementations?

I watched the video mentioned in the repo. I want to know his specific principles and philosophy of the code. Can anyone provide a python implementation of the code?

Unsafe use of return value of snprintf

Unsafe use of return value of snprintf on line 346 can result in out of bounds read when length of nickname + length of name > 254.
https://github.com/antirez/smallchat/blob/main/smallchat.c#L346

from https://linux.die.net/man/3/snprintf
If the output was truncated due to this limit then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available

Could fix by bounding msglen variable to a max value of 256.

why set j to readfds not client[j].fd?

for (int j = 0; j <= Chat->maxclient; j++) {
    if (Chat->clients[j]) FD_SET(j, &readfds);
}

why set FD_SET(j, &readfds); not FD_SET(Chat->clients[j].fd, &readfds);?

There is a bug in finding the maximum slot

In line 105 of smallchat-server.c, it should be wrapped in a pair of curly braces. Because the break on line 106 will now exit the loop directly.

    if (Chat->maxclient == c->fd) {
        /* Ooops, this was the max client set. Let's find what is
         * the new highest slot used. */
        int j;
        for (j = Chat->maxclient-1; j >= 0; j--) {
            if (Chat->clients[j] != NULL) Chat->maxclient = j;
            break;
        }
        if (j == -1) Chat->maxclient = -1; // We no longer have clients.
    }

should be changed to

    if (Chat->maxclient == c->fd) {
        /* Ooops, this was the max client set. Let's find what is
         * the new highest slot used. */
        int j;
        for (j = Chat->maxclient - 1; j >= 0; j--) {
            if (Chat->clients[j] != NULL) {
                Chat->maxclient = j;
                break;
            }
        }
        if (j == -1) Chat->maxclient = -1; // We no longer have clients.
    }

What about add an open-source license

Hi, @antirez.

smallchat is an awesome project. I learned a lot from it.

What about add an exact open-source license to this repo?
I think this will allow people to better learn and participate in this interesting program.

Regards,
Xinfeng

About Performance and Testing

In addition to functional code, testing, especially performance testing is also the point I want to learn about. For example, how much impact does concurrency and delay have on the program?

Mastering performance testing methods helps me quantitatively understand why write this way.

documentation

Brother, is there any usage documentation for this or how to deploy it?

chatMalloc and chatRealloc will probably never fail on Linux, even if memory is full

void *chatMalloc(size_t size) {

Depending on the configuration of someone's system, Linux will probably over-commit and not make malloc/realloc return a NULL pointer even if memory is full.

The default behavior is to over-commit if the memory request is not "unreasonable". Which basically means any small memory allocation request will return a pointer as if it allocated successfully, but then the program will segfault when trying to write to it, or more likely be killed by the out of memory manager before a write to it is attempted.

There's not really a great platform-independent solution, but I usually read /proc/meminfo and check the CommitLimit to check if an allocation request is too large. Edit: For this particular case, probably best to use MemAvailable

How can it compile on windows?

Thanks for your project. It is very nice for junior developers to learn network and C programming.One more question I think it is better if the "smallchat" can run or compile on windows. Because most of our junior developers program on windows ^_^!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.