JAVA基础篇—模拟服务器与客户端通信

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA基础篇—模拟服务器与客户端通信相关的知识,希望对你有一定的参考价值。

第一种:

客户端class Client

package 服务器发送到客户端;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {
     public static void main(String[] args) {
    	 try {
				Socket socket=new Socket("127.0.0.1", 6666);
				System.out.println("??????????????");
					   
 		        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 		        PrintWriter writer=new PrintWriter(socket.getOutputStream());
				BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
				String readline;
				readline=br.readLine();
				
				while (!readline.equals("end")) {
					writer.println(readline);
					writer.flush();
					System.out.println("????????"+readline);
					System.out.println("????????"+in.readLine());
					readline=br.readLine();
				}
				writer.close();
				in.close();
				socket.close();		
    	 } catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println("????????"+e);
			}
     }
}
	

  服务器端class Server

package 服务器发送到客户端;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    	public static void main(String[] args) throws IOException{
    		Server server=new Server();
    		server.onServer();
    		
		}
    	public void onServer(){
    		ServerSocket server=null;
      	try {
    		try {
				server=new ServerSocket(6666);
				System.out.println("????????????");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println("????????"+e);
			}
    		Socket socket=null;
    		try {
				socket=server.accept();
				System.out.println("????????????");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("Error."+e);
			}
    		String line;
  
				BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
			
				PrintWriter writer=new PrintWriter(socket.getOutputStream());
				BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
				System.out.println("??????"+in.readLine());
				line=br.readLine();
				while (!line.equals("end")) {
					writer.println(line);
					writer.flush();
					System.out.println("?????????"+line);
					System.out.println("??????"+in.readLine());
					line=br.readLine();
				}
				writer.close();
				in.close();
				server.close();
				socket.close();				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		
    		
    	}
}

  第二种:

客户端class TCPClient

package 服务器发送到客户端;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPClient {
     public static void main(String[] args) throws UnknownHostException,IOException{
    	 try {
				Socket socket=new Socket("127.0.0.1", 6666);
				System.out.println("??????????????");
					   
		        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		        PrintWriter writer=new PrintWriter(socket.getOutputStream());//?????
		        String readline;
				readline=br.readLine();
				writer.println(readline);
				writer.flush();
				System.out.println("????????"+readline);
				
				BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));//?????????													
					System.out.println("????????"+in.readLine());
				
				writer.close();
				in.close();
				socket.close();		
 	 } catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println("????????"+e);
			}
	}
}

  服务器端class TCPServer

package 服务器发送到客户端;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class TCPServer {
    public static void main(String[] args) throws IOException{
		ServerSocket server=null;
  	try {
		try {
			server=new ServerSocket(6666);
			System.out.println("????????????");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("????????"+e);
		}
		Socket socket=null;
		try {
			socket=server.accept();
			System.out.println("????????????");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Error."+e);
		}
		String line;
			BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));//?????????
			line=in.readLine();
			System.out.println("??????"+line);
			
			
			ArrayList<Integer> list = new ArrayList<Integer>();
			Scanner scanner=new Scanner(line);
			while (scanner.hasNext()) {
				list.add(scanner.nextInt());
			}
			// ?????????????????????,?????????????????鷵???
			
				
			Collections.sort(list);
			
			PrintWriter writer=new PrintWriter(socket.getOutputStream());//?????						
				writer.println(list);
				writer.flush();
				System.out.println("?????????"+list);
				
			
			writer.close();
			in.close();
			server.close();
			socket.close();		
			scanner.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
	}
}

  

以上是关于JAVA基础篇—模拟服务器与客户端通信的主要内容,如果未能解决你的问题,请参考以下文章

Fiddler的安装与使用(基础篇)

Java Socket编程基础篇

Web实时通信

Java基础加强之多线程篇(线程创建与终止互斥通信本地变量)

JAVA服务器与客户端的socket通信问题

grpc教程横向比较与grpc通信模式