62,614
社区成员
发帖
与我相关
我的任务
分享
/**
*服务端
*/
public class Service2 {
public static void main(String[] args) {
InputStream in = null;
try{
ServerSocket service = new ServerSocket(8888);
Socket socket = service.accept();//阻塞
OutputStream out = socket.getOutputStream();
byte [] data = new byte[1024];
//开始读取文件
in = new FileInputStream("e:\\SNET.txt");
while (in.read(data)!=-1) {
out.write(data);
out.flush();
}
in.close();
System.out.println("传送文件结束...");
service.close();
System.out.println("服务器关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 客户端
*/
public class Client2 {
public static void main(String[] args) {
OutputStream out = null;
try {
Socket socket = new Socket("127.0.0.1",8888);
InputStream in = socket.getInputStream();
out = new FileOutputStream("e:\\123.txt");
byte[]data = new byte[1024];
int length = 0;
//这里出现的错误
while((in.read(data))!=-1){
out.write(data );
}
out.close();
System.out.println("文件接收结束...");
socket.close();
System.out.println("客户端结束");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*服务端
*/
public class Service2 {
public static void main(String[] args) {
InputStream in = null;
try{
ServerSocket service = new ServerSocket(8888);
Socket socket = service.accept();//阻塞
OutputStream out = socket.getOutputStream();
byte [] data = new byte[1024];
//开始读取文件
in = new FileInputStream("e:\\SNET.txt");
while (in.read(data)!=-1) {
out.write(data);
out.flush();
}
in.close();
//*****************服务器端输出流必须关闭*****************//
out.close();
System.out.println("传送文件结束...");
service.close();
System.out.println("服务器关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
}