Client information is read through the input stream, and correspondingly, it is implemented through the output stream.
Server class code:
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.util.logging.Level;import java.util.logging.Logger;/** * * @author whn6325689 */public class server {//server-side public static void main(String[] args) {try {//1. Create a server-side Socket, that is, serverSocket, specify the bound port, and listen for this port. ServerSocket serverSocket=new ServerSocket(8888);//2. Call serverSocket's accept() method to wait for the client to connect System.out.println("==The server is about to start, waiting for the client to connect ==");Socket socket=serverSocket.accept();//3. Get the input stream to read the login information sent by the client InputStream is=socket.getInputStream();//Byte input stream InputStreamReader isr=new InputStreamReader(is);//Convert byte stream into character stream//Add bufferedReader for character stream bufferedReader=new BufferedReader(isr);String info=null;//Loop to read the information submitted by the client while((info=bufferedReader.readLine())!=null){System.out.println("I am a server, and the information submitted by the client is: "+info);}socket.shutdownInput();//4. Get the output stream and respond to the client's request OutputStream os= socket.getOutputStream();PrintWriter pw=new PrintWriter(os);//Convert to print stream pw.write("Welcome!");pw.flush();//Fresh cache//5. Close related resources bufferedReader.close();is.close();isr.close();socket.close();serverSocket.close();os.close();pw.close();}catch (IOException ex) {Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);}}}Client class code:
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.Socket;import java.util.logging.Level;import java.util.logging.Logger;/** * * @author whn6325689 */public class client {//client public static void main(String[] args) {try {//1. Create a client Socket, specify the server address and port number. Socket socket=new Socket("127.0.0.1", 8888);//2. Get the output stream to send information to the server OutputStream os=socket.getOutputStream();//Byte output stream//Convert to print stream PrintWriter pw=new PrintWriter(os);pw.write("Username: admin; Password: admin");pw.flush();//Fresh the cache and output information to the server//Close the output stream socket.shutdownOutput();//3. Get the input stream to read the response information on the server InputStream is=socket.getInputStream();BufferedReader br=new BufferedReader(new InputStreamReader(is));String info=null;while((info=br.readLine())!=null){System.out.println("I am the client, and the information returned by the server is: "+info);}//4. Close the resource br.close();is.close();pw.close();os.close();socket.close();}catch (IOException ex) {Logger.getLogger(client.class.getName()).log(Level.SEVERE, null, ex);}}}Run the server-side class first, and then run the client class:
Server-side class output result:
==The server is about to start, waiting for the client to connect == I am the server, the information submitted by the client is: Username: admin; Password: admin
The output result of the client class:
I am the client, and the information returned by the server is: Welcome!
Summarize
The above is the entire content of this article about the Java Socket Programming Server Response Client Instance Code, I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Java programming Socket implements multiple clients to connect to the same server code
Sample code for implementing socket communication in Java multithreaded programming
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!