Search This Blog

Loading...

WAP to Implement Iterative echo Client-Server using Socket Programming - Distributed System

• File Name : Server.c

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>

int main()
{
    int sd,accept_sd,N,len;
    char buffer[100];
    struct sockaddr_in addr,clientaddr;
    len = sizeof(clientaddr);

    printf("Iterative Server Program:");
 
    sd = socket(AF_INET, SOCK_STREAM, 0); 
   
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(9500);

    bind(sd, (struct sockaddr *)&addr, sizeof(addr));
    listen(sd, 5);
    accept_sd = accept(sd, (struct sockaddr *)&clientaddr, &len);
    N = read(accept_sd,buffer,sizeof(buffer));
    write(1,buffer,N);


    close(accept_sd);
    close(sd);
    return 0;
}



•File Name : Client.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>

int main()
{
    int sd,N;
    char buffer[100];
    struct sockaddr_in addr;

    sd = socket(AF_INET, SOCK_STREAM, 0); 
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(9500);

    connect(sd,(struct sockaddr *)&addr, sizeof(addr));
    N = read(0,buffer,sizeof(buffer));
    write(1,buffer,N);

   
    return 0;
}

No comments:

Post a Comment