-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomClient.java
51 lines (41 loc) · 1.39 KB
/
RandomClient.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
package main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;
/**
*
* @author alyacarina
*/
public class RandomClient extends BasicClient {
boolean run;
@Override
public void establishConnection(int portNumber, String hostName){
try(Socket clientSide = new Socket(hostName, portNumber);
PrintWriter outBound = new PrintWriter(clientSide.getOutputStream(), true);
BufferedReader inBound = new BufferedReader(
new InputStreamReader(clientSide.getInputStream()));){
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask(){
@Override
public void run() {
outBound.println((int) (Math.random() * 50));
}
}, 0, 80);
String next = inBound.readLine();
while(next!=null){
System.out.println(next);
next = inBound.readLine();
}
} catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
RandomClient pc = new RandomClient();
pc.establishConnection(4454);
}
}