当前位置:文档之家› Java_QQ_源程序代码

Java_QQ_源程序代码

2010-06-23 20:24:11 TiBackimport .*;import java.io.*;import java.sql.*;import java.util.*;public class QQServer {public static void main(String args[]) {try {//用户名同步(14)HashMap<String , Socket> hm = new HashMap<String , Socket>() ;// 服务器在8000端口监听(1)ServerSocket ss = new ServerSocket(8000);while (true) {"服务器正在8000端口监听.....");Socket s = ss.accept();MyService t = new MyService();t.setSocket(s);t.setHashMap(hm) ;t.start();}} catch (Exception e) {}}}// 支持多用户登陆(13)class MyService extends Thread {private Socket s;private HashMap<String , Socket> hm ;public void setHashMap(HashMap<String , Socket> hm) {this.hm = hm ;}public void setSocket(Socket s) {this.s = s;}public void run() {try {// 接收客户端发送来的用户名和密码(2)InputStream is = s.getInputStream();InputStreamRead er isr = new InputStreamRead er(is);BufferedReader br = new BufferedRead er(isr);String uandp = br.readLine();// 拆分(4)String u = uandp.split("%")[0];String p = uandp.split("%")[1];// 到数据库中验证(5)Class.forName("");Connection cn = DriverManager.getConnection("jdbc:;databasename=wy", "sa","123");PreparedStatement ps = cn.prepareStatement("select * from username where username=? and password=?"); ps.setString(1, u);ps.setString(2, p);ResultSet rs = ps.executeQuery();// 发送确认信息回客户端(7)OutputStream os = s.getOutputStream();OutputStreamWriter osw = new OutputStreamWriter(os);PrintWriter pw = new PrintWriter(osw, true);if (rs.next()) {pw.println("ok");Thread.sleep(10) ;//将自己的名字发送给其他人(17)for(Socket ts : hm.values()){OutputStream tos = ts.getOutputStream() ;OutputStreamWriter tosw = new OutputStreamWriter(tos) ;PrintWriter tpw = new PrintWriter(tosw , true) ;tpw.println("user%"+u) ;}//将其他人的名字发送给自己(18)for(String tu : hm.keySet()){pw.println("user%"+tu) ;}//将用户名和Socket存入HashMap(15)hm.put(u, s) ;// 接收客户端发送来的信息(11)while (true) {String message = br.readLine();//判断是否退出if(message.equals("{exit}")){for(Socket ts : hm.values()){OutputStream tos = ts.getOutputStream() ; OutputStreamWriter tosw = new OutputStreamWriter(tos) ; PrintWriter tpw = new PrintWriter(tosw , true) ;tpw.println("exit%"+u) ;}hm.remove(u) ;return ;}String to = message.split("%")[0] ;String mess = message.split("%")[1] ;Socket ts = hm.get(to) ;OutputStream tos = ts.getOutputStream() ; OutputStreamWriter tosw = new OutputStreamWriter(tos) ; PrintWriter tpw = new PrintWriter(tosw , true) ;tpw.println("mess%"+mess) ;}} else {pw.println("err");}} catch (Exception e) {}}}2010-06-23 20:24:37 TiBackimport java.awt.*;import ;import ;import .*;import java.io.*;import javax.swing.*;public class QQLogin extends JFrame implements ActionListener { JTextFiel d txtUser = new JTextField();JPasswordFiel d txtPass = new JPasswordField();public QQLogin() {// 设置窗体属性this.setSize(250, 125);this.setTitle("QQ登陆");this.setResizable(false);// new一大堆组件JLabel labUser = new JLabel("用户名");JLabel labPass = new JLabel("密码");JButton btnLogin = new JButton("登陆");JButton btnReg = new JButton("注册");JButton btnCancel = new JButton("取消");// 注册事件btnLogin.addActionListener(this);btnReg.ad dActionListener(this);btnCancel.addActionListener(this);// 布置输入面板JPanel panInput = new JPanel();panInput.setLayout(new GridLayout(2, 2));panInput.add(labUser);panInput.add(txtUser);panInput.add(labPass);panInput.add(txtPass);// 布置按钮面板JPanel panButton = new JPanel();panButton.setLayout(new Fl owLayout());panButton.add(btnLogin);panButton.add(btnReg);panButton.add(btnCancel);// 布置窗体this.setLayout(new BorderLayout());this.ad d(panInput, BorderLayout.CENTER);this.ad d(panButton, BorderLayout.SOUTH);}public static void main(String args[]) {JFrame.setDefaultLookAndFeelDecorated(true); QQLogin w = new QQLogin();w.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {if (e.getActionCommand().equals("登陆")) {String u = txtUser.getText();String p = txtPass.getText();try{//将用户名和密码发送到服务器(3)Socket s = new Socket("",8000) ;OutputStream os = s.getOutputStream() ; OutputStreamWriter osw = new OutputStreamWriter(os) ; PrintWriter pw = new PrintWriter(osw , true) ;pw.println(u+"%"+p) ;//接收服务器发送回来的确认信息(6)InputStream is = s.getInputStream() ; InputStreamRead er isr = new InputStreamRead er(is) ; BufferedReader br = new BufferedRead er(isr) ;String yorn = br.readLine() ;//显示主窗体(8)if(yorn.equals("ok")){QQMain w = new QQMain(u) ;w.setSocket(s) ;w.setVisible(true) ;this.setVisible(false) ;}else {JOptionPane.showMessageDialog(this, "滚") ; }}catch(Exception ee){}}if (e.getActionCommand().equals("注册")) {}if (e.getActionCommand().equals("取消")) { System.exit(0);}}}。

相关主题