Single-Client Chat Using UDP Socket

UDP chat-server configuration for single-client use

Client-side implementation of single-client chat using UDP socket

// This code is written by Souvik Ghosh
// Client Program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>

#define BUFLEN 512
#define PORT 8888

void die(char *s)
{
    perror(s);
    exit(1);
}

void *send_msg(void *arg)
{
    struct sockaddr_in si_other;
    int s, slen=sizeof(si_other);
    

    if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        die("socket");
    }

    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(PORT);
     
    if (inet_aton("127.0.0.1",&si_other.sin_addr) == 0) 
    {
        fprintf(stderr,"inet_aton() failed\n");
        exit(1);
    }
    
    while(1)
    { 
    
        char message[BUFLEN];
        fflush(stdin);
 
        fgets(message, BUFLEN, stdin);
         
        //send the message
        if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1)
        {
            die("sendto()");
        }
        if(strncmp(message,"bye",3)==0)
        exit(0);
    }
}

void *recv_msg(void *arg)
{
    struct sockaddr_in si_me, si_other;
    int s, i, slen = sizeof(si_other) , recv_len;
    
     
    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        die("socket");
    }
     
    memset((char *) &si_me, 0, sizeof(si_me));
     
    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = inet_addr("127.0.0.2");
     
    if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
    {
        die("bind");
    }
     
    while(1)
    {
        char buf[BUFLEN];
        fflush(stdout);
       
        //try to receive some data, this is a blocking call
        if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1)
        {
            die("recvfrom()");
        }
         
        //print details of the client/peer and the data received
        printf("Received packet from %s: %d\n",inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
        printf("Server: %s\n",buf);
        if(strncmp(buf,"bye",3)==0)
        exit(0);
    }
}

int main(void)
{
    pthread_t thread1, thread2;

    if(pthread_create(&thread1, NULL, send_msg, NULL)) 
    {
        fprintf(stderr, "Error creating thread\n");
        return 1;
    }

    if(pthread_create(&thread2, NULL, recv_msg, NULL)) 
    {
        fprintf(stderr, "Error creating thread\n");
        return 1;
    }

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    return 0;
}

Server-side implementation of single-client chat using UDP socket

// This code is written by Souvik Ghosh
// Server program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>

#define BUFLEN 512
#define PORT 8888

void die(char *s)
{
    perror(s);
    exit(1);
}

void *send_msg(void *arg)
{
    struct sockaddr_in si_other;
    int s, slen=sizeof(si_other);
   
    

    if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        die("socket");
    }

    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(PORT);
     
    if (inet_aton("127.0.0.2",&si_other.sin_addr) == 0) 
    {
        fprintf(stderr, "inet_aton() failed\n");
        exit(1);
    }

    while(1)
    {
        char message[BUFLEN];
        fflush(stdin);
        
        fgets(message, BUFLEN, stdin);
         
        //send the message
        if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1)
        {
            die("sendto()");
        }
        if(strncmp(message,"bye",3)==0)
        exit(0);
    }
}

void *recv_msg(void *arg)
{
    struct sockaddr_in si_me, si_other;
    int s, i, slen = sizeof(si_other) , recv_len;
    
     
    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        die("socket");
    }
     
    memset((char *) &si_me, 0, sizeof(si_me));
     
    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = inet_addr("127.0.0.1");
     
    if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
    {
        die("bind");
    }
     
    while(1)
    {
        char buf[BUFLEN];
        fflush(stdout);
         
        //try to receive some data, this is a blocking call
        if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1)
        {
            die("recvfrom()");
        }
         
        //print details of the client/peer and the data received
        printf("Received packet from %s: %d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
        printf("Client: %s\n", buf);
         if(strncmp(buf,"bye",3)==0){
         exit(0);
         }
    }
}

int main(void)
{
    pthread_t thread1, thread2;

    if(pthread_create(&thread1, NULL, send_msg, NULL)) 
    {
        fprintf(stderr, "Error creating thread\n");
        return 1;
    }

    if(pthread_create(&thread2, NULL, recv_msg, NULL)) 
    {
        fprintf(stderr, "Error creating thread\n");
        return 1;
    }

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    return 0;
    
}

Output

output of single-client UDP chat server program

Related Posts

Artificial Intelligence Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Artificial Intelligence Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Mixed Signal Design Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Mixed Signal Design Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Fiber Optic Communication Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Fiber Optic Communication Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Cyber Security Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Cyber Security Suggestions! Elevate Your Learning Experience and Excel with Confidence.

MAKAUT 7th Semester Examination Questions – 2023

Mobile Communication and Networks (PE-EC701C) Neural Network and Fuzzy Logic Control (PE-EC702C/PEROB701B) Principles of Management (HS-HU701)

CSS Cheatsheet

Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…

Leave a Reply

Your email address will not be published. Required fields are marked *