-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyServer.java
More file actions
59 lines (37 loc) · 1.42 KB
/
myServer.java
File metadata and controls
59 lines (37 loc) · 1.42 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
51
52
53
54
55
56
57
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.json.JSONObject;
class myServer
{
public static void main(String args[]) throws Exception
{
List<Object> list = Collections.synchronizedList(new ArrayList<>()); //contains veh information and is shared by threads
final int port = 9099;
DatagramSocket serverSocket = new DatagramSocket(port);
UDPHandler udp = new UDPHandler(serverSocket, list);
Thread t1 = new Thread(udp);
t1.start(); //start UDP
ServerSocket welcomeSocket = new ServerSocket(port); //TCP
while (true)
{
Socket s = null; //per connection
try
{
s = welcomeSocket.accept();
s.setSoTimeout(30000);
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
Thread t2 = new ClientHandler(s, dis, dos, list);
t2.start();
}
catch (IOException e){
s.close();
}
}
}
}