-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBingoServer.java
More file actions
178 lines (147 loc) · 4.43 KB
/
BingoServer.java
File metadata and controls
178 lines (147 loc) · 4.43 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
public class BingoServer {
static BufferedWriter myLogOut = null;
static ServerSocket serverSocket;
static int fileId = 0;
static ArrayList<String> fileId2fileCode;
static ArrayList<String> fileName = null;
static String savePath = "/home/bingo/bingosave";
public static void main(String args[]){
BingoServer bingoServer = new BingoServer();
bingoServer.init();
}
private void init(){
try{
myLogOut = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("/home/bingo/BingoServerLog.txt",false)));
fileName = new ArrayList<>();
fileId2fileCode = new ArrayList<>();
Log("init success");
serverSocket = new ServerSocket(44180);
while (true){
Log("正在监听端口44180,准备接入下一个设备");
Socket socket=serverSocket.accept();
Log("新设备呼入");
int port = getPort();
Log("通信端口为"+port);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeInt(port);
dos.flush();
socket.close();
dos.close();
TransmissionTheard theard= new TransmissionTheard(port);
theard.start();
Log("启动线程:"+theard.getId());
}
}catch (Exception e){
e.printStackTrace();
try {
Log(e.getMessage());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
private class TransmissionTheard extends Thread{
private int localPort;
public TransmissionTheard(int lport) {
localPort = lport;
}
@Override
public void run(){
try {
ServerSocket serverSocket = new ServerSocket(localPort);
Socket socket = serverSocket.accept();
String fileCode = generateFileCode();
Log("TheardId: "+getId()+": 文件标识符为"+fileCode);
int bufferSize = 1024;
byte[] buf = new byte[bufferSize];
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
dos.writeUTF(fileCode);
String fileName = dis.readUTF();
String Path =savePath + '/'+fileName;
Log("接收的文件名为:"+fileName);
DataOutputStream fileos = new DataOutputStream(
new BufferedOutputStream(new BufferedOutputStream(
new FileOutputStream(Path))));
long fileLength = dis.readLong();
Log("接收的文件长度为:"+fileLength);
while (true){
int readLength = -1;
if (dis!=null){
readLength = dis.read(buf);
}
Log("读取的长度为"+readLength);
if (readLength==-1){
break;
}
fileos.write(buf,0,readLength);
}
Log("文件接受完毕");
serverSocket.close();
dos.close();
fileos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
Log(e.getMessage());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
//获取一个没有被占用的接口
public static int getPort(){
for (int i = 44181;;i++){
if (checkPort(i))
return i;
}
}
public static boolean checkPort(int port){
try
{
ServerSocket ss = new ServerSocket(port);
ss.close();
return true;
}
catch (Exception e)
{
return false;
}
}
//生成随机的不重复的6位标识码
public static String generateFileCode(){
Random random = new Random();
String code;
while (true){
code = String.valueOf(100000 + random.nextInt(899999));
if (!fileId2fileCode.contains(code)) break;
}
return code;
}
//log
public static void Log(String content) throws IOException{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
myLogOut.write(df.format(new Date())+" "+content+'\n');
myLogOut.flush();
System.out.println(df.format(new Date())+" "+content+'\n');
}
}