-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientIntrest.java
More file actions
46 lines (38 loc) · 1.47 KB
/
ClientIntrest.java
File metadata and controls
46 lines (38 loc) · 1.47 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
// Java program to illustrate Client Side Programming
// for Simple Calculator using TCP
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class ClientIntrest
{
public static void main(String[] args) throws IOException
{
InetAddress ip = InetAddress.getLocalHost();
int port = 4444;
Scanner sc = new Scanner(System.in);
// Step 1: Open the socket connection.
Socket s = new Socket(ip, port);
// Step 2: Communication-get the input and output stream
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
while (true)
{
// Enter the equation in the form-
// "operand1 operation operand2"
System.out.print("Enter the details in the form: ");
System.out.println("'amount rate time'");
String inp = sc.nextLine();
if (inp.equals("bye"))
break;
// send the equation to server
dos.writeUTF(inp);
// wait till request is processed and sent back to client
String ans = dis.readUTF();
System.out.println("Answer=" + ans);
//sc.close();
}
}
}