一个Java编写的小玩意儿---多人在线聊天工具

这个在线聊天工具小项目使用JAVA编写,用JAVA来做图形界面本来就是出了名的低效和丑陋。不过这不是重点。写这个小项目的目的在于串一串J2SE的知识,把当时写这个项目的时候的思路梳理一下。时间有点久了,不过,拿出来再遛一遍算是个总结吧。

1·搭建客户端#

在线聊天工具首先得有一个Client端,作为用户的交互界面。所以可以先搭建一个客户端的框架。把图形界面搭起来。
先做一个客户端类Client.java

  public class ChatClient {

   public static void main(String[] args) {
	   new ClientFrame.launchFrame();
   }
  }

2·构建一个用户界面#

新建一个ClientFrame类用来搭建图形界面的框架。建一个内部私有类供ChatClient调用,并不利于扩展。而且我估计这个项目完成后,再升级的话可能需要把框架拿出来用。当然这是后话,如果真要用的话,我自己也会对代码重构,做一个自己的ClientFrame借口,然后让新做的类来实现,再用多态来搞定。这里先不管这些细节,先把主题的东西搭出来吧。省得最后烂尾了。ClientFrame.java
import java.awt.*;

public class ClientFrame extends Frame {

   TextField tfTxt = new TextField();

   TextArea taContent = new TextArea();

   public void launchFrame() {
	   this.setLocation(400, 300);//设置界面显示的位置
	   this.setSize(400, 400);//设置界面的大小
	   this.add(tfTxt, BorderLayout.SOUTH);//把文本输入框加到边界布局的下部
	   this.add(taContent, BorderLayout.NORTH);//把文本显示框加到布局的上部
	   this.pack();//让窗口适应布局组件的大小,形成包裹
	   this.setVisible(true);//显示界面
   }

}

3·添加一个窗口监听#

框架搭好以后再此基础上进行优化。给窗口加上监听器,让窗口能后实现关闭。加监听器可以有三种方法:1.构建一个外部监听器类,实现监听操作。这种方法适合需要加很多的监听器对象的时候。这里用并排的外部监听器类就不太合适。我们这里的监听器只是供我们这个类来使用。2.内部类3.匿名类这里使用内部类匿名类都OK。先姑且使用匿名类吧。后续估计也不会再打开这个地方的代码了。ClientFrame.java
import java.awt.*;

public class ClientFrame extends Frame {

   TextField tfTxt = new TextField();

   TextArea taContent = new TextArea();

   public void launchFrame() {
	   this.setLocation(400, 300);//设置界面显示的位置
	   this.setSize(400, 400);//设置界面的大小
	   this.add(tfTxt, BorderLayout.SOUTH);//把文本输入框加到边界布局的下部
	   this.add(taContent, BorderLayout.NORTH);//把文本显示框加到布局的上部
	   this.pack();//让窗口适应布局组件的大小,形成包裹
	   this.setVisible(true);//显示界面
               this.addWindowListener(new WindowAdapter() {

		@Override
		public void windowClosing(WindowEvent arg0) {
			System.exit(0);
		}
		
	});
   }

}

4·进一步实现界面的文本处理#

实现文本输入后回车发送到文本框中,回车响应还是要加一个监听器。TextField自带有监听器addActionListener(ActionListener l)
Adds the specified action listener to receive action events from this text field.
这里同样遇到上面添加监听器的时候那个问题,对监听器的加入方式有一个选择。这里用匿名类不好,后面继续拓展功能的时候会让代码看起来很乱。耦合性太强了。不利于后续开发。这里用外部类也不好,这个TextField的监听器只为他自己服务,没必要暴露给别的类。不符合OCP。采用内部类比较好,既解耦了代码,让类的功能单一了,符合SRP。又不会让专一为ClientFrame类服务的监听器不会暴露给外部,符合OCP。是一种很不错的选择。这里就选用内部类解决这个问题。
ClientFrame.java

import java.awt.*;
import java.awt.event.*;

public class ClientFrame extends Frame {

    TextField tfTxt = new TextField();

    TextArea taContent = new TextArea();

    public void launchFrame() {
	    setLocation(400, 300);
	    this.setSize(300, 300);
	    add(tfTxt, BorderLayout.SOUTH);
	    add(taContent, BorderLayout.NORTH);
	    pack();
	    this.addWindowListener(new WindowAdapter() {

		    @Override
		    public void windowClosing(WindowEvent arg0) {
			    System.exit(0);
		    }    
		
	    });
	    tfTxt.addActionListener(new TFListener());
 	    setVisible(true);
    }

    private class TFListener implements ActionListener {

	    public void actionPerformed(ActionEvent e) {
	  	    String s = tfTxt.getText().toString();
		    taContent.setText(s);
		    tfTxt.setText("");
	    }
	
    }

 }

5·搭建服务器#

搭建一个Server,用来接收Client的连接。
Server.java

import java.io.IOException;
import java.net.*;

public class Server {

