socket通信 数据阻塞的问题

skyfors 2012-12-27 05:44:40
主要思路是从服务器那边下载一个xml文件,里面包含图片路径。然后客户端通过这个路径通过socket读取过来显示。现在问题出在双方第二次交互也就是拿到文件路径后到服务器下载这部分~~调试很久都不行~~

server端核心代码:
public class Server implements Runnable {
OutputStream out;
sendFile sendFile;

@Override
public void run() {
try {
String message;

ServerSocket server = new ServerSocket(8000);

Socket socket = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = socket.getOutputStream();

String address = socket.getInetAddress().toString() + ":"
+ socket.getPort();
System.out.println(address);
while (true) {
message = in.readLine();
System.out.println("message: " + message);
if (message.equals("connect...")) {
System.out.println(message);
sendFile = new sendFile(out);
if (message.equals("download...")) {
System.out.println("...");
message = in.readLine();
sendFile = new sendFile(message, out);
// Fileout.write();
}
}

}

} catch (Exception e) {
System.out.println("连接失败");
} finally {
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
FileUtil fileutil = new FileUtil();
try {
WriteXml myxml = new WriteXml("d:/picture.xml");
myxml.toWrite(fileutil);
myxml.toSave();
System.out.print("Your writing is successful!");
} catch (ParserConfigurationException exp) {
exp.printStackTrace();
System.out.print("Your writing is failed!");
}
Thread thread = new Thread(new Server());
thread.start();

}

}

public class sendFile {
public sendFile(OutputStream out) throws IOException {

File file = new File("d:/picture.xml");
FileInputStream fin = new FileInputStream(file);
// byte[] b = new byte[90];
int n = fin.read();
while (n != -1) {
out.write(n);
n = fin.read();
}
}

public sendFile(String dir, OutputStream out) throws IOException {
File file = new File(dir);
FileInputStream fin = new FileInputStream(file);
// byte[] b = new byte[90];

int n = fin.read();
while (n != -1) {
out.write(n);
n = fin.read();
}
}
}


client端核心代码:
public class client implements Runnable {
static List<String> dir_name = new ArrayList<String>();
static List<String> dir_path = new ArrayList<String>();
BufferedInputStream in;
downFile downFile;

@Override
public void run() {
try {
// TODO Auto-generated method stub
Socket socket = new Socket(new Socket().getLocalAddress(), 8000);
System.out.println("socket=" + socket);
in = new BufferedInputStream(socket.getInputStream());
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
out.println("connect...");
downFile = new downFile(in);

if (dir_path.size() != 0) { 《《调试发现这行木有执行到
System.out.println("download...");
out.println("download...");

for (int i = 0; i < dir_path.size(); i++) {
out.println(dir_path.get(i));
downFile = new downFile(dir_path.get(i), in);
}
}
} catch (Exception e) {
} finally {
}
}

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Thread thread = new Thread(new client());
thread.start();

BufferedReader fin = new BufferedReader(new FileReader("d:/a.xml"));
String tempString = null;
while ((tempString = fin.readLine()) != null) {
System.out.println(tempString);
}

SaxParseXml sax = new SaxParseXml();
dir_name = sax.getDir_name();
dir_path = sax.getDir_path();

}
}

public class downFile {
public downFile(InputStream in) throws IOException {
FileOutputStream fout = new FileOutputStream("d:/a.xml");
// byte[] buffer = new byte[90];
int n = in.read();
while (n != -1) {
fout.write(n);
// System.out.println(2);
n = in.read();
}
}

public downFile(String dir, InputStream in) throws IOException {
dir = "e:/" + dir.substring(dir.indexOf("/") + 1, dir.length());
System.out.println(dir);
FileOutputStream fout = new FileOutputStream(dir);

// byte[] buffer = new byte[90];
int n = in.read();
System.out.println(1);
while (n != -1) {
fout.write(n);
// System.out.println(2);
n = in.read();
}
}
...全文
186 1 打赏 收藏 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
eviljordan 2012-12-29
  • 打赏
  • 举报
回复
晕死。。我一个close()都没看见

62,620

社区成员

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

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