socket 文件传输问题

dhq0819 2007-10-16 12:08:24
在开发中设计了一个文件传输程序,有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;

}

...全文
205 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
DiligencyMan 2007-10-23
  • 打赏
  • 举报
回复
估计是网络设备的问题吧!
hexizheng 2007-10-19
  • 打赏
  • 举报
回复
测试一下 当时服务器端的网络流量,可能真的达到你的局域网的带宽了,
另外服务器端接收到数据以后,放在一个缓存中,开另外一个线程去写文件。
读写文件很耗时间的,这样就会很大的减缓网络传输的速度。特别是当几个文件同时写的时候
dhq0819 2007-10-19
  • 打赏
  • 举报
回复
mark?没有报任何错误,我怀疑是网络设备的问题,今天我又测试了很多变,没有出现这个问题
syhhl007 2007-10-18
  • 打赏
  • 举报
回复
mark!
syhhl007 2007-10-18
  • 打赏
  • 举报
回复
mark!
syhhl007 2007-10-18
  • 打赏
  • 举报
回复
mark!
guangtoucaipan 2007-10-18
  • 打赏
  • 举报
回复
mark!
tomsmish 2007-10-17
  • 打赏
  • 举报
回复
CesConstant ce
DesEncrypt des
这两个类是干什么用的?
DiligencyMan 2007-10-17
  • 打赏
  • 举报
回复
用来做des加密的啦!
beiouwolf 2007-10-17
  • 打赏
  • 举报
回复
tcp传输还是不要用bufferedwriter的好
用常规的byte[]+stream来读写更好,更容易控制流量

DesEncrypt 这是用来做des加密的

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