1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <netdb.h>
#include <errno.h>
#include<map>
#include<time.h>
#include<sstream>
using namespace std;
int oper(int , int ,int );
struct category
{
int fir,sec,tot;
string timecal,tOper;
};
void error(const char *msg)
{
cout<<msg<<endl;
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
int mynetid,mysecid,clientid,clientsecid,num,ss,errorno;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n,iKey,uKey,uPos,uVal;
char endc;
time_t rawtime;
string name,chOper;
iKey=0;
map<const int,category>categories;
struct category tmp;
if (argc < 2)
{
cout<<"ERROR, no port provided\n";
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
int yes=1;
if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1)
{
cout<<"setsockopt";
exit(1);
}
if ((bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr))) < 0)
cout<<"ERROR on binding :"<<errno<<endl;
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
if (newsockfd < 0)
cout<<"ERROR on accept";
do
{
recv(newsockfd,&num,4,0);
switch(num)
{
case 1: case 2: case 3: case 4:{ // Logical operation and insert data into map
recv(newsockfd,&mynetid,4,0);
clientid=mynetid;
cout<<"First value is :\t"<<clientid<<endl;
recv(newsockfd,&mysecid,4,0);
clientsecid=mysecid;
cout<<"Second value is :\t"<<clientsecid<<endl;
int logicaloper;
logicaloper=oper(clientid,clientsecid,num);
cout<<"Result was sent :\t"<<logicaloper<<endl;
if(send(newsockfd,&logicaloper,4,0) < 0)
{
cout<<"error while sending result"<<endl;
close(newsockfd);
shutdown(newsockfd,2);
close(sockfd);
shutdown(sockfd,2);
}
time(&rawtime);
tmp.fir=clientid;
tmp.sec=clientsecid;
tmp.tot=logicaloper;
if(num==1)
tmp.tOper="Add";
else if(num==2)
tmp.tOper="Sub";
else if(num==3)
tmp.tOper="Mul";
else
tmp.tOper="Div";
tmp.timecal=ctime(&rawtime);
int iValue = iKey;
categories.insert(pair<const int,struct category>(iValue,tmp));
iKey = iKey+1;}
break;
case 5: //update the map data
recv(newsockfd,&uKey,4,0);
recv(newsockfd,&uPos,4,0);
recv(newsockfd,&uVal,4,0);
if(uPos==1)
{
categories[uKey].fir=uVal;
}
if(uPos==2)
{
categories[uKey].sec=uVal;
}
chOper=categories[uKey].tOper;
if(chOper=="Add")
categories[uKey].tot=oper(categories[uKey].fir,categories[uKey].sec,1);
else if(chOper=="Sub")
categories[uKey].tot=oper(categories[uKey].fir,categories[uKey].sec,2);
else if(chOper=="Mul")
categories[uKey].tot=oper(categories[uKey].fir,categories[uKey].sec,3);
// if(chOper=="Div")
else
categories[uKey].tot=oper(categories[uKey].fir,categories[uKey].sec,4);
break;
case 6:
recv(newsockfd,&uKey,4,0);
categories.erase(uKey);
case 7:{ //Display the map data
int counts;
map<int ,category>::iterator it=categories.begin();
counts=categories.size();
string str="",kd="",fd="",sd="",td="";
send(newsockfd,(const char*)&counts,4,0);
while(it!=categories.end())
{
//string str="",kd="",fd="",sd="",td="";
stringstream kst,fst,sst,tst;
kst<<it->first;
kd=kst.str();
fst<<it->second.fir;
fd=fst.str();
sst<<it->second.sec;
sd=sst.str();
tst<<it->second.tot;
td=tst.str();
str=str+" "+kd+" "+fd+" "+sd+" "+td+" "+it->second.tOper+" "+it->second.timecal;
send(newsockfd,str.c_str(),str.size(),0);
// str.append("\r\n");
str="";
it++;
}
}
break;
default:
cout<<"The client enters not correct option"<<endl;
}
recv(newsockfd,&endc,4,0);
}while(endc!='n');
map<int ,category>::iterator it=categories.begin();
while(it!=categories.end())
{
cout<<it->first<<"\t"<<it->second.fir<<"\t"<<it->second.sec<<"\t"<<it->second.tot<<"\t"<<it->second.tOper<<"\t"<<it->second.timecal<<"\t"<<endl;
it++;
}
categories.clear();
tmp.tot=NULL;
close(sockfd);
shutdown(sockfd,2);
close(newsockfd);
shutdown(newsockfd,2);
return 0;
}
int oper(int a,int b,int c)
{
int aaa,cliid,clisecid,opt;
cliid=a;clisecid=b;opt=c;
aaa=0;
switch(opt)
{
case 1:
aaa=cliid+clisecid;
return aaa;
case 2:
aaa=cliid-clisecid;
return aaa;
case 3:
aaa=cliid*clisecid;
return aaa;
case 4:
aaa=cliid/clisecid;
return aaa;
}
return 0;
}
|