-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientThread.java
More file actions
46 lines (36 loc) · 1.09 KB
/
ClientThread.java
File metadata and controls
46 lines (36 loc) · 1.09 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
package socket;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Random;
public class ClientThread extends Thread {
private Socket socket;
public ClientThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
PrintStream printStream;
try {
printStream = new PrintStream(socket.getOutputStream());
printStream.println(randomMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String randomMessage() {
Random random = new Random();
String charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
int length = 10;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
int randomIndex = random.nextInt(charset.length());
char randomChar = charset.charAt(randomIndex);
sb.append(randomChar);
}
return sb.toString();
}
}