The goal of this post is to implement a TCP client and server. Your TCP client/server will communicate over the network and exchange data. The user interface (i.e., what’s displayed to the user) should look the same for both the TCP applications.
The following programs run on different terminal with a unique IP address and port number after success full initiation of server and client programs the user enter the number and operator on the client port and send to the server then server calculate the number with entered operator and send the result to the client.
calcclient.c
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<ctype.h>
#include<stdlib.h>
#include<netinet/in.h>
#define BUF_SIZE 1000
int main(int argc , char *argv[])
{
int sockfd ,n;
char sbuffer[BUF_SIZE], rbuffer[BUF_SIZE];
unsigned int servport;
char* servip;
char x[100],y[100],z[100];
servport = atoi(argv[1]);
servip = argv[2];
struct sockaddr_in serveraddress;
sockfd = socket(PF_INET,SOCK_STREAM,0);
serveraddress.sin_port = htons(servport);
serveraddress.sin_family = PF_INET;
serveraddress.sin_addr.s_addr = inet_addr(servip);
while(1)
{
int con = connect(sockfd,(struct sockaddr *) &serveraddress,sizeof(serveraddress));
do{
printf("Enter 1st number : ");
scanf("%s", sbuffer);
send(sockfd, sbuffer, strlen(sbuffer) + 1, 0);
printf("data sent (%s)\n", sbuffer);
printf("Enter 2nd number : ");
scanf("%s", sbuffer);
send(sockfd, sbuffer, strlen(sbuffer) + 1, 0);
printf("data sent (%s)\n", sbuffer);
printf("Enter :1 for + , 2 for -, 3 for *, 4 for / ");
scanf("%s", sbuffer);
send(sockfd, sbuffer, strlen(sbuffer) + 1, 0);
printf("data sent (%s)\n", sbuffer);
n=recv(sockfd, sbuffer, BUF_SIZE, 0);
printf("received from server %s\n", sbuffer);
}while(strcmp(sbuffer, "quit"));
}
return 0;
}
calcserver.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<unistd.h>
#include<ctype.h>
#define MAX_SIZE 1000
char* stringReverse(char *str){
char ch;
char first[100];
int length=strlen(str);
for(int i=0;i<length;i++)
{
ch=str[i];
if(ch == '+'||'*'||'/'||'-')
{
printf("%s is in the position of %d\n",&ch,i);
}
str[i]=str[--length];
str[length]=ch;
}
return str;
}
int main(int argc , char *argv[])
{
int serversocket,clientsocket;
char sbuffer[MAX_SIZE],rbuffer[MAX_SIZE];
unsigned int clientaddrlen;
unsigned short serverport;
int opr;
int n,num1,num2,num3,sum = 0;
serverport = atoi(argv[1]);
struct sockaddr_in serveraddress,clientaddress;
serversocket = socket(PF_INET,SOCK_STREAM,0);
serveraddress.sin_family = PF_INET;
serveraddress.sin_port = htons(serverport);
serveraddress.sin_addr.s_addr = INADDR_ANY;
bind(serversocket,(struct sockaddr *) &serveraddress,sizeof(serveraddress));
listen(serversocket,5);
printf("binding done waiting for client .....\n");
while(1)
{
clientaddrlen = sizeof(clientaddress);
clientsocket = accept(serversocket,(struct sockaddr *)&clientaddress,&clientaddrlen);
printf("connected to the client ip = %s\nand portnumber = %d\n",inet_ntoa(clientaddress.sin_addr),ntohs(clientaddress.sin_port));
do{
memset(rbuffer,0x0,MAX_SIZE);
n=recv(clientsocket, rbuffer, MAX_SIZE, 0);
num1=atoi(rbuffer);
printf("recieved first number (%d)\n",num1);
n=recv(clientsocket, rbuffer, MAX_SIZE, 0);
num2=atoi(rbuffer);
printf("recieved second number (%d)\n",num2);
n=recv(clientsocket, rbuffer, MAX_SIZE, 0);
opr=atoi(rbuffer);
printf("recieved operator (%d)\n",opr);
switch(opr)
{
case 1:
sum = num1+num2;
break;
case 2:
sum = num1-num2;
break;
case 3:
sum = num1*num2;
break;
case 4:
sum = num1/num2;
break;
default:
break;
}
sprintf(sbuffer,"%d",sum);
send(clientsocket, sbuffer, strlen(sbuffer) + 1, 0);
printf("send number (%d)\n",sum);
}while(abs(strcmp(rbuffer, "quit")));
}
close(serversocket);
return 0;
}
output:
0 comments:
Post a Comment