-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDPserver.java
144 lines (110 loc) · 3.61 KB
/
UDPserver.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package code;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;
import ca.pfv.spmf.test.MainTestDefMe_saveToFile;
//send info back to client ->diff function
public class UDPserver {
//create the server socket
private DatagramSocket server;
public UDPserver() throws SocketException {
this.server = new DatagramSocket(509);
//System.out.println(server.isBound());
}
//receive the objects from the client
private void receiveLogInfo() throws IOException, ClassNotFoundException {
int port=0;
InetAddress ipAddress=null;
FileWriter writer = new FileWriter("log_results.txt");
//receive the info and write it to a file
while(true) {
byte[] buffer = new byte[4096];
//recieve the datagram packet from the client
DatagramPacket dps = new DatagramPacket(buffer, buffer.length);
server.receive(dps);
//take the IP adress and port of the sender
ipAddress = dps.getAddress();
port = dps.getPort();
getAddress(port, ipAddress);
String incomingLine = new String(dps.getData());
//writing the content to a file in the appropriate format
String[] vars = incomingLine.split(",");
if(vars.length<4) {// || (incomingLine.charAt(0)!='"')) {
break;
}
String content = vars[5];
//extract the first number from the content description
String number="";
int temp=0;
for(int i=0;i<content.length();i++) {
char c=content.charAt(i);
if(Character.isDigit(c)) {
temp=1;
while(Character.isDigit(c)) {
number+=c;
i=i+1;
c=content.charAt(i);
}
}
if(temp==1)
break;
}
//write the user id for each log
writer.write(number);
writer.flush();
writer.write("\r\n");
writer.flush();
}
writer.close();
}
//write the ipAddress and port to a file
void getAddress(int port, InetAddress ipAddr) throws IOException {
FileWriter writerAddr = new FileWriter("address.txt");
writerAddr.write(Integer.toString(port));
writerAddr.flush();
writerAddr.write("\r\n");
writerAddr.flush();
String host=ipAddr.getHostName();
writerAddr.write(host);
writerAddr.flush();
writerAddr.close();
}
void sendInfo() throws IOException {
//get the IpAddress and port of the host
String ipHost = null;
int port=0;
Scanner sc1 = new Scanner(new FileInputStream("address.txt"));
port = Integer.parseInt(sc1.nextLine());
ipHost=sc1.nextLine();
InetAddress ipAddr= InetAddress.getByName(ipHost);
sc1.close();
//send the info to the client
//System.out.println("send info");
String fileName = "output.txt";
String line = "";
Scanner scnr = new Scanner(new FileInputStream(fileName));
//first line is total number of rows
while(scnr.hasNextLine()) {
line=scnr.nextLine();
byte[] data = line.getBytes();
DatagramPacket datagramPacket = new DatagramPacket(data, data.length, ipAddr, port);
server.send(datagramPacket);
}
scnr.close();
}
public static void main(String[] args) throws SocketException, IOException, ClassNotFoundException {
UDPserver s = new UDPserver();
s.receiveLogInfo();
//run DefMe algorithm
final String[] res = {"log_results.txt","output.txt"};
System.out.println("Main\n");
MainTestDefMe_saveToFile.main(res);
//send info back to client
s.sendInfo();
}
}