TCP Chat Client/Server Programming in Unix Using C Programming

🎯 Introduction

TCP (Transmission Control Protocol) chat client/server programming allows communication between multiple clients and a central server over a network using sockets. In this blog post, we will explore a simple implementation of a TCP chat client and server using C programming on Unix-like systems. The code examples provided here will be updated and explained in detail to ensure a better understanding of the concepts involved.

🎯 Concept

🎯 Chat Server Detailed Explanation

👉 Chat Server Code

Let's start by understanding the TCP chat server implementation in C:

#include <sys/types.h>

#include <sys/socket.h>

#include <fcntl.h>

#include <netinet/in.h>

#include <stdio.h>

#include <arpa/inet.h>

#include <string.h>


int main() {

    int sfd, cfd;

    fd_set rset;

    char buff[1024] = " ";

    struct sockaddr_in server;


    sfd = socket(AF_INET, SOCK_STREAM, 0);


    if (sfd < 0) {

        printf("Socket creation failed.\n");

        return -1;

    }


    bzero(&server, sizeof(struct sockaddr_in));

    server.sin_family = AF_INET;

    server.sin_port = htons(1005);

    inet_aton("172.16.29.110", &server.sin_addr);


    if (bind(sfd, (struct sockaddr*)&server, sizeof(server)) < 0) {

        printf("Binding failed.\n");

        return -1;

    }


    listen(sfd, 7);


    cfd = accept(sfd, NULL, NULL);


    for (;;) {

        FD_ZERO(&rset);

        FD_SET(0, &rset); // 0 represents the standard input (stdin)

        FD_SET(cfd, &rset);


        select(cfd + 1, &rset, NULL, NULL, NULL);


        if (FD_ISSET(0, &rset)) {

            printf("Enter the message:\n");

            scanf("%s", buff);

            write(cfd, buff, strlen(buff));

        }


        if (FD_ISSET(cfd, &rset)) {

            read(cfd, buff, 1024);

            printf("Message received: %s\n", buff);

        }

    }


    close(cfd);

    close(sfd);


    return 0;

}

👉 Explanation:

🎯 Chat Client Detailed Explanation

👉 Chat Client Code

Now, let's understand the TCP chat client implementation in C:

#include <sys/types.h>

#include <sys/socket.h>

#include <fcntl.h>

#include <netinet/in.h>

#include <stdio.h>

#include <arpa/inet.h>

#include <string.h>


int main() {

    int sfd;

    fd_set rset;

    char buff[1024] = " ";

    struct sockaddr_in server;


    sfd = socket(AF_INET, SOCK_STREAM, 0);


    if (sfd < 0) {

        printf("Socket creation failed.\n");

        return -1;

    }


    bzero(&server, sizeof(struct sockaddr_in));

    server.sin_family = AF_INET;

    server.sin_port = htons(1005);

    inet_aton("172.16.29.110", &server.sin_addr);


    printf("Connect: %d\n", connect(sfd, (struct sockaddr*)&server, sizeof(server)));


    for (;;) {

        FD_ZERO(&rset);

        FD_SET(0, &rset); // 0 represents the standard input (stdin)

        FD_SET(sfd, &rset);


        select(sfd + 1, &rset, NULL, NULL, NULL);


        if (FD_ISSET(0, &rset)) {

            printf("Enter the message:\n");

            scanf("%s", buff);

            write(sfd, buff, strlen(buff));

        }


        if (FD_ISSET(sfd, &rset)) {

            read(sfd, buff, 1024);

            printf("Message received: %s\n", buff);

        }

    }


    close(sfd);


    return 0;

}

👉 Explanation:

Sample Output

[root@localhost chat]# ./tcpchatserver

bind=0

Enter the message 

kala

message received hema

Enter the message 

[root@localhost chat]# ./tcpchatclient

connect=0

message received kala

Enter the message 

hema

🎯 Summary


This blog post introduced TCP chat client/server programming in C on Unix-like systems, providing a step-by-step explanation of the code examples. The concept of TCP communication was explained, highlighting its reliability and error-checking capabilities.

The chat server implementation demonstrated socket creation, binding to a specific IP address and port, and listening for incoming connections. Once a client connected, the server relayed messages to and from the client using the select() function to monitor both stdin and the client socket.

On the other hand, the chat client code showed socket creation and connection to the server. It allowed users to send and receive messages from the server using select() to monitor stdin and the server socket.

By understanding the provided examples, developers can build more robust and feature-rich chat applications, such as adding support for multiple clients, handling user identities, or implementing encryption for secure communication.

🎯 Key Points