026.4 upload text for network programming

In general, it is in the sealed jar package. Here, you can implement this function through the original steps

##############################################################################
Client steps:
1. Determine the connection address and port
2. Read files
3. Get socket output stream
4, send
5. Send the end tag to the server to end the read operation
6. Receiving return information
7. Close resources

###UploadTextClient.java

public class UploadTextClient {
    /**
     * @param args
     * @throws IOException 
     * @throws UnknownHostException 
     */
    public static void main(String[] args) throws UnknownHostException, IOException {
        System.out.println("Upload file client running......");
        // Client:
        // Steps:
        // 1,Establish socket,Specify the address and port.
        Socket s = new Socket("192.168.1.223", 10006);

        // 2,Source: read text file. Get the data that needs to be converted.
        BufferedReader bufr = new BufferedReader(new FileReader("tempfile\\client.txt"));
        
        // 3,Purpose: network, socket Output stream. Send the entered data to the server.
        PrintWriter out = new PrintWriter(s.getOutputStream(),true);
        
        // 4,Frequent read and write operations.
        String line = null;
        while((line=bufr.readLine())!=null){
            out.println(line);
        }
        
        //Send an end tag to the server. This tag is a convention tag. It's a bit of a hassle. It can be simpler. Use socket Object shutdownOutput();
        s.shutdownOutput();//An end tag was sent to the server. It can let the server end the read operation.
        
        // 5,Source: socket,socket Read the stream, read the upload success information sent by the server.
        BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String info = bufIn.readLine();
        System.out.println(info);
        
        // 6,Close the resource.
        bufr.close();
        s.close();
    }

}

 

###########################################################################################
Server steps:
1. Set up server socket
2. Receive the socket of the client
3. Establish file object
4. Multiple receiving and writing
5. Return the received information
6. Close resources

###UploadTextServer.java

public class UploadTextServer {
    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {


        System.out.println("Upload text server to run....");
        // Server:
        // Train of thought:
        // 1,Create server socket Explicit port.
        ServerSocket ss = new ServerSocket(10006);
        while (true) {
            // Gets the client object.
            Socket s = ss.accept();
            System.out.println(s.getInetAddress().getHostAddress()+".....connected");

            // 2,Source: socket Input stream. Read the data sent by the client.
            BufferedReader bufIn = new BufferedReader(new InputStreamReader(
                    s.getInputStream()));

            // 3,Purpose: documentation.
            PrintWriter pw = new PrintWriter(new FileWriter("tempfile\\server.txt"),true);

            // 4,Frequent read and write operations.
            String line = null;
            while ((line = bufIn.readLine()) != null) {
//                if("over".equals(line)){
//                    break;
//                }
                pw.println(line);
            }
            
            // 5,Send back to the client to upload the word "success".
            PrintWriter out = new PrintWriter(s.getOutputStream(),true);
            out.println("Upload success");
            
            // 6,Close the client.
            s.close();
        }
    }
}

Keywords: Java socket network

Added by PHPHorizons on Sun, 15 Dec 2019 22:02:04 +0200