-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextClient.java
More file actions
41 lines (34 loc) · 1.89 KB
/
Copy pathTextClient.java
File metadata and controls
41 lines (34 loc) · 1.89 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
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class TextClient {
public static void main(String[] args) {
try {
// Accept input from the user
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a message to send to the server: ");
String userInput = scanner.nextLine();
// Make a connection to the server running on localhost at port 6013
Socket sock = new Socket("127.0.0.1", 6013);
// Send the user's input to the server
PrintWriter pout = new PrintWriter(sock.getOutputStream(), true);
pout.println("Client says: " + userInput);
// Get the input stream from the socket
InputStream in = sock.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
// Read the response from the server and print it to the console
String line;
while(true){ // WHILE TRUE LOOP, we dont want to stop the communiaction
if( (line = bin.readLine()) != null){ // if there is something to read, we will print it. this is like the wait
System.out.println(line); // prints to the CLIENT terminal
}
System.out.print("Enter a message to send to the server: "); // prompts client to send emssage
userInput = scanner.nextLine(); // stores input
pout.println("Client says: " + userInput); // // sends message to the stream
if(userInput.equals("")) break; // break if no input, using .equals cause strings are objects, scanner does not store null, stores empty string
}
// Close the socket connection
sock.close();
} catch (IOException ioe) { System.err.println(ioe); }
}
}