-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNihar_MT2020009_tcp_client_4a.java
More file actions
69 lines (49 loc) · 2.5 KB
/
Nihar_MT2020009_tcp_client_4a.java
File metadata and controls
69 lines (49 loc) · 2.5 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Nihar_MT2020009_tcp_client_4a {
public static void main(String args[]) throws Exception {
String ip_address = "127.0.0.1"; //loopback address
int port_no = 9995;
String IP1_string="";
String IP2_string="";
String SMASK_string="";
System.out.println("Connecting to server");
Socket s = new Socket(ip_address, port_no);
//socket is connected and a connection request is sent to the server at given ip and port no
System.out.println("Connected to server");
while ((!IP1_string.equals("stop")) ) {
Scanner sc = new Scanner(System.in); //fetching input from user
System.out.println("Enter IP1,IP2,SUBNET MASK in form A.B.C.D");
System.out.println("Enter the 1st IP Address (Enter 'stop': to exit)");
IP1_string=sc.nextLine();
if(IP1_string.equals("stop")) { //if "stop" is given as input,terminate the program.
System.out.println("Connection terminated");
s.close();
break;
}
System.out.println("Enter the 2nd IP Address");
IP2_string=sc.nextLine();
System.out.println("Enter the Subnet Mask");
SMASK_string=sc.nextLine();
OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream()); //writing the input_string onto the socket stream
PrintWriter out = new PrintWriter(os);
out.println(IP1_string);
os.flush();
// OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream()); //writing the input_string onto the socket stream
// PrintWriter out = new PrintWriter(os);
out.println(IP2_string);
os.flush();
// OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream()); //writing the input_string onto the socket stream
// PrintWriter out = new PrintWriter(os);
out.println(SMASK_string);
os.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); //read the server data via the inputstream
String RESULT = br.readLine();
System.out.println("RESULT FROM SERVER :" + RESULT+"\n"); //display the RESULT
}
}
}