Java TCP uses sockets for Network File Transfer (5)

Source: Internet
Author: User

Author: Ching

Original address: http://blog.csdn.net/qingdujun/article/details/39324737


This article demonstrates that TCP uses a socket for network File transfer, which allows the client to upload file Upload.txt to the server.

1) The client, upload the file upload.txt, and accept the server feedback upload success or not message.

2) The server, accepts the client file, names the received file as Rece.txt, and sends the message "upload success" to the client.


Client, the code is as follows:

Package Upload.file.client;import Java.io.bufferedreader;import Java.io.filereader;import Java.io.inputstreamreader;import java.io.printwriter;import java.net.socket;//Client: Upload file public class cuploadfileclient {public static void main (string[] args) throws Exception {socket s = new socket ("127.0.0.1", 8686); BufferedReader bufr = new BufferedReader (New FileReader ("Upload.txt")); PrintWriter out = new PrintWriter (S.getoutputstream (), true); String line = null;//reads the data while (lines = Bufr.readline ()) = null) {//reads a row, sends a row out.println (line);} Close the client output stream, which is equivalent to adding the end tag s.shutdownoutput () to the stream,//receiving the service-side message BufferedReader Bufin = new BufferedReader (New InputStreamReader ( S.getinputstream ())); String str = bufin.readline ();//print server sends over message SYSTEM.OUT.PRINTLN (str);//Close Resource bufr.close (); Bufin.close (); S.close ();}}
service side, the code is as follows:

package upload.file.server;import java.io.bufferedreader;import java.io.FileWriter; Import Java.io.inputstreamreader;import java.io.printwriter;import Java.net.serversocket;import java.net.Socket;// Server: Accept file public class Cuploadfileserver {public static void main (string[] args) throws exception{//listening 8686 Port ServerSocket SS = new ServerSocket (8686); SYSTEM.OUT.PRINTLN ("Server started, listening on port 8686 ...");//Enter message waiting to be accepted ... Socket s = ss.accept ();//Display client IP address string ip = s.getinetaddress (). gethostaddress (); System.out.println (ip+ "Connected");//here is getInputStream stream BufferedReader Bufin = new BufferedReader (New InputStreamReader ( S.getinputstream ()));//here is filewriterprintwriter out = new PrintWriter (New FileWriter ("Rece.txt"), true); String line = null;//reads the data, writes a row to the file while (lines = Bufin.readline ()) = null) {OUT.PRINTLN (lines);} Get the output stream, send a message to the client printwriter pw = new PrintWriter (S.getoutputstream (), True);p w.println ("Upload succeeded");//Close Resource Bufin.close ( ); Out.close ();p w.close (); S.close (); Ss.close ();}} 
Here are a few things to note:

1) BufferedReader is to read the data to the buffered stream, if not println (), the buffer stream flush () needs to be refreshed.

2) ReadLine () reads end with a newline, which can easily cause the server client to go into a blocking state if the end is not processed.


Run effect display:

1) Start the service side


2) Start the client, upload the file


3) server side and Client link success



In addition, the file location that needs to be uploaded and received is displayed as follows:

1) before uploading


2) Upload success



References: Java video Bi Xiangdong presenter

Original address: http://blog.csdn.net/qingdujun/article/details/39324737


Java TCP uses sockets for Network File Transfer (5)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.