-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClient.java
56 lines (43 loc) · 1.53 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
/**
* Student: Rustam Zokirov
* ID: U1910049
* Lab #9: Networking, socket programming
* Client side
*/
// import libraries we need
import java.io.*; // DataInputStream/DataOutputStream
import java.net.*; // ServerSocket/Socket
import java.util.Scanner;
public class Client {
private DataOutputStream toServer;
private DataInputStream fromServer;
public static void main(String[] args) {
new Client();
}
public Client() { // constructor
try {
// Create a socket to connect to the server
Socket socket = new Socket("localhost", 8000);
Scanner sc = new Scanner(System.in);
String message;
do {
System.out.print("Please enter a message (0 = exit): ");
message = sc.next();
if (message.compareTo("0") == 0)
break;
// Create an input stream to receive data from the server
fromServer = new DataInputStream(socket.getInputStream());
// Create an output stream to send data to the server
toServer = new DataOutputStream(socket.getOutputStream());
toServer.writeUTF(message);
toServer.flush();
String convertedMessage = fromServer.readUTF();
System.out.println("Converted message: " + convertedMessage);
System.out.println();
} while(true);
}
catch (IOException ex) {
System.out.println(ex.toString() + '\n');
}
}
}