A Simple File Transfer Program

File transfer requires a server and a client to transfer files after the connection is established.
First is the server.
Writing mistakes
1. When listening, I forgot to give the loop, so that the file can only be transferred once and then it ends.
2. To get the customer's ip, I only wrote getInetAddress(), but not getHostAddress(), and the resulting IP will have an additional "/".
3. Write while as if, resulting in only one run and only 1 KB when the file is downloaded.
4. Forget to close the data stream, resulting in file transmission damage.

public static void main(String[] args) throws IOException {
		File file = new File("Here is the directory and name of the file you want to transfer.");
//		This is to create a service first but not a port, and then set it up later.
//		ServerSocket server = new ServerSocket();
//		server.bind(new InetSocketAddress(8888));
		//Occupy port 8888 to create a service
		ServerSocket server = new ServerSocket(8888);
		System.out.println("The service has been created,Waiting for connection...");
		//Loop monitoring
			//Start listening and return the Socket object once the client connects
			Socket s = server.accept();
			System.out.println("Client Connected!"+s.getInetAddress().getHostAddress());
			//Get the output stream of Socket
			OutputStream os = s.getOutputStream();
			FileInputStream is = new FileInputStream(file);
			BufferedInputStream bis = new BufferedInputStream(is);
			BufferedOutputStream bos = new BufferedOutputStream(os);
			byte[] b = new byte[1024];
			int len;
			while((len = bis.read(b)) != -1) {


1. When writing the target file, the name of the file is not written, which causes the program to report errors and the folder to refuse access. (Suffix name cannot be wrong)
2. The ip address is dynamic, and it always uses the previous day's ip to cause an error.
3. Finish the closure and documents, use up the closure, use up the closure, use up the closure!

public class MyClient {
	public static void main(String[] args) throws UnknownHostException, IOException {

		File file = new File("Here is the path and name of the file you want to write.");
		// Connect to a specified ip, a specified port of service
		Socket s = new Socket("", 8888);
		// Getting the byte input stream based on Socket
		InputStream is = s.getInputStream();
		//Create a byte output stream based on the target file
		FileOutputStream fos = new FileOutputStream(file);
		//Converting low-level byte streams to high-level buffer streams
		BufferedInputStream  bis = new BufferedInputStream(is) ;
		//Write bytes to the underlying output stream
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		byte[] b = new byte[1024];
		int len = 0;
		//Judge whether the document has been read or not
		while((len = bis.read(b) ) != -1) {
		//Control that the last write will not be written to excess content causing file corruption


Writing code encounters errors is normal, but to learn to debug, learn to analyze where the problem is, and determine the scope of the problem, find out where the logical error is, and use a small book to write down, nothing to look at, beware of stepping on the pit again!!!

Keywords: socket

Added by gordo2dope on Thu, 10 Oct 2019 20:59:47 +0300