import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服务器端
9. *
10. * @author Kree
11. */
public class Server {
public static void main(String[] args ) throws Exception {
//server socket waits for requests to come in over the network. It performs some operation
//based on that request, and then possibly returns a result to the requester.
ServerSocket server = new ServerSocket(8888);
// is an endpoint for communication between two machines.
Socket recive = server.accept();
InputStream in = recive.getInputStream();
OutputStream fos = new FileOutputStream("d:/abc.txt");
int n = 0;
while ((n = in.read()) != -1) {
fos.write(n);
}
fos.flush();
in.close();
fos.close();
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
8. * 客户端
9. *
10. * @author Kree
11. */
public class Client {
public static void main(String[] args ) throws Exception {
//模拟连接的一端
Socket client = new Socket("localhost", 8888);
InputStream fis = new FileInputStream(new File("d:/table1.txt"));
OutputStream out = client.getOutputStream();
int n = 0;
while ((n = fis.read()) != -1) {
out.write(n);
}
fis.close();
out.close();
}
}