-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
50 lines (43 loc) · 1.33 KB
/
Copy pathClient.java
File metadata and controls
50 lines (43 loc) · 1.33 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
48
49
50
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
public class Client {
private static Socket socket;
public static void main(String args[]) {
String host = System.getenv("HOST_IP");
String port = System.getenv("HOST_PORT");
System.out.println("posting messages on " + host +":" + port);
for (int i = 0; i < 10; i++) {
try {
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, Integer.parseInt(port));
// Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String msg = "Hello from XOR";
String sendMessage = msg + "_" + i;
bw.write(sendMessage);
bw.flush();
try {
// sending the actual Thread of execution to sleep X milliseconds
Thread.sleep(1000);
} catch (Exception e) {
System.out.println("Exception : " + e.getMessage());
}
System.out.println("Message sent to the server : " + sendMessage);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
// Closing the socket
try {
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}