Java TCP uses Socket for Network File Transfer (5)
In this demonstration, tcpuses socketto transfer network files, and upload the client file upload.txt to the server.
1. Upload the file upload.txt to the client and receive the server's feedback about the upload success or failure.
2. The server accepts the client file, and the received file is named rece.txt, and the message "uploaded successfully" is sent to the client ".
The client 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 the 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 PrintWr Iter (s. getOutputStream (), true); String line = null; // read data while (line = bufr. readLine ())! = Null) {// read a row and send a row out. println (line);} // close the client output stream, equivalent to adding end Mark s to the stream. shutdownOutput (); // receives the message BufferedReader bufIn = new BufferedReader (new InputStreamReader (s. getInputStream (); String str = bufIn. readLine (); // print the message System sent by the server. out. println (str); // closes the resource bufr. close (); bufIn. close (); s. close ();}}
Server, 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 the public class CUploadFileServer {public static void main (String [] args) throws Exception {// listen to port 8686 ServerSocket ss = new ServerSocket (8686 ); system. out. println (the server is started and listening to port 8686 ...); // enter the message waiting for acceptance... so Cket s = ss. accept (); // display Client ip address String ip = s. getInetAddress (). getHostAddress (); System. out. println (ip + connected); // here is the getInputStream stream BufferedReader bufIn = new BufferedReader (new InputStreamReader (s. getInputStream (); // here is FileWriterPrintWriter out = new PrintWriter (new FileWriter(Rece.txt), true); String line = null; // read data wildly, write a row in the file while (line = bufIn. readLine ())! = Null) {out. println (line);} // gets the output stream and sends a message PrintWriter pw = new PrintWriter (s. getOutputStream (), true); pw. println (uploaded successfully); // closes the resource bufIn. close (); out. close (); pw. close (); s. close (); ss. close ();}}
Note the following points:
1) BufferedReader reads data to the buffer stream. If println () is not used, refresh the buffer stream flush ().
2) readLine () reads end with a line break. If the end is not processed, the server client may be blocked.
Running Effect display:
1) Start the server
2) Start the client and upload files
3) the connection between the server and the client is successful.
In addition, the location of the file to be uploaded and received is shown as follows:
1) Before upload
2) Upload successful
References: Lecture on Java video Bi Xiangdong
Address: http://blog.csdn.net/qingdujun/article/details/39324737