<>Java: be based on TCP agreement socket File transfer of network programming

<> Let's get to know it first socket Basic concepts
<>socket Also called socket :
Between application processes on different hosts in the network , Abstraction of endpoints for two-way communication .

The simple understanding is :

Communication between two hosts , You need to know each other ip, Port number and other information , And a host set of these information :

Can be understood as an endpoint , This is the socket

Both sides use the socket as a kind of coordinate , Establishing information channels , Form a connection ( Two points connect a straight line )

After a brief understanding of the concept of socket , Let's see how to get through java socket Programming to achieve

Receiving and sending files of two hosts :

<> The code is as follows :

<> Sender :
import java.io.*; import java.net.InetAddress; import java.net.Socket; /** *
sender */ public class TcpSendCilent { public static void main(String[] args)
throws Exception { //1. adopt socket object establish socket connect Socket socket = new Socket(
InetAddress.getByName("127.0.0.1"), 9000); //2, Create a byte stream OutputStream os =
socket.getOutputStream(); //3. Byte file input stream read file // Avoid mistakes Write the absolute path of the file to be sent FileInputStream
fis= new FileInputStream(new File("F:/TEST/1604407051256.jpg")); //4. Byte file output stream
Write a document byte[] buffer=new byte[1024*10]; int len; while ((len=fis.read(buffer))!=-1
){ //socket Programming based on I/O flow therefore // Output stream write to byte array file = Send file to receiver os.write(buffer,0,len); }
// Notify the server that I'm done call shutdownOutput() Otherwise, the subsequent code execution will be affected socket.shutdownOutput();
// Make sure the server is finished accepting , Disconnect // Get the response from the receiver That's it ! response InputStream inputStream = socket.
getInputStream(); ByteArrayOutputStream baos=new ByteArrayOutputStream(); byte[]
bytes2=new byte[1024*10]; int len2; while ((len2=inputStream.read(bytes2))!=-1)
{ baos.write(bytes2,0,len2); } // Displays the response of the receiver System.out.println(baos.toString());
// After sending , Finally, close all resources // Good habit of closing resources This part is simple // It is generally recommended to make a non null judgment before closing baos.close(); fis.close();
os.close(); socket.close(); } }
<> Receiver :
import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** *
receiving end */ public class TcpAcceptServer { public static void main(String[] args)
throws IOException { // adopt ServerSocket object Create receiver socket ServerSocket serverSocket=null;
// socket Socket socket=null; // Byte input stream InputStream in=null; // The default parameter defines the port number The sender sends according to this port number
serverSocket=new ServerSocket(9000); socket = serverSocket.accept(); //socket object
Get input stream , Get the papers in = socket.getInputStream(); // Send the sender File output stream by file byte Output to the specified path
FileOutputStream fos= new FileOutputStream(new File("F:/recevice.jpg")); int len
; byte[] bytes=new byte[1024*10]; while ((len=in.read(bytes))!=-1){ fos.write(
bytes,0,len); } // Notify the sender that the acceptance is complete OutputStream outputStream = socket.getOutputStream(
); outputStream.write(" I'm the document recipient : I have accepted it successfully ".getBytes()); // After acceptance , Good habit of closing resources This part is simple
// It is generally recommended to make a non null judgment before closing fos.close(); serverSocket.close(); socket.close(); in.close()
; } }
<> test method :

<> The above code can be pasted directly

1, Run the receiver first and wait for acceptance ,

2, Rerun sender ,

3, Wait for the receiver to respond to the sender : I'm the document recipient : I have accepted it successfully ;

4, The recipient specifies the directory to accept , If the received file exists, it means the receiving is successful !

<> summary :

this demo It's not very difficult to achieve , It mainly tests our understanding of io Flow proficiency

Technology
©2019-2020 Toolsou All rights reserved,
python3 Of tkinter Design of login interface +mysql Import data of database A magic lossless compression experiment to shake “ information theory ”!!!【Java Details of knowledge points 3】 Serializable and Deserialize What is polymorphism ? What is the mechanism to achieve polymorphism ?【 Le Bo TestPro】 Force button 416. Dividing equal sum subsets knapsack problem 【 Data structure and algorithm 13】 Binary tree Smart screen and smart wearable development : Component method Model 2 Addition, subtraction, multiplication and division of operations C++ realization 《 Labyrinth 》 Games 【 Data structure and algorithm 8】 The maze problem of recursion