This blog includes subject like Computer Organization, Microprocessor, Digital Electronics, System Programming

Pages

This blog includes subject like Computer Organization, Microprocessor, Digital Electronics, System Programming

Powered by Blogger.

Sunday, August 18, 2019

Write a program using TCP socket for wired network for following a. Say Hello to Each other b. Calculator (Arithmetic)


a. Say Hello to Each other 

//tcp_client.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<error.h>

#define ERROR -1
#define BUFFER 1024

int main(int argc, char **argv)
{
int sock; // client socket descriptor

struct sockaddr_in remote_server;

char send_data[BUFFER]= "hi from client Dnyanesh";
// send buffer: hold information to send to server
char recv_data[BUFFER];
// receive buffer: hold information to received from server

int sockaddr_len = sizeof(struct sockaddr_in);
// socket address length
int data_len;
// store data length of send_data or recv_data length 

if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket error. ");
exit(-1);
}

remote_server.sin_family = AF_INET; //IPv4
remote_server.sin_port = htons(atoi(argv[2]));
//htons: host to network short byte order, atoi: convert a //string to an integer
remote_server.sin_addr.s_addr = inet_addr(argv[1]);
//inet_addr: function converts the Internet host address cp ??IPv4 numbers-and-dots notation into binary data in network byte order.
bzero(&remote_server.sin_zero , 8);
// function sets the first 8 bytes of the area starting at &remote_server.sin_zero to zero

if((connect(sock, (struct sockaddr*)&remote_server, sockaddr_len)) == -1)
{
perror("connect error");
exit(-1);
}

//send(sock, send_data, strlen(send_data), 0);



// data_len = recv(sock, recv_data, BUFFER,0);
//recv_data[data_len] ='\0';
// printf("Message received: %s\n", recv_data);



close(sock);
return 0;
}


//tcp_server.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<error.h>

#define ERROR -1
#define BUFFER 1024
#define MAX_CLIENTS 2

int main(int argc, char **argv)
{
int sock, cli; // sock: server socket descriptor, cli: client socket descriptor
struct sockaddr_in server, client;

char send_data[BUFFER]= "Hello from server Dhanu"; 
// send buffer: hold information to send to client
char recv_data[BUFFER]; 
// receive buffer: hold information to received from client

int sockaddr_len = sizeof(struct sockaddr_in); 
// socket address length
int data_len; 
// store data length of send_data or recv_data length   


if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket error. ");
exit(-1);
}
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[1]));
server.sin_addr.s_addr = INADDR_ANY;
bzero(&server.sin_zero , 8);

if((bind(sock, (struct sockaddr *)&server, sockaddr_len)) == -1)
{
perror("bind error");
exit(-1);
}
if((listen(sock,10)) == -1)
{
perror("listen error");
exit(-1);
}

while(1)
{
if((cli = accept(sock, (struct sockaddr*)&client, &sockaddr_len )) == -1)
{
perror("accept error");
exit(-1);
}


printf("New client connected to port: %d and IP %s \n", ntohs(client.sin_port), inet_ntoa(client.sin_addr)); 
//  inet_ntoa: network byte order, to a string in IPv4 dotted-decimal notation.
// data_len = recv(cli, recv_data, BUFFER,0);
//recv_data[data_len] = '\0';
// printf("Message received: %s\n", recv_data);

//send(cli, send_data, strlen(send_data), 0);

//printf("client disconnected \n");


close(cli);
}
return 0;
}


b. Calculator (Arithmetic) 

//Arith_client.c

#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<error.h>

#define ERROR -1
#define BUFFER 1024
#define MAX_CLIENTS 2

int main(int argc, char **argv)
{
int n1,n2,res=0;
char operator;
int sock; // client socket descriptor 

struct sockaddr_in remote_server;

char send_data[BUFFER]= "hello from client Dnyanesh"; 
// send buffer: hold information to send to server
char recv_data[BUFFER];
// receive buffer: hold information to received from server

int sockaddr_len = sizeof(struct sockaddr_in); 
// socket address length
int data_len; // store data length of send_data or recv_data length   
if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket error. ");
exit(-1);
}

remote_server.sin_family = AF_INET; //IPv4
remote_server.sin_port = htons(atoi(argv[2])); 
//htons: host to network short byte order, atoi: convert a string to an integer
remote_server.sin_addr.s_addr = inet_addr(argv[1]); //inet_addr: function converts the Internet host address cp IPv4 numbers-and-dots notation into binary data in network byte order. 
bzero(&remote_server.sin_zero , 8);
// function sets the first 8 bytes of the area starting at &remote_server.sin_zero to zero 

if((connect(sock, (struct sockaddr*)&remote_server, sockaddr_len)) == -1)
{
perror("connect error");
exit(-1);
}

printf("\nEnter Operator: ");
scanf("%c", &operator);

printf("\nEnter value of n1: ");
scanf("%d",&n1);

printf("\nEnter value of n2: ");
scanf("%d",&n2);

write(sock,&n1,sizeof(n1));
write(sock,&n2,sizeof(n2));
write(sock,&operator,sizeof(operator));

int ret1 = read(sock,&res,sizeof(res));
printf("FROM SERVER:%d \t Bytes=%d\n",res,ret1);

close(sock);
return 0;
}


//Arith_server.c

#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<error.h>

#define ERROR -1
#define BUFFER 1024
#define MAX_CLIENTS 2

int main(int argc, char **argv)
{
int n1,n2,res=0;
char operator;
int sock, cli; 
// sock: server socket descriptor, cli: client socket descriptor
struct sockaddr_in server, client;

char send_data[BUFFER]= "Hello from server Dhanu"; 
// send buffer: hold information to send to client
char recv_data[BUFFER]; // receive buffer: hold information to received from client

int sockaddr_len = sizeof(struct sockaddr_in); 
// socket address length
int data_len; 
// store data length of send_data or recv_data length   

if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket error. ");
exit(-1);
}
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[1]));
server.sin_addr.s_addr = INADDR_ANY;
bzero(&server.sin_zero , 8);

if((bind(sock, (struct sockaddr *)&server, sockaddr_len)) == -1)
{
perror("bind error");
exit(-1);
}
if((listen(sock,10)) == -1)
{
perror("listen error");
exit(-1);
}

while(1)
{
if((cli = accept(sock, (struct sockaddr*)&client, &sockaddr_len )) == -1)
{
perror("accept error");
exit(-1);
}
printf("New client connected to port: %d and IP %s \n", ntohs(client.sin_port), inet_ntoa(client.sin_addr)); //  inet_ntoa: network byte order, to a string in IPv4 dotted-decimal notation.



read(cli,&n1,sizeof(n1));
read(cli,&n2,sizeof(n2));
read(cli,&operator,sizeof(operator));

switch(operator)
{
case '+': res=n1+n2;
break;
case '-': res=n1-n2;
break;
case '*': res=n1*n2;
break;
case '/': res=n1/n2;
break;
default:
printf("invalid operation\n");
}

printf("From CLIENT:n1=%d\n",n1);
printf("From CLIENT:n2=%d\n",n2);
printf("From CLIENT:operator=%c\n",operator);  
   
int b=write(cli,&res,sizeof(res));

close(cli);
}
close(sock);
return 0;
}


0 comments:

Post a Comment