-
Notifications
You must be signed in to change notification settings - Fork 1
/
Calculator.txt
200 lines (167 loc) · 5.28 KB
/
Calculator.txt
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
/* CALCULATOR:
SERVER:
Problem Statement: To implement client-server calculator.
Server Side
Please pass port no as command line argument
*/
#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 <ctype.h>
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[1024];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"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);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
int num1 , num2 , ans , choice;
S: n = write(newsockfd,"Enter Number 1 : ",strlen("Enter Number 1")); //Ask for Number 1
if (n < 0) error("ERROR writing to socket");
read(newsockfd, &num1, sizeof(int)); //Read No 1
printf("Client - Number 1 is : %d\n" , num1);
n = write(newsockfd,"Enter Number 2 : ",strlen("Enter Number 2")); //Ask for Number 2
if (n < 0) error("ERROR writing to socket");
read(newsockfd, &num2, sizeof(int)); //Read Number 2
printf("Client - Number 2 is : %d\n" , num2);
n = write(newsockfd,"Enter your choice : \n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Exit\n",strlen("Enter your choice : \n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Exit\n")); //Ask for choice
if (n < 0) error("ERROR writing to socket");
read(newsockfd, &choice, sizeof(int)); //Read choice
printf("Client - Choice is : %d\n" , choice);
switch(choice)
{
case 1:
ans = num1 + num2;
break;
case 2:
ans = num1 -num2;
break;
case 3:
ans = num1*num2;
break;
case 4:
ans = num1/num2;
break;
case 5 :
goto Q;
break;
}
write(newsockfd , &ans , sizeof(int));
if(choice != 5)
goto S;
Q: close(newsockfd);
close(sockfd);
return 0;
}
*/
CLIENT:
Please pass ip address and port no as command line arguments in the same sequence.
*/
#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<ctype.h>
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[1024];
if (argc < 3)
{
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"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)
error("ERROR connecting");
int num1 ;
int num2 ;
int ans;
int choice;
S:bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("Server - %s\n",buffer);
scanf("%d" , &num1); //Enter No 1
write(sockfd, &num1, sizeof(int));
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("Server - %s\n",buffer);
scanf("%d" , &num2);
write(sockfd, &num2, sizeof(int)); //Send No 2 to Server
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("Server - %s",buffer);
scanf("%d" , &choice); //Enter choice
write(sockfd, &choice, sizeof(int)); //Send choice to Server
if (choice == 5)
goto Q;
read(sockfd , &ans , sizeof(int)); //Read Answer from Server
printf("Server - The answer is : %d\n" , ans); //Get Answer from server
if(choice != 5)
goto S;
Q: printf("You chose to exit, Thank You so much.");
close(sockfd);
return 0;
}