- //服务器端ServerSocket
- public class MyServer {
- // 定义保存所有Socket的ArrayList
- public static ArrayList<Socket> socketList = new ArrayList<Socket>();
- public static void main(String[] args) throws IOException {
- ServerSocket ss = new ServerSocket(30002);
- while (true) {
- // 此行代码会阻塞,将一直等待别人的连接
- Socket s = ss.accept();
- socketList.add(s);
- // 每当客户端连接后启动一条ServerThread线程为该客户端服务
- new Thread(new ServerThread(s)).start();
- }
- }
- }
- //服务器端监听客户端发送消息的线程
- // 负责处理每个线程通信的线程类
- public class ServerThread implements Runnable {
- // 定义当前线程所处理的Socket
- Socket s = null;
- // 该线程所处理的Socket所对应的输入流
- BufferedReader br = null;
- public ServerThread(Socket s) throws IOException {
- this.s = s;
- // 初始化该Socket对应的输入流
- br = new BufferedReader(new InputStreamReader(s.getInputStream(),
- "utf-8")); // ②
- }
- public void run() {
- try {
- String content = null;
- // 采用循环不断从Socket中读取客户端发送过来的数据
- while ((content = readFromClient()) != null) {
- // 遍历socketList中的每个Socket,
- // 将读到的内容向每个Socket发送一次
- for (Socket s : MyServer.socketList) {
- OutputStream os = s.getOutputStream();
- os.write((content + "\\n").getBytes("utf-8"));
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // 定义读取客户端数据的方法
- private String readFromClient() {
- try {
- return br.readLine();
- }
- // 如果捕捉到异常,表明该Socket对应的客户端已经关闭
- catch (IOException e) {
- // 删除该Socket。
- MyServer.socketList.remove(s); // ①
- }
- return null;
- }
- }
- //客户端UI代码
- public class MultiThreadClient extends Activity {
- // 定义界面上的两个文本框
- EditText input, show;
- // 定义界面上的一个按钮
- Button send;
- OutputStream os;
- Handler handler;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- input = (EditText) findViewById(R.id.input);
- send = (Button) findViewById(R.id.send);
- show = (EditText) findViewById(R.id.show);
- Socket s;
- handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- // 如果消息来自于子线程
- if (msg.what == 0x123) {
- // 将读取的内容追加显示在文本框中
- show.append("\\n" + msg.obj.toString());
- }
- }
- };
- try {
- s = new Socket("192.168.1.181", 30002);
- // 客户端启动ClientThread线程不断读取来自服务器的数据
- new Thread(new ClientThread(s, handler)).start(); // ①
- os = s.getOutputStream();
- } catch (Exception e) {
- e.printStackTrace();
- }
- send.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- try {
- // 将用户在文本框内输入的内容写入网络
- os.write((input.getText().toString() + "\\r\\n").getBytes("utf-8"));
- // 清空input文本框
- input.setText("");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
- //客户端监听服务器端发送消息的线程
- public class ClientThread implements Runnable {
- // 该线程负责处理的Socket
- private Socket s;
- private Handler handler;
- // 该线程所处理的Socket所对应的输入流
- BufferedReader br = null;
- public ClientThread(Socket s, Handler handler) throws IOException {
- this.s = s;
- this.handler = handler;
- br = new BufferedReader(new InputStreamReader(s.getInputStream()));
- }
- public void run() {
- try {
- String content = null;
- // 不断读取Socket输入流中的内容。
- while ((content = br.readLine()) != null) {
- // 每当读到来自服务器的数据之后,发送消息通知程序界面显示该数据
- Message msg = new Message();
- msg.what = 0x123;
- msg.obj = content;
- handler.sendMessage(msg);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0708201513385.html
来源: http://www.codesnippet.cn/detail/0708201513385.html