-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNihar_MT2020009_tcp_client_4b.java
More file actions
47 lines (35 loc) · 1.75 KB
/
Nihar_MT2020009_tcp_client_4b.java
File metadata and controls
47 lines (35 loc) · 1.75 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Arrays;
import java.util.Scanner;
public class Nihar_MT2020009_tcp_client_4b {
public static void main(String args[]) throws Exception {
String ip_address = "127.0.0.1"; //loopback address
int port_no = 9998;
String input_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 ((!input_string.equals("stop")) ) {
System.out.println("Enter the String to sort (Enter 'stop': to exit)");
Scanner scanner = new Scanner(System.in); //fetching input from user
input_string = scanner.nextLine();
System.out.println(input_string);
OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream()); //writing the input_string onto the socket stream
PrintWriter out = new PrintWriter(os);
out.println(input_string);
os.flush();
if(input_string.equals("stop")) { //if "stop" is given as input,terminate the program.
System.out.println("Connection terminated");
break;
}
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); //read the server data via the inputstream
String sorted_string = br.readLine();
System.out.println("Sorted string is :" + sorted_string); //display the sorted string
}
}
}