Multithreaded Chat Server in C and Java!

Getting Started - Part 0

Author: Joshua Crotts

Date: 2021/05/16

Howdy! I decided to take on the difficult journey of writing a tutorial for of all things, a multi-threaded chat server. Not only will we build this from scratch, we’ll also use C as the back end with Java as the front end. Realistically, you could probably use anything for the front end, but just for experimentation, we’ll do it this way.


Before we get started, I’ll first explain a couple of thing that we’re not doing, either out of simplicity or just because it’s too complex for what I want to demonstrate in this tutorial:

  • Secure Authentication: HTTPS is a complex protocol that takes some time to not only learn but also implement. As such, we won’t be using it. To complement, while we will use a database, we will not be implementing the world’s best security measures.

So, what will we do?


  • Writing the front end in Java: Some people will critique the decision to write the front end of the application in Java, and that’s completely understandable — Java Swing is very outdated, but I believe it’s good for what I want to accomplish. Plus, as stated earlier, if you want to write a different front end, that’s totally up to you!
  • Back end development in C: Another interesting decision is to write our server in C. In my opinion, the C implementation of pthreads is really easy to understand and work with, so I think it’s a good choice. Plus, using sockets in C is also relatively straightforward. Perhaps not as much as, say, Python or another higher-level language, but hey, it works.
  • Conversations, friends, rooms, private messages: Much like every chatroom from 2004, we will have private message support, [private] rooms, a friends list, and broadcast messages (to everyone in a room).
  • Command support: Suppose the user wants to, say, clear their current screen or change their text color. It would be nice to add support for this and other commands as well.
  • API integration: There are some interesting APIs to experiment with, such as quotes of the day and things to that effect. Querying the server for things like this is an awesome and rather trivial inclusion once we get boilerplate code inserted, especially since we’re going to be using multiple threads to handle such requests!

Now that that’s out of the way, let’s discuss what I expect of you (i.e., what you should know coming into this):

  • You should be comfortable with Java. I’m not expecting you to be an expert, but methods, classes, objects, arrays, iteration, and sequential statements should be familiar terms.
  • You should also know some C. Typedefs, structs, unions, functions, C-strings, headers, preprocessor definitions, and pointers should be recognizable as well.
  • Preferably, familiarity with networking protocols such as TCP, Makefiles, and how threads work at a basic level would be helpful, but it’s not required.

So, before we start programming, we have to set up a few preliminary things, namely our development environment(s) for the respective languages.


First off, you need MacOS or a Linux environment. I’m going to use two: both a MacBook Pro 2017 and a copy of Xubuntu 20.04 for experimentation. As long as it’s not a Windows environment (since we’re working with pthreads), it should work fine. You can use a virtual machine with VirtualBox, VMWare, Hyper-V, or something else (possibly even WSL…) if you don’t want to install Linux on your own hardware.

Additionally, you need an environment and compiler for C. I’ll be using VSCode since it has become sort of the de-facto standard for programming on Linux. For a compiler, if you’re on Linux, then gcc is the way to go. On Mac, I recommend either that or clang.

Regarding Java, I will be using OpenJDK 15 and the IntelliJ IDE. Any IDE should work for this, though (Eclipse, NetBeans, BlueJ…).

Setting everything up is relatively straightforward, I’ll leave that to you since it’s irrelevant to actually designing the server. There are plenty of resources available to get you started with it. I’ll take care of everything else.


One other thing that I’ll emphasize with this project is that I’m going to actively encourage good programming practices and paradigms. While I’m obviously not a programming prodigy in C nor Java, there are certain things we can do to make our code easy to maintain and debug. So, we’ll be using GitHub for our version control software, Valgrindfor memory leaks/dynamic memory, and gdb for debugging in C.


With that out of the way, let's write us a chat server!