forked from jlyang1990/Spark_Python_Do_Big_Data_Analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketserver.java
executable file
·67 lines (53 loc) · 1.94 KB
/
socketserver.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
59
60
61
62
63
64
65
66
67
package com.v2maestros.socketserver;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.List;
import java.util.Random;
public class socketserver {
public static void main(String[] args) {
ServerSocket socServer = null;
String line;
DataInputStream is;
PrintStream os;
Socket clientSocket = null;
Path path = FileSystems.getDefault().getPath("/Users/jlyang/Documents/Intern&Job/Spark_Python_Do_Big_Data_Analytics", "streamingtweets.txt");
List<String> lines = null;
try {
lines = Files.readAllLines(path,StandardCharsets.UTF_8);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
socServer = new ServerSocket(9000);
System.out.println("Socket opened");
System.out.println("Total records read :" + lines.size());
}
catch (IOException e) {
System.out.println(e);
}
try {
clientSocket = socServer.accept();
System.out.println("Accepted client request from : " + clientSocket.getInetAddress() );
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
while (true) {
//Pick a random line
int randomline = (int) (Math.random() * lines.size());
System.out.println("Publishing " + lines.get(randomline));
os.println( lines.get(randomline) );
os.flush();
//Randomly sleep 1 - 3 seconds
Thread.sleep((long) (Math.random() * 3000));
}
}
catch (Exception e) {
System.out.println(e);
}
}
}
/* Run command
java -cp c:\Personal\V2Maestros\JavaWorkSpace\SocketServer\bin com.v2maestros.socketserver.socketserver
*/