使用dataInputStream和dataOutputStream造成“死锁”或“阻塞”

zylwszylws 2010-04-08 02:19:15
如题,请各位大侠帮我看看,如何解决

//客户端
public class ClientTest_Short {
private ClientSocket_Short cs = null;

private String ip = "localhost";// 设置成服务器IP

private int port = 8821;

private String sendMessage = "sss";

private Socket socket = null;

public ClientTest_Short() {
try {
if (createConnection()) {
sendMessage();
// getMessage();
}

} catch (Exception ex) {
ex.printStackTrace();
}
}

private boolean createConnection() {
cs = new ClientSocket_Short(ip, port);
try {
socket = cs.CreateConnection();
if(socket != null)
{
System.out.print("连接服务器成功!" + "\n");
}
else
{
System.out.println("socket is null");
}
return true;
} catch (Exception e) {
System.out.print("连接服务器失败!" + "\n");
return false;
}

}

private void sendMessage() throws IOException {
String filePath = "D:\\OOo_3.2.0rc5_20100203_Win32Intel_install_wJRE_zh-CN.exe";
File fi = new File(filePath);

System.out.println("文件长度:" + (int) fi.length());
System.out.println("建立socket链接");

DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
DataOutputStream ps = new DataOutputStream(socket.getOutputStream());
//将文件名及长度传给客户端。这里要真正适用所有平台,例如中文名的处理,还需要加工,具体可以参见Think In Java 4th里有现成的代码。
ps.writeUTF(fi.getName());
ps.flush();
ps.writeLong((long) fi.length());
ps.flush();

int bufferSize = 8192;
byte[] buf = new byte[bufferSize];

while (true) {
int read = 0;
if (fis != null) {
read = fis.read(buf);
}

if (read == -1) {
break;
}
ps.write(buf, 0, read);
}
ps.flush();
fis.close();

//接收服务器端成功返回信息
DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
//在这里发生死锁或者阻塞
System.out.println("从服务器返回的信息 = " + inputStream.readUTF());

socket.close();
System.out.println("文件传输完成");
}


public static void main(String arg[]) {
new ClientTest_Short();
}
}

//服务器端
public class ServerTest_Short {
int port = 8821;

void start() {
Socket s = null;
try {
ServerSocket ss = new ServerSocket(port);
while (true) {
// 选择进行传输的文件
String filePath = "D:/compress-image.sh";
File fi = new File(filePath);

System.out.println("文件长度:" + (int) fi.length());

// public Socket accept() throws
// IOException侦听并接受到此套接字的连接。此方法在进行连接之前一直阻塞。

s = ss.accept();
DataInputStream inputStream = null;
try {
inputStream = new DataInputStream(new BufferedInputStream(s.getInputStream()));
} catch (Exception e) {
System.out.print("接收消息缓存错误\n");
return;
}

try {
//本地保存路径,文件名会自动从服务器端继承而来。
String savePath = "E:\\";
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
int passedlen = 0;
long len=0;

savePath += inputStream.readUTF();
System.out.println("savePath = " + savePath);
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath))));
len = inputStream.readLong();

System.out.println("文件的长度为:" + len + "\n");
System.out.println("开始接收文件!" + "\n");

while (true) {
int read = 0;
if (inputStream != null) {
read = inputStream.read(buf);
}
passedlen += read;
if (read == -1) {
break;
}
//下面进度条本为图形界面的prograssBar做的,这里如果是打文件,可能会重复打印出一些相同的百分比
// System.out.println("文件接收了" + (passedlen * 100/ len) + "%\n");
fileOut.write(buf, 0, read);
}
System.out.println("接收完成,文件存为" + savePath + "\n");
fileOut.close();

//将成功信息返回客户端
DataOutputStream outputStream = new DataOutputStream(s.getOutputStream());
outputStream.writeUTF("success");
outputStream.flush();

} catch (Exception e) {
System.out.println("接收消息错误" + "\n");
return;
}

}

} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String arg[]) {
new ServerTest_Short().start();
}
}

//工具类
public class ClientSocket_Short {
private String ip;

private int port;

private Socket socket = null;

DataOutputStream out = null;

DataInputStream getMessageStream = null;

public ClientSocket_Short(String ip, int port) {
this.ip = ip;
this.port = port;
}

/** *//**
* 创建socket连接
*
* @throws Exception
* exception
*/
public Socket CreateConnection() throws Exception {
try {
socket = new Socket(ip, port);
} catch (Exception e) {
e.printStackTrace();
if (socket != null)
socket.close();
throw e;
} finally {
}
return socket;
}

public void sendMessage(String sendMessage) throws Exception {
try {
out = new DataOutputStream(socket.getOutputStream());
if (sendMessage.equals("Windows")) {
out.writeByte(0x1);
out.flush();
return;
}
if (sendMessage.equals("Unix")) {
out.writeByte(0x2);
out.flush();
return;
}
if (sendMessage.equals("Linux")) {
out.writeByte(0x3);
out.flush();
} else {
out.writeUTF(sendMessage);
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
if (out != null)
out.close();
throw e;
} finally {
}
System.out.println("发送结束1");
}

public DataInputStream getMessageStream() throws Exception {
try {
getMessageStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
return getMessageStream;
} catch (Exception e) {
e.printStackTrace();
if (getMessageStream != null)
getMessageStream.close();
throw e;
} finally {
}
}

public void shutDownConnection() {
try {
if (out != null)
out.close();
if (getMessageStream != null)
getMessageStream.close();
if (socket != null)
socket.close();
} catch (Exception e) {

}
}
}
...全文
504 5 打赏 收藏 转发到动态 举报
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
GK20182 2010-09-27
  • 打赏
  • 举报
回复
忘了说“fileOut.write(buf, 0, read);”需要放到“if (read <= buf.length)”上面

测试小文件没问题,大文件同样出现阻塞现象(估计是客户端问题)……等高人解答
GK20182 2010-09-27
  • 打赏
  • 举报
回复
刚测试了一下LZ的代码,问题出在服务端。
把服务端读取客户端信息那处的“if (read == -1)”改为“if (read <= buf.length)”即可。
因为LZ程序中服务端的“if (read == -1)”在客户端输出口不close的情况下,永远不会成立,所以会锁死。

没想到翻了个老帖出来,希望对新手有所帮助。我自己也是菜鸟,有什么说错的请多多指教。
GK20182 2010-09-27
  • 打赏
  • 举报
回复
楼上的没做过Socket通信吧
shuaiAWP 2010-04-08
  • 打赏
  • 举报
回复

//这里添代码

shuaiAWP 2010-04-08
  • 打赏
  • 举报
回复
while循环都是死循环
相关推荐

62,567

社区成员

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