A simple case
client:
package demo; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class TCPClient { public static void main(String[] args) throws IOException { Socket socket = new Socket("127.0.0.1", 7000); OutputStream out = socket.getOutputStream(); out.write("hello".getBytes()); InputStream in = socket.getInputStream(); byte[] data = new byte[1024]; int len = in.read(data); System.out.println(new String(data, 0, len)); socket.close(); } }
Server side:
package demo; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class TCPServer { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(7000); Socket socket = server.accept(); InputStream in = socket.getInputStream(); byte[] data = new byte[1024]; int len = in.read(data); System.out.println(new String(data, 0, len)); OutputStream out = socket.getOutputStream(); out.write("Received".getBytes()); socket.close(); server.close(); } }
Start the server first, then the client. The server Prints: hello, then the client Prints: received
Pay attention to distinguish the flow objects here
Next, on the basis of this case, make a case of image upload
The essence of image upload is file copy
client:
package demo1; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class TCPClient { public static void main(String[] args) throws IOException { Socket socket = new Socket("127.0.0.1", 7000); OutputStream out = socket.getOutputStream(); FileInputStream fis = new FileInputStream("d:\\666.jpeg"); int len = 0; byte[] bytes = new byte[1024]; while ((len = fis.read(bytes)) != -1) { out.write(bytes, 0, len); } socket.shutdownOutput(); // Server side cannot read-1,Do not terminate. Call this method to terminate InputStream in = socket.getInputStream(); len = in.read(bytes); System.out.println(new String(bytes, 0, len)); fis.close(); socket.close(); } }
Server side:
package demo1; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Random; public class TCPServer { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(7000); Socket socket = server.accept(); InputStream in = socket.getInputStream(); File upload = new File("d:\\upload"); if (!upload.exists()) { upload.mkdirs(); } String filename = System.currentTimeMillis() + new Random().nextInt(999999) + ".jpeg"; FileOutputStream fos = new FileOutputStream(upload + File.separator + filename); // Here is the problem of dealing with the duplicate names of pictures byte[] bytes = new byte[1024]; int len = 0; while ((len = in.read(bytes)) != -1) { fos.write(bytes, 0, len); } socket.getOutputStream().write("Upload succeeded".getBytes()); fos.close(); socket.close(); server.close(); } }
If multiple clients upload together, multi-threaded technology is needed, and each client takes up one thread
Client does not need to be modified
Server side:
package demo1; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class TCPServer { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(7000); while (true) { Socket socket = server.accept(); new Thread(new Upload(socket)).start(); } } }
package demo1; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Random; public class Upload implements Runnable { private Socket socket; public Upload(Socket socket) { this.socket = socket; } public void run() { try { InputStream in = socket.getInputStream(); File upload = new File("d:\\upload"); if (!upload.exists()) { upload.mkdirs(); } String filename = System.currentTimeMillis() + new Random().nextInt(999999) + ".jpeg"; FileOutputStream fos = new FileOutputStream(upload + File.separator + filename); // Here is the problem of dealing with the duplicate names of pictures byte[] bytes = new byte[1024]; int len = 0; while ((len = in.read(bytes)) != -1) { fos.write(bytes, 0, len); } socket.getOutputStream().write("Upload succeeded".getBytes()); fos.close(); socket.close(); } catch (IOException ex) { System.out.println(ex); } } }