-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
41 lines (36 loc) · 1.39 KB
/
Client.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
package cs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
try {
System.out.println("Waiting for connection.....");
InetAddress localAddress = InetAddress.getLocalHost();
try (Socket clientSocket = new Socket(localAddress, 6013);
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader br = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()))) {
System.out.println("Successfully connected to server");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter text (Type quit to quit):");
String inputLine = scanner.nextLine();
if ("quit".equalsIgnoreCase(inputLine)) {
scanner.close();
break;
}
out.println(inputLine);
String response = br.readLine();
System.out.println("Server response: " + response);
}
}
} catch (IOException ex) {
System.out.println("Failed to connecto to Server");
}
}
}