Linux Socket Programming, server will perform arithmetic operations
Code for Client
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main(int argc,int *argv[])
{
int n1=atoi(argv[1]),n2=atoi(argv[2]),res=0;
char operator=argv[3][0];
int sock_id=socket(AF_UNIX,SOCK_STREAM,0);
if(sock_id<0)
{
printf("Error in getting socket\n");
return 0;
}
struct sockaddr_in clientstruct;
clientstruct.sin_family=AF_UNIX;
clientstruct.sin_addr.s_addr=inet_addr("127.0.0.1");
clientstruct.sin_port=1025;
int ret=connect(sock_id,(struct sockaddr *)&clientstruct,sizeof(clientstruct));
write(sock_id,&n1,sizeof(n1));
write(sock_id,&n2,sizeof(n2));
write(sock_id,&operator,sizeof(operator));
int ret1=read(sock_id,&res,sizeof(res));
printf("FROM SERVER:%d \n Bytes=%d\n",res,ret1);
close(sock_id);
return 0;
}
Code for Server
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main()
{
int n1,n2,res=0;
char operator;
int session_id;
int server_id=socket(AF_UNIX,SOCK_STREAM,0);
if(server_id<0)
{
printf("Error in getting socket\n");
return 0;
}
struct sockaddr_in serverstruct,clientstruct;
serverstruct.sin_family=AF_UNIX;
serverstruct.sin_addr.s_addr=inet_addr("127.0.0.1");
serverstruct.sin_port=1025;
int i=bind(server_id,(struct sockaddr*)&serverstruct,sizeof(serverstruct));
if(i<0)
{
printf("Error in bind\n");
return 0;
}
i=listen(server_id,10);
if(i<0)
{
printf("Error in listening\n");
return 0;
}
int client_len=sizeof(serverstruct);
while(1)
{
printf("Waiting for the client\n");
session_id=accept(server_id,(struct sockaddr*)&serverstruct,&client_len);
read(session_id,&n1,sizeof(n1));
read(session_id,&n2,sizeof(n2));
read(session_id,&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(session_id,&res,sizeof(res));
close(session_id);
}
close(server_id);
return 0;
}
No comments:
Post a Comment