Skip to content

SohamxP/Chatbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chatbot Interface

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.

Features

  • 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 QUIT command: Typing QUIT as a message will immediately exit the chat loop and close the socket. Receiving a QUIT message from the peer likewise causes the program to terminate.
  • Console formatting: Sent and received messages are displayed in separate, aligned columns for readability.

Prerequisites

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

Build Instructions

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 chat

If 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_32

For Microsoft Visual Studio (using the Developer Command Prompt), you could use:

cl chat.c udp3.c kbhit.c ws2_32.lib

This will produce an executable (chat or chat.exe) that you can run as described below.

Usage

  1. 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.exe on Windows.) Each user must provide the other’s correct IPv4 address so that messages are directed properly.

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

  3. Exit the chat. To end the session, type QUIT and press Enter. The program will send QUIT to the peer and then cleanly exit. Likewise, if a QUIT message 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
$

File Structure

  • chat.c – The main program. Implements the chat loop: it repeatedly checks for keyboard input via kbhit(), reads strings (e.g. with fgets()), and sends them over UDP. It also listens for incoming UDP datagrams. When a message is entered, it uses functions from udp3.c to send it; when data arrives, it displays it. The kbhit() function (from kbhit.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 like sendUdpData() and receiveUdpData() that wrap sendto()/recvfrom(). These functions handle creating sockets, addressing (using the IPv4 address), and sending/receiving message buffers.
  • kbhit.c – Provides a kbhit() implementation for non-blocking keyboard input. This allows chat.c to check if a key has been pressed without pausing program execution.

Limitations

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

Future Improvements

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages