“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”
A lot of people in college don’t know the difference between a client and a server. Simply put, there is no networking function of the program, are playing stand-alone. So how does an application get online? Let’s talk about socket connections.
Know the socket
Socket is an intermediate software abstraction layer for communication between application layer and TCP/IP protocol family. It is a group of interfaces. In design mode, Socket is actually a facade mode, it hides the complex TCP/IP protocol family behind the Socket interface, to the user, a simple set of interfaces is all, let the Socket to organize data to conform to the specified protocol.
Here is the schematic.
The service side
/** * The client and the server (one to one) chat function, the client first initiates the chat, the input content is displayed in the server and client, * then the server can also input information, Public class server {//ChatServer private int port = 8081; Public server(int port) {// Constructor this.port = port; Public void service() {// Create a service method try {// Set up a server connection ServerSocket Server = new ServerSocket(port); // Create ServerSocket class Socket = server.accept(); Try {DataInputStream in = new DataInputStream(socket.getinputStream ()); DataInputStream DataOutputStream out = new DataOutputStream(socket.getOutputStream ()); // DataOutputStream to send information to the client Scanner Scanner = new Scanner(system.in); // Receive data from the keyboard while (true) {... }} finally {// Do not execute socket.close(); socket.close(); Server.close (); }} catch (IOException e) {// catch the exception e.printStackTrace(); }} public static void main(String[] args) {// New server().service(); // Call the service method}}Copy the code
The client
Public class client {private String host = "localhost"; Private int port = 8081; Public client(String host, int port) {// Constructor this.host = host; // Default connection to port 8189 public client() {} public client(String host, int port) {// constructor this. // Pass the constructor parameter host to the class variable host this.port = port; Public void chat() {//chat method try {// connect to server Socket Socket = new Socket(host, port); Try {DataInputStream in = new DataInputStream(socket.getinputStream ()); DataInputStream DataOutputStream out = new DataOutputStream(socket.getOutputStream ()); // DataOutputStream to send information to the server Scanner Scanner = new Scanner(system.in); // Decorate the standard input stream for input from the console while (true) {... }} finally {socket.close(); }} catch (IOException e) {// catch the exception e.printStackTrace(); }} public static void main(String[] args) {// New client().chat(); // Call the chat method}}Copy the code
Finally, the code runs directly to get the desired communication. Note that the server needs to be started first, otherwise the client will fail to connect.
We only need to cooperate with the above flowchart, plus the specific source code can achieve client and server communication. There is a need to take the complete source code, please move to the public number: like the code poem. Now that you’re in, you can click “like” before you leave.