-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.java
109 lines (108 loc) · 4.83 KB
/
server.java
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
import java.net.*;
import java.io.*;
public class server
{
public static void main(String args[]) throws IOException
{
System.out.println("============Server======================");
ServerSocket ss = new ServerSocket(10000);
System.out.println("Server is Setup");
Socket s = ss.accept();
DataInputStream din = new DataInputStream(s.getInputStream());
// String str = (String)din.readUTF();
// System.out.println("Client : "+str);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
// dout.writeUTF("Hello Client");
String str = "";
int c = -1;
int a = 0,b = 0,ans = 0;
while(c != 0)
{
str = "===========Calculator Menu==============\nPress 1 for Addition : \nPress 2 for Subtraction : \nPress 3 for Multiplication : \nPress 4 for Division : \nPress 0 to Exit\n";
System.out.println(str);
dout.writeUTF(str);
c = Integer.parseInt(din.readUTF());
System.out.println("Client : "+c);
switch(c)
{
case 0:
{
dout.writeUTF("Exited");
ss.close();
break;
}
case 1:
{
dout.writeUTF("Enter a for a+b expression : ");
System.out.println("Enter a for a+b expression : ");
a = Integer.parseInt(din.readUTF());
System.out.println("Client : "+a);
System.out.println("Enter b for a+b expression : ");
dout.writeUTF("Enter b for a+b expression : ");
b = Integer.parseInt(din.readUTF());
System.out.println("Client : "+b);
ans = a + b;
System.out.println(ans);
dout.writeUTF(Integer.toString(ans));
break;
}
case 2:
{
dout.writeUTF("Enter a for a-b expression : ");
System.out.println("Enter a for a-b expression : ");
a = Integer.parseInt(din.readUTF());
System.out.println("Client : "+a);
System.out.println("Enter b for a-b expression : ");
dout.writeUTF("Enter b for a-b expression : ");
b = Integer.parseInt(din.readUTF());
System.out.println("Client : "+b);
ans = a - b;
System.out.println(ans);
dout.writeUTF(Integer.toString(ans));
break;
}
case 3:
{
dout.writeUTF("Enter a for a*b expression : ");
System.out.println("Enter a for a*b expression : ");
a = Integer.parseInt(din.readUTF());
System.out.println("Client : "+a);
System.out.println("Enter b for a*b expression : ");
dout.writeUTF("Enter b for a*b expression : ");
b = Integer.parseInt(din.readUTF());
System.out.println("Client : "+b);
ans = a * b;
System.out.println(ans);
dout.writeUTF(Integer.toString(ans));
break;
}
case 4:
{
dout.writeUTF("Enter a for a/b expression : ");
System.out.println("Enter a for a/b expression : ");
a = Integer.parseInt(din.readUTF());
System.out.println("Client : "+a);
System.out.println("Enter b for a/b expression : ");
dout.writeUTF("Enter b for a/b expression : ");
b = Integer.parseInt(din.readUTF());
System.out.println("Client : "+b);
if(b == 0)
{
dout.writeUTF("Invalid Operand b which is 0.");
continue;
}
double ans1 = a / b;
System.out.println(ans1);
dout.writeUTF(Double.toString(ans1));
break;
}
default:
{
System.out.println("Invalid Choice Please Try Again.");
dout.writeUTF("Invalid Choice Please Try Again.");
break;
}
}
}
}
}