-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServIntrest.java
More file actions
49 lines (39 loc) · 1.58 KB
/
ServIntrest.java
File metadata and controls
49 lines (39 loc) · 1.58 KB
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
// Java program to illustrate Server Side Programming
// for Simple Calculator using TCP
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
public class ServIntrest
{
public static void main(String args[]) throws IOException
{
// Step 1: Establish the socket connection.
ServerSocket ss = new ServerSocket(4444);
Socket s = ss.accept();
// Step 2: Processing the request.
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
while (true)
{
// wait for input
String input = dis.readUTF();
if(input.equals("bye"))
break;
System.out.println("Values received:-" + input);
int result;
// Use StringTokenizer to break the equation into operand and
// operation
StringTokenizer st = new StringTokenizer(input);
int amount = Integer.parseInt(st.nextToken());
int rate = Integer.parseInt(st.nextToken());
int time = Integer.parseInt(st.nextToken());
double interest = (amount * rate * time) / 100.0;
System.out.println("Sending the result...");
// send the result back to the client.
dos.writeUTF(Double.toString(interest));
}
}
}