-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
58 lines (46 loc) · 1.91 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 8888);
System.out.println("Connected to server.");
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("Server says: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
socket.close();
System.out.println("Disconnected from server.");
}
}*/
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException {
// create a socket and connect to the server on port 8888
Socket socket = new Socket("localhost", 8888);
System.out.println("Connected to server.");
// create input and output streams for the socket
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// read input from user and send to the server
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("Server says: " + in.readLine());
}
// close the connection
out.close();
in.close();
stdIn.close();
socket.close();
System.out.println("Disconnected from server.");
}
}