A simple console-based peer-to-peer chat program written in C using UDP sockets. It allows two users on different machines to exchange text messages over the network. The program reads user input from the console and sends it via UDP to the specified IPv4 address; incoming UDP datagrams from the peer are printed on the screen. Typing a message beginning with QUIT will terminate the chat loop and exit the application. Because UDP is connectionless and provides no built-in reliability or ordering, the application is intentionally lightweight – making it suitable for learning the basics of network programming and non-blocking I/O.
- Peer-to-peer messaging over UDP: Sends and receives messages as independent UDP datagrams directly between two hosts. (No central server is required.) UDP is connectionless (no handshake) and does not handle lost packets or ordering.
- Non-blocking keyboard input: Uses a
kbhit()function to check for user input without blocking. This allows the program to continue listening for incoming messages while the user is typing. Non-blocking I/O lets the program continue execution during I/O operations, improving responsiveness. - Clean exit on
QUITcommand: TypingQUITas a message will immediately exit the chat loop and close the socket. Receiving aQUITmessage from the peer likewise causes the program to terminate. - Console formatting: Sent and received messages are displayed in separate, aligned columns for readability.
- C compiler: A standard C compiler such as gcc or clang (both are typically available on Linux and macOS).
- POSIX-compatible OS: A Unix-like system (Linux, macOS) is required to use standard BSD sockets and termios. POSIX (Portable Operating System Interface) is a standard that ensures compatibility across Unix-like systems.
- Windows (optional): On Windows, install MinGW (a native port of GCC) to provide a Linux-like development environment. MinGW includes the GNU Compiler Collection (GCC) and libraries for building native Windows applications.
Compile the program by providing all source files to the compiler. For example, on Linux or macOS in a terminal:
gcc chat.c udp3.c kbhit.c -o chatIf you prefer clang, you can substitute clang for gcc.
On Windows with MinGW, link against the Winsock library. For example:
gcc chat.c udp3.c kbhit.c -o chat.exe -lws2_32For Microsoft Visual Studio (using the Developer Command Prompt), you could use:
cl chat.c udp3.c kbhit.c ws2_32.libThis will produce an executable (chat or chat.exe) that you can run as described below.
-
Start the program on both machines. In each machine’s terminal (or command prompt), run the executable with the other user’s IP address as an argument. For example, if the peer’s IP is
192.168.1.5, start:./chat 192.168.1.5
(Replace
chat.exeon Windows.) Each user must provide the other’s correct IPv4 address so that messages are directed properly. -
Enter messages. Type a message and press Enter to send it. Your sent messages will appear on one side of the console and incoming messages on the other side (different columns).
-
Exit the chat. To end the session, type
QUITand press Enter. The program will sendQUITto the peer and then cleanly exit. Likewise, if aQUITmessage is received from the other user, the program will close the chat loop and terminate.
Example terminal session:
$ ./chat 192.168.1.5
Me: Hello!
Peer: Hi there!
Me: How are you?
Peer: Good, thanks. QUIT
Me: QUIT
$
chat.c– The main program. Implements the chat loop: it repeatedly checks for keyboard input viakbhit(), reads strings (e.g. withfgets()), and sends them over UDP. It also listens for incoming UDP datagrams. When a message is entered, it uses functions fromudp3.cto send it; when data arrives, it displays it. Thekbhit()function (fromkbhit.c) is used to poll the keyboard without blocking.udp3.c– Contains UDP socket utilities. This file sets up the UDP socket (binding to a local port) and provides functions likesendUdpData()andreceiveUdpData()that wrapsendto()/recvfrom(). These functions handle creating sockets, addressing (using the IPv4 address), and sending/receiving message buffers.kbhit.c– Provides akbhit()implementation for non-blocking keyboard input. This allowschat.cto check if a key has been pressed without pausing program execution.
- No encryption: All messages are sent in plaintext over the network. (UDP by itself provides no encryption or security.)
- No reliability or reconnection: UDP is “fire-and-forget” – lost packets are never retransmitted. If a packet is dropped on the network, the chat application does not recover it. Also, if one user quits or a message is lost, the program does not attempt to reconnect or confirm delivery.
- Manual startup required: Both parties must manually start the program and enter IP addresses. There is no service discovery or automatic peer connection.
- GUI interface: Add a graphical user interface (e.g., using GTK or Qt) to replace the console for a more user-friendly chat window.
- Message logging: Implement a feature to save chat history to a log file.
- Multi-client support: Extend the design to allow more than two users (e.g. via multicasting or a simple server) for group chat.