java多客户端通信主要使用Socket编程,理论上是通过服务器广播的方式进行通信。
做个例子来实现这个理论,打开服务端后,登录多个客户端,一个客户端发出消息,对所有客户端进行广播。
服务器的主内,主要用于接受多个客户端连接,以及客户端数据
服务器端:
1 package services;
2
3 import java.io.IOException;
4 import java.net.ServerSocket;
5 import java.net.Socket;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 public class Server
10 {
11
12 /**
13 * Author:至尊龙
14 */
15 public static int PORT = 90;
16 public static String HOST = "127.0.0.1";
17 public static List msg=new ArrayList();
18 public static void main(String[] args)
19 {
20 ServerSocket serverSocket = null;
21 Socket socket = null;
22 try
23 {
24 serverSocket = new ServerSocket(PORT);
25 // 等待请求,无请求一直等待
26 while (true)
27 {
28 System.out.println("等待连接");
29 socket = serverSocket.accept();// 接受请求
30 System.out.println("有连接接接入");
31 new ThreadedServer(socket);
32 }
33 }
34 catch (Exception e)
35 {
36 try
37 {
38 socket.close();
39 }
40 catch (IOException e1)
41 {
42 e1.printStackTrace();
43 }
44 }
45 finally
46 {
47 try
48 {
49 serverSocket.close();
50 }
51 catch (IOException e)
52 {
53 e.printStackTrace();
54 }
55 }
56
57 }
58
59 }
服务器端辅助线程
1 package services;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.OutputStreamWriter;
8 import java.io.PrintWriter;
9 import java.net.Socket;
10
11 public class ThreadedServer extends Thread
12 {
13
14 private Socket socket = null;
15 private BufferedReader br = null;
16
17 PrintWriter pw = null;
18
19 public ThreadedServer(Socket s)
20 {
21 socket = s;
22 try
23 {
24 br = new BufferedReader(new InputStreamReader(
25 socket.getInputStream()));
26 pw = new PrintWriter(
27 new BufferedWriter(new OutputStreamWriter(
28 socket.getOutputStream())), true);
29 start();
30 }
31 catch (Exception e)
32 {
33
34 e.printStackTrace();
35 }
36 }
37
38 public void run()
39 {
40 new ReadClientMessage(br, socket);//读取客户端数据
41 while (true)//向客户端发送数据
42 {
43 try
44 {
45 if(Server.msg.size()>0){
46 for(int i=0;i<Server.msg.size();i++){
47 pw.println(Server.msg.get(i));
48 pw.flush();
49 Server.msg.remove(i);
50 }
51 }
52 }
53 catch (Exception e)
54 {
55 //这段异常需要根据情况处理..
56 // try
57 // {
58 // br.close();
59 // pw.close();
60 //// socket.close();
61 // }
62 // catch (IOException e1)
63 // {
64 // e1.printStackTrace();
65 // }
66 }
67 }
68 }
69
70 }
71
72 class ReadClientMessage extends Thread
73 {
74 BufferedReader bReader;
75 Socket socket;
76
77 public ReadClientMessage(BufferedReader br,Socket s)
78 {
79 this.bReader = br;
80 this.socket=s;
81 start();
82 }
83
84 public void run()
85 {
86 String str = "";
87 while (true)
88 {
89 try
90 {
91 if(bReader!=null){
92 str = bReader.readLine();
93 if (str.equals("q"))
94 {
95 bReader.close();
96 socket.close();
97 break;
98 }
99 }
100 }
101 catch (IOException e)
102 {
103 e.printStackTrace();
104 }
105 if(str!=null){
106 Server.msg.add(str);
107 str=null;
108 }
109 }
110 }
111 }
客户端:
1 package client;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.OutputStreamWriter;
8 import java.io.PrintWriter;
9 import java.net.Socket;
10 import java.util.Scanner;
11
12 public class Client
13 {
14
15 /**
16 * Author: 至尊龙
17 * 客户端
18 */
19 public static void main(String[] args)
20 {
21 Socket socket = null;
22 BufferedReader br = null;
23 PrintWriter pw = null;
24 Scanner scanner = new Scanner(System.in);// 从键盘读取
25 try
26 {
27 // 创建客户端socket
28 socket = new Socket("127.0.0.1", 90);
29 // 读取从客户端发来的消息
30 br = new BufferedReader(new InputStreamReader(
31 socket.getInputStream()));
32 // 写入信息到服务器端
33 pw = new PrintWriter(
34 new BufferedWriter(new OutputStreamWriter(
35 socket.getOutputStream())));
36 new ReadServerMessage(br);// 从服务器读取消息
37 while (true)
38 {
39 String temp = scanner.nextLine();// 从键盘读取一行
40 pw.println(temp);// 写到服务器
41 pw.flush();
42 if (temp.equals("q"))
43 break;
44 }
45 }
46 catch (Exception e)
47 {
48 e.printStackTrace();
49 }
50 finally
51 {
52 try
53 {
54 System.out.println("关闭......");
55 br.close();
56 pw.close();
57 socket.close();
58 }
59 catch (IOException e)
60 {
61 e.printStackTrace();
62 }
63 }
64
65 }
66
67 }
68
客户端辅助线程:
1 package client;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.net.Socket;
6
7 public class ReadServerMessage extends Thread
8 {
9 BufferedReader bReader;
10 Socket socket;
11 public ReadServerMessage(BufferedReader br)
12 {
13 this.bReader = br;
14 start();
15 }
16
17 public void run()
18 {
19 String str = "";
20 while (true)//一直等待着服务器的消息
21 {
22 try
23 {
24 if(bReader!=null){
25 str = bReader.readLine();
26 if (str.equals("q"))
27 {
28 bReader.close();
29 socket.close();
30 break;
31 }
32 }
33 }
34 catch (IOException e)
35 {
36 e.printStackTrace();
37 }
38 System.out.println("Server Message:" + str);
39 }
40 }
41 }
只是简单的实现,如果有更好的方法,欢迎补充。