Socket file transfer

There is FTP (File Transfer Protocol) in the network protocol, which is specially used for transferring files. But today we use Socket to directly realize the file transfer between the client and the server, and only need to make a little change in the last Socket communication to achieve the purpose of file transfer.

step

1. Create the server side

package FileSocket;

import java.io. *;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        File file=new File("E:\\Client.java");//The absolute path to save the file
        ServerSocket ss=new ServerSocket(6666);//Create a client and specify the port number

        //The server starts listening
        Socket socket=ss.accept();

        InputStream is=socket.getInputStream(); //Get the input stream that comes with the socket
        OutputStream os=socket.getOutputStream();//Get the output stream that comes with the socket

        BufferedReader br=new BufferedReader(new InputStreamReader(is));//The input buffer stream is used to receive data from the client
        PrintWriter pw=new PrintWriter(new FileWriter(file),true);//Print (output) the data in the cache to the file
        String temp=null;
        while ((temp=br.readLine())!=null){//读
            pw.println(temp);//Write
            pw.flush();//Refresh
        }
        System.out.println("Accept success!");
        br.close();//Close
        pw.close();
        ss.close();
        socket.close();
    }
}

Create the server side, put the client's data into the Buffer cache stream through the Socket, and then write the data in the cache stream to the file (read and write at the same time).

2. Create a client

package FileSocket;

import java.io. *;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException {
        File file=new File("src/FileSocket/Client.java");//The path of the file to be transferred (relative path)
        Socket socket=new Socket("localhost",6666);//Specify the client's IP address and port number

        InputStream is=socket.getInputStream();
        OutputStream os=socket.getOutputStream();

        BufferedReader br=new BufferedReader(new FileReader(file));//Used to read the data in the file
        PrintWriter pw=new PrintWriter(os,true);//Transfer the file to the server
        String temp=null;
        while ((temp=br.readLine())!=null){//读
            pw.println(temp);//Write (write to the other party through Socket)
            pw.flush();
        }
        System.out.println(file.getName()+"File to transfer!");
        br.close();//Close
        pw.close();
        socket.close();
    }
}

Create a client, read the file into the Buffer cache, and then transmit the data in the cache to the server through Socket.

3. Open the server first, then open the client

At this time, you will see the Client.java file under the E drive, which is the code in the client of your project.

Summarize 

When we transfer files, it is similar to copying files through IO streams, except that Socket is added. The client first reads the file into the memory (buffer area), and then transmits the data in the buffer area to the server side through Socket. The server side reads the data from the client into the buffer, and then writes the buffer's data to the file. In this process, both the client and the server perform read and write operations, but the read and write directions are different.


       Chicken Soup of the Day: With Choices, There Will Be Regrets. So no regrets!


Over!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324825735&siteId=291194637