-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathServer.java
More file actions
67 lines (62 loc) · 1.5 KB
/
Server.java
File metadata and controls
67 lines (62 loc) · 1.5 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
58
59
60
61
62
63
64
65
66
67
package com.hjy.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;
import com.hjy.serverThread.ServerThread;
public class Server {
@SuppressWarnings("unused")
private ServerSocket server;
private int port;
private static Properties prop;
static{
prop = new Properties();
try {
prop.load(new FileInputStream(new File("src/source/property.properties")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//初始化,创建服务器
public void init(){
try {
port = Integer.parseInt(prop.getProperty("port"));
server = new ServerSocket(port);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void receive(){
try {
Socket client = server.accept();
ServerThread thread = new ServerThread(client);
thread.start();
thread.join();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Server() {
}
public static void main(String[] args) {
//拿到server对象
Server server2 = new Server();
//创建服务器
server2.init();
while(true){
server2.receive();
}
}
}