日期:2014-05-20  浏览次数:20705 次

一个简单的socket连接问题。。下面是我的代码。。客户端执行循环后(红字),后面的代码就不执行了。
Server
public class Test4 {
private static int num = 1;
  public static void main(String[] args) throws IOException {
  ServerSocket servsock = new ServerSocket(7000);
  File myFile = new File("src/ImageBase64/one.txt");
  while (true) {
  Socket sock = servsock.accept();
  OutputStream out = sock.getOutputStream();
  BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
   
  int count;
  byte[] buffer = new byte[8192];
  while ((count = bis.read(buffer)) > 0)
  {
  out.write(buffer, 0, count);
  System.out.println(num++);
  }
  System.out.println("传送完成");
  out.flush();
  }
  }
}
Client
public class Test5 {
private static int num = 1;
  public static void main(String[] argv) throws Exception {
  Socket sock = new Socket("111.187.144.106", 7000);
  InputStream is = sock.getInputStream();
  FileOutputStream fos = new FileOutputStream("one.txt");
   
  int count;
  byte[] buffer = new byte[2048];
while ((count = is.read(buffer)) > 0)
{
fos.write(buffer, 0, count);
// System.out.println(num++);
}
  System.out.println("接收完成");
  fos.flush();
  fos.close();
  } 
}

------解决方案--------------------
应该是在while ((count = is.read(buffer)) > 0)堵塞住了,你把server端的out关掉试试。
System.out.println("传送完成");
out.flush();
out.close();
谢谢理解,这东西环境不好配。