1、package mytalk;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import .ServerSocket;import .Socket;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class Server Map clients = new HashMap(); /创建一个Map,记录客户端线程Connection con = null;Statement sta = null;ResultSet rs = null;Server() throws ExceptiontryClass.forName(com.mysq
2、l.jdbc.Driver);con = DriverManager.getConnection(jdbc:mysql:/127.0.0.1:3306/qq?useUnicode=true&characterEncoding=utf8,root,root);sta = con.createStatement();catch(Exception e)System.out.println(找不到驱动程序类 ,加载驱动失败!);ServerSocket ss = new ServerSocket(9000); /服务器端口while(true)System.out.println(链接成功);Socket s = ss.accept(); /服务器启动监听rs = sta.executeQuery(select * from people);while(rs.next()String nickname = rs.getString(2);String chlog = rs.getString(5);String cnk = rs.getString(7);if(chlog.equals(tr
3、ue)/过滤没启动的客户端if(cnk.equals(null)/过滤已启动的客户端ClientThread ct = new ClientThread(s,nickname);clients.put(nickname,ct);/把当前客户端加入集合new Thread(ct).start();class ClientThread implements RunnableSocket s = null; /记录与服务器链接的客户端端口号DataInputStream is = null; /输入字节流DataOutputStream os = null; /输出字节流String str = null; /记录客户端名称String nickname = null; /登陆昵称ClientThread(Socket s,String nickname)this.s = s;this.nickname = nickname;public void run() try while(true)if(s.isConnected()is = new DataInputStream(s.getInp
4、utStream(); /获得输入流str = is.readUTF(); /从输入流中得到数据String stalk = null; /确定是否私聊ResultSet rs0 = sta.executeQuery(select * from people where nickname=+nickname+);while(rs0.next()stalk = rs0.getString(6);if(stalk.equals(null)Iterator it = clients.entrySet().iterator(); /设置一个遍历器,遍历 Mapwhile(it.hasNext() Map.Entry entry = (Map.Entry) it.next(); ClientThread c = (ClientThread)entry.getValue();/获得 Map 的值c.sendMsg(str); /获得客户端的信息并发送给所有的客户端elseClientThread c = clients.get(stalk); /得到私聊对象c.sendMsg(str); /获得客
5、户端的信息并发送给指定客户段端elseclients.remove(nickname);break; /当一个客户端结束后,移出 Map catch (Exception e) public void sendMsg(String str) try os = new DataOutputStream(this.s.getOutputStream(); /获得输出流os.writeUTF(str); /将字符串添加到输出流上并发送出去 catch (IOException e) public static void main(String args) throws Exceptionnew Server();package mytalk;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.Ite
6、mEvent;import java.awt.event.ItemListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import .Socket;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Vector;import javax.swing.BorderFactory;import javax.swing.B
7、uttonGroup;import javax.swing.DefaultListModel;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JRadioButton;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.ListSelectionModel;import javax.swing.ScrollPaneConstants;import javax.swing.event.ListSelectionEvent;import javax.swing.eve
8、nt.ListSelectionListener;public class Client extends JFrame implements ListSelectionListenerConnection con = null;Statement sta = null;ResultSet rs = null;Socket s = null;DataInputStream is = null;DataOutputStream os = null;JTextArea jt = new JTextArea();/输入框JTextArea jta = new JTextArea();/群聊框JTextArea sjta = new JTextArea();/私聊框JScrollPane jsp0 = new JScrollPane(jta);/群聊框JScrollPane jsp1 = new JScrollPane(sjta);/私聊框JScrollPane jsp3 = new JScrollPane(jt);/输入框JPanel jp1 = new JPanel();JPanel eas
9、tPanel = new JPanel(new BorderLayout();JPanel jp = new JPanel(new GridLayout(3, 1);JButton jb = new JButton(发送私聊);JButton jbq = new JButton(发送群聊);JButton refhButton = new JButton(刷新列表);JLabel title = new JLabel(我的在线好友);DefaultListModel listModel = new DefaultListModel();/ 用户列表JList fri = new JList(listModel);JScrollPane js = new JScrollPane(fri);Vector items = new Vector();JComboBox jcb = new JComboBox(items);JRadioButton jc = new JRadioButton(私聊,false);JRadioButton jq = new JRadioButton(群聊,false);ButtonGroup bg = new ButtonGroup();JMenuItem menuItem;JMenuItem cMenuItem;String nickname = null;String str1 = null;/私聊对象Client(final String nkname,String ip) throws Exceptions = new Socket(ip,9000);nickname = nkname;this.setTitle(nkname);this.setLocation(100, 0);jt.setLineWrap(true);/设置文本自动换行jta.setL
《java聊天程序》由会员第***分享,可在线阅读,更多相关《java聊天程序》请在金锄头文库上搜索。