package com.ljy.inernet.tcpTest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo02 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=new ServerSocket(7777);
Socket socket=serverSocket.accept();
InputStream inputStream = socket.getInputStream();
FileOutputStream fos=new FileOutputStream(new File("xxx.docx"));
byte[] buffer=new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
package com.ljy.inernet.tcpTest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class ClientDemo02 {
public static void main(String[] args) throws IOException {
Socket socket=new Socket("127.0.0.1",7777);
OutputStream outputStream = socket.getOutputStream();
FileInputStream fis=new FileInputStream(new File("E:\\IdeaProject\\internet\\改善读后感.docx"));
byte[] buffer=new byte[1024];
int len;
while ((len=fis.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
outputStream.close();
fis.close();
socket.close();
}
}
|