Java TCP uses Socket for network image transfer (6), tcpsocket
Author: Qing Dujun
Address: http://blog.csdn.net/qingdujun/article/details/39338831
This article demonstrates how to use socketto transfer online images and upload the Client client's client.bmp file to the server.
1. Upload the client.bmp image to the server and receive the message "uploaded successfully" returned by the server.
2. on the service end, save the image from the client as server.bmp and send the "uploaded successfully" message to the client.
The client code is as follows:
Package upload. photo. client; import java. io. fileInputStream; import java. io. inputStream; import java. io. outputStream; import java.net. socket; // client: Upload the image public class CUploadPhotoClient {public static void main (String [] args) throws Exception {// 1. connect to the server Socket s = new Socket ("127.0.0.1", 5612); System. out. println ("You have connected to port 5612 of the server to transfer images... "); // get the image byte stream FileInputStream FCM = new FileInputStream (" client.bmp "); // get Output stream OutputStream out = s. getOutputStream (); byte [] buf = new byte [1024]; int len = 0; // 2. serving data in the output stream while (len = Fi. read (buf ))! =-1) {out. write (buf, 0, len);} // notifies the server that data has been sent. shutdownOutput (); // 3. obtain the output stream and accept the message "uploaded successfully" InputStream in = s. getInputStream (); byte [] bufIn = new byte [1024]; int num = in. read (bufIn); System. out. println (new String (bufIn, 0, num); // disable Resource (s. close (); out. close (); in. close (); s. close ();}}
Server, the Code is as follows:
Package upload. photo. server; import java. io. fileOutputStream; import java. io. inputStream; import java. io. outputStream; import java.net. serverSocket; import java.net. socket; public class CUploadPhotoServer {public static void main (String [] args) throws Exception {// 1. the server starts listening to port 5612 ServerSocket ss = new ServerSocket (5612); System. out. println ("the server is started and listening for port 5612... "); // wait for the client Socket s = ss. accept (); System. out. pri Ntln ("detects the client and prepares data for receiving... "); // The client is connected to obtain the InputStream in = s. getInputStream (); // create an image byte stream FileOutputStream fos = new FileOutputStream ("server.bmp"); byte [] buf = new byte [1024]; int len = 0; // write the image data into the byte stream while (len = in. read (buf ))! =-1) {fos. write (buf, 0, len);} // get the output stream and prepare to send the message OutputStream out = s to the client. getOutputStream (); out. write ("Upload successful ". getBytes (); // closes the resource fos. close (); in. close (); out. close (); s. close (); ss. close ();}}
For more information, see my previous article: using Socket for Network File Transfer in Java TCP (5)
Running Effect display:
1) Start the server
2) Start the client
3) server Display Results
4) Client image storage location
5) The server stores images
References: Lecture on Java video Bi Xiangdong
Address: http://blog.csdn.net/qingdujun/article/details/39338831
How does java socket server send image streams?
Tcp or UDP transmission is used. As mentioned above, the bytes transmitted by socket communication are both outputStream, but they are accepted differently. You only need to use inputStream to write files.
If it is not complete, part of the data can be displayed theoretically. Check the browser's image preview and lazy load will know. Do six frames represent six images? If yes, you can view the network transmission speed and file size.
Java Course Design: use TCP Socket to transmit arbitrary types of files, sizes, and add graphical user interfaces
Must this be of any type and size? Java seems to have a limit on the size of only transmitted files.