<>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(""), 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

©2019-2020 Toolsou All rights reserved,
VUE+Canvas Achieve desktop Pinball brick elimination games C/C++ Memory model 2019PHP Interview questions ( Continuously updated )PHPspringboot2 Separation of front and rear platforms ,token Put in header Pit for verification Vue SpringBoot conduct Excel download element-ui Step on pit record 45 The 12-year-old programmer was turned down , Is the workplace wrong ?Python Web frame Pandas Fundamentals of statistical analysis _ data processing (DataFrame Common operations )Java Misunderstanding —— Method overloading is a manifestation of polymorphism ?