socket 文件传输问题
在开发中设计了一个文件传输程序,有N个客户端同时向服务器发送程序,2个客户端同时发送没有问题,可是当客户端超过是三个的时候中途就会出现网络阻塞的情况,使得整个局域网内的所有客户端无法发送文件
请各位高手帮忙
以下是接受程序:
public class ReceiveFile extends Thread {
private Socket socket;
private BufferedReader bf;
private PrintStream ps;
private CesConstant ces;
private DesEncrypt des;
public ReceiveFile(Socket socket) {
des = new DesEncrypt();
des.getKey("社会是残酷的!");
ces = new CesConstant();
this.socket = socket;
try {
ps = new PrintStream(socket.getOutputStream());
bf = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
//System.out.println("客户端已经关闭!!");
}
}
public void run() {
while (true) {
try {
this.sleep(500);
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
String filename = "";
String receivePath = "", projectPath = "";
FileWriter fw = null;
BufferedWriter bw = null;
PrintWriter pw = null;
File file = null;
try {
projectPath = ces.props
.getProperty("system.pathonfo.project_path");
receivePath = ces.props
.getProperty("system.pathonfo.center_receive_path");
receivePath = projectPath + receivePath;
String fileSendOver = ces.props
.getProperty("system.tranceinfo.file_send_over");
String fileReceiveOver = ces.props
.getProperty("system.tranceinfo.file_receive_over");
String dealPath = ces.props
.getProperty("system.pathonfo.center_deal_path");
dealPath = projectPath + dealPath;
File fr = new File(receivePath);
if (!fr.exists()) {
fr.mkdirs();
}
File fd = new File(dealPath);
if (!fd.exists()) {
fd.mkdirs();
}
filename = bf.readLine();
if (filename == null) {
continue;
}
file = new File(receivePath + filename);
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
pw = new PrintWriter(bw);
String str;
while ((str = bf.readLine()) != null) {
if (fileSendOver.equals(str)) {
pw.close();
bw.close();
fw.close();
File f = new File(dealPath + filename);
if (f.exists()) {
f.delete();
}
file.renameTo(f);
ps.println(fileReceiveOver);
filename = bf.readLine();
if (filename == null) {
continue;
}
file = new File(receivePath + filename);
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
pw = new PrintWriter(bw);
continue;
}
// System.out.println(str);
// String ming = des.getDesString(str);
pw.println(str);
}
} catch (Exception e) {
// TODO Auto-generated catch block
try {
pw.close();
bw.close();
fw.close();
if (file.exists()) {
file.delete();
}
socket.close();
this.interrupt();
break;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//System.out.println("客户端关闭");
}
}
以下是发送程序
public void sendFile(String file){
PesConstant p = new PesConstant();
String fileSendPath = p.props.getProperty("system.pathinfo.upload_send_path");
String projectPath = p.props.getProperty("system.pathinfo.project_path");
fileSendPath = projectPath + fileSendPath;
String fileSendOver = p.props.getProperty("system.tranceinfo.file_send_over");
String sendSpeed = p.props.getProperty("system.tranceinfo.file_send_speed");
long speed = Long.parseLong(sendSpeed);
String filePath = fileSendPath + file;
File f = new File(filePath);
long sengTime = 0;
sengTime = f.length()/speed;
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(f);
br = new BufferedReader(fr);
ps.println(file);
String str;
while ((str = br.readLine()) != null) {
// String mi = des.getEncString(str);
ps.println(str);
}
br.close();
fr.close();
ps.println(fileSendOver);
sleep(2*sengTime+4000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 接收确认
* @return boolean 是否收到
*/
public boolean receiveVali(){
boolean receive = false;
PesConstant p = new PesConstant();
String fileReceiveOver = p.props.getProperty("system.tranceinfo.file_receive_over");
try {
String validator = bf.readLine();
if(fileReceiveOver.equals(validator)){
receive = true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
}
return receive;
}