Related Java classes
Socket
public class Socket extends Object
・Function: TCP client socket ・Construction method: Socket(InetAddress address, int port) Create a stream socket and connect it to the specified port number of the specified IP address ・Common methods: 1.getInetAddress Get relevant information about InetAddress 2.getInputStream Get the input stream of this TCP connection 3.getOutPutStream Get the output stream of this TCP connection
ServerSocket
public class ServerSocket extends Object
・Function: TCP server socket ・Construction method: ServerSocket(int port) Create a server socket bound to a specific port.・Common methods: 1.accept Get the socket of the client connected to TCP 2.isClosed Get the closed status of ServerSocket
TCP server side
TcpServer.java
The server adopts a multi-threading method. Each time a connection is established, a java thread is started, and an image is sent to the client, and then the TCP connection is closed.
package cn.xidian.tcpSocket;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class TcpServer extends Thread{Socket clientSocket;public TcpServer(Socket clientSocket) {super();this.clientSocket = clientSocket;}@Override public void run() {try {//Get the client's ip address and host name String clientAddress = clientSocket.getInetAddress().getHostAddress();String clientHostName = clientSocket.getInetAddress().getHostName();System.out.println(clientHostName + "(" + clientAddress + ")" + "Connected successfully!");System.out.println("Now, transfer image data.........");long startTime = System.currentTimeMillis();//Get OutputStreamOutputStream out = clientSocket.getOutputStream();//Transfer image data FileInputStream in = new FileInputStream(new File("/home/gavinzhou/test.jpg"));byte[] data = new byte[4096];int length = 0;while((length = in.read(data)) != -1){out.write(data, 0, length);//Write out data} long endTime = System.currentTimeMillis();//Prompt message System.out.println(clientHostName + "(" + clientAddress + ")" + "The image transmission is successful," + "Time:" + ((endTime-startTime)) + "ms");//Close the resource in.close();clientSocket.close();System.out.println("Connection close!");}catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) throws IOException {//Create a TCP connection service, bind the port ServerSocket tcpServer = new ServerSocket(9090);//Accept the connection and pass the picture to the connected client. Each TCP connection is a java thread while(true){Socket clientSocket = tcpServer.accept();new TcpServer(clientSocket).start();}}}TCP Client
TcpClient
package cn.xidian.tcpSocket;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.InputStream;import java.net.Socket;public class TcpClient {public static void main(String[] args) throws IOException {// Establish TCP service// Connect to the native TCP server Socket socket = new Socket(InetAddress.getLocalHost(), 9090);// Get input stream InputStream inputStream = socket.getInputStream();// Write data FileOutputStream out = new FileOutputStream(new File("../save.jpg"));byte[] data = new byte[4096];int length = 0;while((length = inputStream.read(data)) != -1){out.write(data, 0, length);}//Close the resource out.close();socket.close();}}result
First, start the server on the command line and then start the client. The result is as follows:
The picture is relatively small and very fast!
Summarize
The above is the complete content of this article about Java programming to implement a multi-threaded TCP server. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!