insert one key with three value in map in c++ with client server program

I completed the program but how insert three values without using struct???

Server Program
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;
}

Last edited on
and my client program is
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <iostream>
#include <errno.h>
using namespace std;
void error(const char *msg)
{
        cout<<msg<<endl;
        exit(0);
}

int main(int argc, char *argv[])
{
        int sockfd, portno, n;
        struct sockaddr_in serv_addr;
        struct hostent *server;
        int mynetid,mysecid,num,logicaloper,errorno,uKey,uPos,uVal,counts;
        char endc,buff[1024];
        string str;
        if (argc < 3)
        {
                cout<<"usage %s hostname port\n"<< argv[0]<<endl;
                exit(0);
        }
        portno = atoi(argv[2]);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0)
                cout<<"ERROR opening socket";
        server = gethostbyname(argv[1]);
        if (server == NULL)
        {
        cout<<"ERROR, no such host\n";
        exit(0);
        }
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
        serv_addr.sin_port = htons(portno);
        if ((connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr))) < 0)
        {
                cout<<"ERROR connecting\n"<<errno<<endl;
                exit(0);
        }
        do
        {
                cout<<"Select the option\n1.Add\n2.Sub\n3.Mul\n4.Div\n5.Update\n6.Delete\n7.Display\n8.Exit\n";
                cin>>num;
                switch(num)
                {

 case 1: case 2: case 3: case 4://Logical operation
                        send(sockfd,(const char*)&num,4,0);
                        cout<<"Enter first value :\t";
                        cin>>mynetid;
                        cout<<"Enter second value :\t";
                        cin>>mysecid;
                        send(sockfd,(const char*)&mynetid,4,0);
                        send(sockfd,(const char*)&mysecid,4,0) ;
                        recv(sockfd,&logicaloper,4,0);
                        cout<<"Result is : "<<logicaloper<<"\n";
                        break;
                case 5: //Update the data in map
                        send(sockfd,(const char*)&num,4,0);
                        cout<<"Enter the key to update the value:\n";
                        cin>>uKey;
                        send(sockfd,(const char*)&uKey,4,0);
                        cout<<"Enter the number for first value or seconnd value"<<endl;
                        cin>>uPos;
                        send(sockfd,(const char*)&uPos,4,0);
                        cout<<"Enter the value"<<endl;
                        cin>>uVal;
                        send(sockfd,(const char*)&uVal,4,0);
                        break;
                case 6 : //Delete the data in map
                        send(sockfd,(const char*)&num,4,0);
                        cout<<"Enter the key to delete the value:\n";
                        cin>>uKey;
                        send(sockfd,(const char*)&uKey,4,0);
                        break;
                case 7: //Display the data in map
                        send(sockfd,(const char*)&num,4,0);
                        recv(sockfd,&counts,4,0);
                        while(counts>0)
                        {

                                memset(buff,0,1024);
                                recv(sockfd,buff,1024,0);
                                cout<<buff<<endl;
                                counts--;
                        }
                        break;
                default :
                        send(sockfd,(const char*)&num,4,0);
                        cout<<"Enter Correct option"<<endl;
                }
                cout<<"Do you want continue y or n"<<endl;
                cin>>endc;
                send(sockfd,&endc,4,0);
        }while(endc!='n');
        close(sockfd);
        shutdown(sockfd,2);
        return 0;
}
how insert three values without using struct???

Um... by using a value type that isn't a struct?

I'm not really clear what you're asking here. Why don't you want to use struct in your program?
if i use struct, the memory takes more. so i reduce the wastage of memory
So you think using a struct with 3 values in it uses a lot more memory than just using 3 values?
yes. one more doubt, can i transfer float value over client and server??
for the reason divide operation...............
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>

struct Wibble
{
	int n1;
	int n2;
	int n3;
};


int main() 
{  
	Wibble myStruct;

	std::cout << "Size of struct: " << sizeof(myStruct) << std::endl;
	std::cout << "Size of 3 ints: " << sizeof(int)*3 << std::endl;

	
	return 0;
}


Examine the output of that...
can i declare the category as pointer
std::map<std::string, category*> categories;
Of course. You can use any type in a vector, as long as it's copyable.

Topic archived. No new replies allowed.