欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java Socket 使用多线程实现服务器与多个客户端通信

程序员文章站 2022-06-06 08:01:05
...

Client 类:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;


public class Client {
    private static PrintWriter pw=null;
    private static BufferedReader br=null;
    private static Socket s;
    static Scanner scanner=new Scanner(System.in);
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            Socket s=new Socket(InetAddress.getLocalHost(),8888);
            pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
            br=new BufferedReader(new InputStreamReader(s.getInputStream()));
            while(true){
                System.out.println("Client端请输入:");
                String str = scanner.next();
                pw.println(str);
                pw.flush();
                String string=br.readLine();
                System.out.println("Client读到:"+string);
                if(str.equals("exit")){
                    break;
                }
            } 
        }catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        try {
            br.close();
            pw.close();
        } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
        }
    }
}

Server类:

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


public class Server {
//    private static BufferedReader br=null;
//    private static PrintWriter pw=null;
    private static ServerSocket ss;
    private static Socket s;
    static Scanner scanner=new Scanner(System.in);
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
        	int count=0;
        	ss=new ServerSocket(8888);
            System.out.println("服务器正常启动。。。。");
//            br=new BufferedReader(new InputStreamReader(s.getInputStream()));
//            pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
            while(true){
//                String string=br.readLine();
//                System.out.println("Server读到:"+string);
//                System.out.println("Server端请输入:");
//                String str=scanner.next();
//                pw.println(str);
//                pw.flush();
//                if(str.equals("exit")){
//                    break;
//                }
            	s=ss.accept();//阻塞方法
            	ServerThread sst = new ServerThread(s);
            	sst.start();
            	count++;
            	System.out.println("客户端的数量: " + count); 
            	System.out.println("连接成功"+s.getRemoteSocketAddress());
            }
        } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
        }
//        try {
//            pw.close();
//            br.close();
//        } catch (IOException e1) {
//                // TODO Auto-generated catch block
//            e1.printStackTrace();
//        }
    }
}





ServerThread类:

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


public class ServerThread extends Thread{
	static Scanner scanner=new Scanner(System.in);
	  Socket s = null;
	  BufferedReader br = null; 
	  PrintWriter pw = null;
	  public ServerThread(Socket s){ 
	    this.s = s; 
	  }
	
	public void run(){
		
		try{
			//String string=br.readLine();
			br=new BufferedReader(new InputStreamReader(s.getInputStream()));
            pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
			String string = null;
			while (true)
			{ string  = br.readLine();
				System.out.println("Server读到:"+string);
             System.out.println("Server端请输入:");
             String str=scanner.next();
             pw.println(str);
             pw.flush();
             if(str.equals("exit")){
                 break;
             }}
		}
		catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
		}
		try {
            pw.close();
            br.close();
        } catch (IOException e1) {
                // TODO Auto-generated catch block
            e1.printStackTrace();
        }
	}
}