Introduction:
A chat application is a platform that allows users to send and receive messages in real-time. In this program, we will create a basic console-based chat application using the C programming language. The application will enable two users to communicate through a simple client-server model.
Objective:
The primary objective of this project is to understand socket programming in C. This involves using system calls to create communication channels between two devices (or processes) over a network. By implementing this chat application, users will learn how to handle user input and display received messages between two clients connected to a server.
Code: Chat Application in C
#include #include #include #include #include <arpa/inet.h> #define PORT 8080 #define MAX 100 // Function to handle communication between server and client void chat(int sock) { char buff[MAX]; int n; while(1) { bzero(buff, sizeof(buff)); printf("Enter message: "); n = 0; // Read message from user while((buff[n++] = getchar()) != '\n'); // Send message to server write(sock, buff, sizeof(buff)); bzero(buff, sizeof(buff)); // Receive message from server read(sock, buff, sizeof(buff)); // Print received message printf("Server: %s", buff); if(strncmp(buff, "exit", 4) == 0) { printf("Exiting chat...\n"); break; } } } int main() { int sock; struct sockaddr_in serverAddr; char buffer[MAX]; // Create socket sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("Socket creation failed"); exit(0); } printf("Client socket created.\n"); // Assign IP, PORT serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Localhost IP serverAddr.sin_port = htons(PORT); // Connect to server if (connect(sock, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) != 0) { perror("Connection with server failed"); exit(0); } printf("Connected to server.\n"); // Function for chat between client and server chat(sock); // Close the socket close(sock); }
Explanation of the Program
This simple chat application consists of two main parts: the client-side and the server-side. The client sends messages to the server, and the server responds. Both the client and server are implemented using socket programming, which provides a way for programs to communicate over a network.
The chat
function is used to handle the sending and receiving of messages. The client enters a message, which is sent to the server using the write
function. After sending the message, the client waits for a response from the server using the read
function, which it then displays. If the client types exit
, the program terminates.
The main function is responsible for setting up the socket, specifying the server address (localhost in this case), and initiating the connection. Once connected, the client can start chatting with the server.
How to Run the Program
- Open a terminal window and create two separate files:
server.c
andclient.c
. - Copy the server-side code into
server.c
and the client-side code intoclient.c
. - To compile both files, use the following commands in the terminal:
gcc server.c -o server gcc client.c -o client
- Start the server first by running the following command:
./server
- Then, in another terminal window, run the client program using:
./client
- Now you can start chatting. Type your messages, and they will be displayed by the other program.