    public static void main(String[] args) {
	    try {
		    ServerSocket ss = new ServerSocket(8080);//首先起一个server socket 帮到制定的端口8080
		    while(true) {
			    Socket s = ss.accept();//Listens for a connection to be made to this socket and accepts it.(阻塞式的)
                                System.out.println("connected");
		    }
	    } catch (IOException e) {
		    e.printStackTrace();
	    }
    }
}

6·让Client连入Server#

ClientFrame.java
import java.awt.*;

public class ClientFrame extends Frame {

   TextField tfTxt = new TextField();

   TextArea taContent = new TextArea();

   public void launchFrame() {
	   this.setLocation(400, 300);//设置界面显示的位置
	   this.setSize(400, 400);//设置界面的大小
	   this.add(tfTxt, BorderLayout.SOUTH);//把文本输入框加到边界布局的下部
	   this.add(taContent, BorderLayout.NORTH);//把文本显示框加到布局的上部
	   this.pack();//让窗口适应布局组件的大小,形成包裹
	   this.setVisible(true);//显示界面
               this.addWindowListener(new WindowAdapter() {

		@Override
		public void windowClosing(WindowEvent arg0) {
			System.exit(0);
		}
		
	});
   }

}

4·进一步实现界面的文本处理发送到Server端#

实现文本输入后回车发送到文本框中,回车响应还是要加一个监听器。TextField自带有监听器addActionListener(ActionListener l)
Adds the specified action listener to receive action events from this text field.
这里同样遇到上面添加监听器的时候那个问题,对监听器的加入方式有一个选择。这里用匿名类不好,后面继续拓展功能的时候会让代码看起来很乱。耦合性太强了。不利于后续开发。这里用外部类也不好,这个TextField的监听器只为他自己服务,没必要暴露给别的类。不符合OCP。采用内部类比较好,既解耦了代码,让类的功能单一了,符合SRP。又不会让专一为ClientFrame类服务的监听器不会暴露给外部,符合OCP。是一种很不错的选择。这里就选用内部类解决这个问题。
ClientFrame.java

	import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class Client extends Frame {
	Socket s = null;
	DataOutputStream dos = null;

	TextField tfTxt = new TextField();

	TextArea taContent = new TextArea();

	public void launchFrame() {
		setLocation(400, 300);
		this.setSize(300, 300);
		add(tfTxt, BorderLayout.SOUTH);
		add(taContent, BorderLayout.NORTH);
		pack();
		this.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent arg0) {
				disconnect();
				System.exit(0);
			}
			
		});
		tfTxt.addActionListener(new TFListener());
		setVisible(true);
		connect();
	}
	
	public void connect() {
		try {
			s = new Socket("192.168.0.1", 8080);
			dos = new DataOutputStream(s.getOutputStream());
                        System.out.println("connected!");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void disconnect() {
		try {
			dos.close();
			s.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	private class TFListener implements ActionListener {
                    //发送数据时调用的是actionPerformed
		public void actionPerformed(ActionEvent e) {
			String str = tfTxt.getText().trim();
			taContent.setText(str);
			tfTxt.setText("");
			
			try {
				dos.writeUTF(str);
				dos.flush();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
		}
		
	}

}

Server.java

   	import java.io.*;
import java.net.*;

public class Server {

	public static void main(String[] args) {
		boolean started = false;
		ServerSocket ss = null;
		Socket s = null;
		DataInputStream dis = null;
		try {
			ss = new ServerSocket(8080);
		} catch (BindException e) {
			System.out.println("服务器已启动");
			System.exit(0);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			started = true;
			while(started) {
				boolean bConnected = false;
				s = ss.accept();
                                System.out.println("a client connected!");
				bConnected = true;
				dis = new DataInputStream(s.getInputStream());
				while(bConnected) {
					String str = dis.readUTF();
					System.out.println(str);
				}
				
			}
		} catch (EOFException e) {
			System.out.println("Client closed!");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(dis != null) dis.close();
				if(s != null) s.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

}

5·实现多个Client端连接Server端,将信息发送到Server端#

之前的版本只能实现一个客户端连接上Server端。如果起多个Client,就需要进行调试。否则报错。s.accept()与readUTF()都是阻塞式的,server端main方法在执行的过程中,会在

                                   while(bConnected) {
					String str = dis.readUTF();
					System.out.println(str);
				}

语句里一直处于循环的状态。他再也没有机会去接受另外的socket,也就是说s = ss.accept();永远也不会得到第二次执行。所以导致不会就收第二个Client接入。 这里有两种解决办法。1、异步 意思就是,你可以在循环里等着接受信息,当有一个客户端要连上来的时候,马上调这个链接让Client连上,处理让其他的地方去处理 2、多线程
这里使用线程做。用一个线程控制一个客户端。主线程不干别的,专门负责客户端的链接。只要有Client连上来,就交给一个单独的线程,去处理Client 与Server的通讯过程。

建立一个内部的线程类。用它处理Client与Server的通讯。
Server.java

import java.io.*;
import java.net.*;

public class Server {
	boolean started = false;
	ServerSocket ss = null;
	
	public static void main(String[] args) {
		new Server().start();
	}
	
	public void start() {
		try {
			ss = new ServerSocket(8080);
			started = true;
		} catch (BindException e) {
			System.out.println("端口使用中....");
			System.exit(0);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			
			while(started) {
				Socket s = ss.accept();
				Client c = new Client(s);
				new Thread(c).start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				ss.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private class Client implements Runnable {
		private Socket s;
		private DataInputStream dis = null;
		private boolean bConnected = false;
		
		public Client(Socket s) {
			this.s = s;
			try {
				dis = new DataInputStream(s.getInputStream());
				bConnected = true;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		public void run() {
			try {
				while(bConnected) {
					String str = dis.readUTF();
					System.out.println(str);
				}
			} catch (EOFException e) {
				System.out.println("Client closed!");
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if(dis != null) dis.close();
					if(s != null) s.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		
	}
}

6·将Server端从Client接收到的数据分发给各个Client#

在Server接收到Client的数据以后,第一步要做的就是保存下来Client的数据,保存下来以后,其他的Client才能访问。用集合把Client的链接存储起来。供其他的客户端访问。
Server.java

import java.io.*;
import java.net.*;
import java.util.*;

public class Server {
	boolean started = false;
	ServerSocket ss = null;
	
	List<Client> clients = new ArrayList<Client>();
	
	public static void main(String[] args) {
		new Server().start();
	}
	
	public void start() {
		try {
			ss = new ServerSocket(8888);
			started = true;
		} catch (BindException e) {
			System.out.println("端口使用中....");
			System.out.println("请关掉相关程序并重新运行服务器!");
			System.exit(0);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			
			while(started) {
				Socket s = ss.accept();
				Client c = new Client(s);
System.out.println("a client connected!");
				new Thread(c).start();
				clients.add(c);
				//dis.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				ss.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	class Client implements Runnable {
		private Socket s;
		private DataInputStream dis = null;
		private DataOutputStream dos = null;
		private boolean bConnected = false;
		
		public Client(Socket s) {
			this.s = s;
			try {
				dis = new DataInputStream(s.getInputStream());
				dos = new DataOutputStream(s.getOutputStream());
				bConnected = true;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		public void send(String str) {
			try {
				dos.writeUTF(str);
			} catch (IOException e) {
				clients.remove(this);
				System.out.println("对方退出了!我从List里面去掉了!");
				//e.printStackTrace();
			}
		}
		
		public void run() {
			try {
				while(bConnected) {
					String str = dis.readUTF();
System.out.println(str);
					for(int i=0; i<clients.size(); i++) {
						Client c = clients.get(i);
						c.send(str);
//System.out.println(" a string send !");
					}
					/*
					for(Iterator<Client> it = clients.iterator(); it.hasNext(); ) {
						Client c = it.next();
						c.send(str);
					}
					*/
					/*
					Iterator<Client> it = clients.iterator();
					while(it.hasNext()) {
						Client c = it.next();
						c.send(str);
					}
					*/
				}
			} catch (EOFException e) {
				System.out.println("Client closed!");
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if(dis != null) dis.close();
					if(dos != null) dos.close();
					if(s != null)  {
						s.close();
						//s = null;
					}
					
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				
				
			}
		}
		
	}
}

7·Client接收Server端发送的数据#

main方法不能一直用来接收数据,这会影响其他程序的执行。这里依然起线程来单独接收Server传来的数据。

ClientFrame.java

	import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class ClientFrame extends Frame {
	Socket s = null;
	DataOutputStream dos = null;
	DataInputStream dis = null;
	private boolean bConnected = false;

	TextField tfTxt = new TextField();

	TextArea taContent = new TextArea();
	
	Thread tRecv = new Thread(new RecvThread()); 

	public void launchFrame() {
		setLocation(400, 300);
		this.setSize(300, 300);
		add(tfTxt, BorderLayout.SOUTH);
		add(taContent, BorderLayout.NORTH);
		pack();
		this.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent arg0) {
				disconnect();
				System.exit(0);
			}
			
		});
		tfTxt.addActionListener(new TFListener());
		setVisible(true);
		connect();
		
		tRecv.start();
	}
	
	public void connect() {
		try {
			s = new Socket("127.0.0.1", 8888);
			dos = new DataOutputStream(s.getOutputStream());
			dis = new DataInputStream(s.getInputStream());
System.out.println("connected!");
			bConnected = true;
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void disconnect() {
		try {
			dos.close();
			dis.close();
			s.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private class TFListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			String str = tfTxt.getText().trim();
			//taContent.setText(str);
			tfTxt.setText("");
			
			try {
				dos.writeUTF(str);
				dos.flush();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
		}
		
	}
	
	private class RecvThread implements Runnable {

		public void run() {
			try {
				while(bConnected) {
					String str = dis.readUTF();
					//System.out.println(str);
					taContent.setText(taContent.getText() + str + '
');
				}
			} catch (SocketException e) {
				System.out.println("quit!");
			} catch (EOFException e) {
				System.out.println("推出!");
			} catch (IOException e) {
				e.printStackTrace();
			} 
			
		}
		
	}
}
原文地址:https://www.cnblogs.com/zharma/p/4554531.html