package ldci.com.chatApp.client.view; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Vector; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import ldci.com.chatApp.client.service.ChatService; import ldci.com.chatApp.userinfo.SomeBodySays; import ldci.com.chatApp.userinfo.UserInfo; /* * 聊天界面 *注意发送按钮事件就是ChatService请求 */ public class ChatView extends JFrame { String temp = new String(); JFrame chatJf = new JFrame("聊天界面"); JPanel chatJp = new JPanel(); TextArea getInfo = new TextArea(null, 10, 80, TextArea.SCROLLBARS_BOTH); // 接收信息 JTextField sendInfo = new JTextField(50); // 等待发送的信息 boolean bol = true; JButton sendButton = new JButton("发送"); UserInfo user = new UserInfo(); ChatService chatService = new ChatService(); // 内部继续Thread class ThreadReadInfo extends Thread { public void run() { while (bol) { try { temp = chatService.getChat(); getInfo.setText(temp); } catch (Exception e) { System.out.println(e); e.printStackTrace(); getInfo.append("与服务器断开"); }finally{ try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println(e); e.printStackTrace(); } } } } } // 构造聊天窗口,启动线程 public ChatView(UserInfo temp) { user.setName(temp.getName()); chatJf.setSize(600, 300); chatJp.add(getInfo); chatJp.add(sendInfo); chatJp.add(sendButton); sendButton.addActionListener(new SendActionLis()); getInfo.setEditable(false); chatJf.add(chatJp); chatJf.setResizable(false); chatJf.setVisible(true); chatJf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); new ThreadReadInfo().start(); } // 把说的话和名字打包成一个类发送给Server public void doSend() { SomeBodySays say = new SomeBodySays(); String says = sendInfo.getText(); String name = user.getName(); say.setName(name); say.setSays(says); chatService.sendChat(say); // String ss = chatService.sendChat(say); // getInfo.setText(ss); } // 发送事件处理 class SendActionLis implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == sendButton) { doSend(); sendInfo.setText(null); } } } } package ldci.com.chatApp.client.view; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class LinkView extends JFrame { LinkView linkView; JFrame linkJf = new JFrame("连接服务器"); JPanel linkJp = new JPanel(); JLabel ip = new JLabel("IP:"); JTextField serverIp = new JTextField(20); JLabel port = new JLabel("PORT:"); JTextField serverPort = new JTextField(20); JButton link = new JButton("连接"); Socket m_skt = null; public LinkView() { linkView = this; linkJp.add(ip); linkJp.add(serverIp); linkJp.add(port); linkJp.add(serverPort); linkJp.add(link); link.addActionListener(new linkActionLis()); linkJf.add(linkJp); linkJf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); linkJf.setSize(250, 280); linkJf.setVisible(true); linkJf.setResizable(false); } class linkActionLis implements ActionListener { public void actionPerformed(ActionEvent e) { try { m_skt = new Socket(serverIp.getText(), Integer.parseInt(serverPort .getText())); System.out.println("连接成功"); LoginView view = new LoginView(); linkView.linkJf.dispose(); } catch (NumberFormatException e1) { new JOptionPane().showMessageDialog(null, "连接失败"); System.out.println(e1); } catch (UnknownHostException e1) { new JOptionPane().showMessageDialog(null, "连接失败"); System.out.println(e1); } catch (IOException e1) { new JOptionPane().showMessageDialog(null, "连接失败"); System.out.println(e1); } } } } package ldci.com.chatApp.client.view; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import ldci.com.chatApp.client.service.LoginService; import ldci.com.chatApp.userinfo.UserInfo; /* * 登陆界面 */ public class LoginView extends JFrame { LoginView loginView; JFrame loginJf = new JFrame("登陆窗口"); JPanel loginJp = new JPanel(); /* * 登陆中主要有用户名输入框,密码框,聊天按钮(既登陆),注册按钮。 */ JLabel userName = new JLabel("用户帐号"); JLabel userPass = new JLabel("用户密码"); JTextField nameText = new JTextField(10); JPasswordField passText = new JPasswordField(10); JButton chatButton = new JButton("聊天"); JButton regButton = new JButton("注册"); JButton resetButton = new JButton("重置"); public LoginView(){ loginView = this; loginJp.setLayout(null); loginJp.add(userName); userName.setBounds(40, 40, 100, 40); loginJp.add(nameText); nameText.setBounds(140, 50, 200, 30); loginJp.add(userPass); userPass.setBounds(40, 110, 100, 40); loginJp.add(passText); passText.setBounds(140, 120, 200, 30); loginJp.add(chatButton); chatButton.addActionListener(new chat_reg_resetActionLis()); chatButton.setBounds(100, 170, 70, 40); loginJp.add(regButton); regButton.addActionListener(new chat_reg_resetActionLis()); regButton.setBounds(200, 170, 70, 40); loginJp.add(resetButton); resetButton.addActionListener(new chat_reg_resetActionLis()); resetButton.setBounds(300, 170, 70, 40); loginJf.add(loginJp); loginJf.setVisible(true); loginJf.setSize(400,300); loginJf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); loginJf.setResizable(false); } /* * 内部监听类,监听聊天事件和注册事件 */ class chat_reg_resetActionLis implements ActionListener { UserInfo temp = new UserInfo(); public void actionPerformed(ActionEvent e) { String name = nameText.getText(); String pass = String.valueOf(passText.getPassword()); if(name.length() != 0 && pass.length() != 0){ temp.setName(name); temp.setUserPassword(pass); if(e.getSource() == chatButton){ System.out.println("聊天按钮"); LoginService loginService = new LoginService(); temp.toString(); if(loginService.sendLogin(temp)){ loginView.loginJf.dispose(); new ChatView(temp); }else{ new JOptionPane().showMessageDialog(null, "登陆失败"); } }else if(e.getSource() == regButton){ System.out.println("注册按钮"); RegView reg = new RegView(); loginView.loginJf.dispose(); }else if(e.getSource() == resetButton){ nameText.setText(null); passText.setText(null); } }else if(name.length() ==0 || pass.length() == 0){ if(e.getSource() == resetButton){ nameText.setText(null); passText.setText(null); }else if(e.getSource() == regButton){ System.out.println("注册按钮"); RegView reg = new RegView(); loginView.loginJf.dispose(); }else{ new JOptionPane().showMessageDialog(null, "用户名和密码不能为空"); } } } } } package ldci.com.chatApp.client.view; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JRadioButton; import javax.swing.JTextField; import ldci.com.chatApp.client.service.RegService; import ldci.com.chatApp.userinfo.RegInfo; public class RegView extends JFrame { RegView regView; JFrame regJf = new JFrame("注册界面"); JPanel panel = new JPanel(); // 容器控件Panel存放各类组件 JLabel loveName = new JLabel("昵称:"); JLabel birthday = new JLabel("生日:"); JLabel yearLabel = new JLabel("年"); JLabel mouthLabel = new JLabel("月"); JLabel dayLabel = new JLabel("日"); JLabel sexLabel = new JLabel("性别:"); JLabel passLabel = new JLabel("密码:"); JLabel passLabel1 = new JLabel("确认密码:"); JLabel showLabel = new JLabel("6-16个字符组成,区分大小写,不能为9为以下纯数字"); JLabel drassLabel = new JLabel("所在地:"); JTextField loveField = new JTextField(18); JPasswordField passField = new JPasswordField(18); JPasswordField pass1Field = new JPasswordField(18); private String year[] = new String[50]; private String month[] = new String[12]; private String day[] = new String[31]; // 数组信息 private String country[] = { "中国", "美国" }; private String captialCh[] = { "山东", "广东", "辽宁" }; private String captialAr[] = {"亚拉巴马州","阿拉斯加州","亚利桑那州"}; private String cityCh1[] = { "泰安", "济南", "青岛", "烟台", "威海" }; private String cityCh2[] = { "广州", "珠海", "佛山", "从化" }; private String cityCh3[] = { "沈阳", "大连", "鞍山", "本溪" }; private String cityAr1[] = { "多森", "莫比尔", "伯明翰" }; private String cityAr2[] = { "朱诺", "菲尔班克斯" }; private String cityAr3[] = { "菲尔科斯", "图桑", "美克" }; // 下拉列表组建 JComboBox yearBox = new JComboBox(); JComboBox monthBox = new JComboBox(); JComboBox dayBox = new JComboBox(); JComboBox countryBox = new JComboBox(); JComboBox shengBox = new JComboBox(); JComboBox shiBox = new JComboBox(); // 单选组建 JRadioButton boyRb = new JRadioButton("男"); JRadioButton girlRb = new JRadioButton("女"); // 存放JRadioButton实现只选一个 ButtonGroup bg = new ButtonGroup(); // Button组建 JButton regButton = new JButton("注册"); JButton backButton = new JButton("返回"); JButton resetButton = new JButton("重置"); // 构造方法 public RegView() { regView = this; panel.setLayout(new FlowLayout(FlowLayout.LEFT)); // 左对齐布局管理 addInfoBox(); // 向JComboBox中添加项 // 将JRadioButton放入ButtonGroup中 bg.add(boyRb); bg.add(girlRb); // 向panel中添加组件 panel.add(loveName); panel.add(loveField); panel.add(birthday); panel.add(yearBox); panel.add(yearLabel); panel.add(monthBox); panel.add(mouthLabel); panel.add(dayBox); panel.add(dayLabel); panel.add(sexLabel); panel.add(boyRb); panel.add(girlRb); panel.add(passLabel); panel.add(passField); panel.add(showLabel); panel.add(passLabel1); panel.add(pass1Field); panel.add(drassLabel); panel.add(countryBox); panel.add(shengBox); panel.add(shiBox); panel.add(regButton); panel.add(resetButton); panel.add(backButton); this.getContentPane().add(panel); // 保存监听器 RegButtonListener saveButtonListener = new RegButtonListener(); regButton.addActionListener(saveButtonListener); // 重置 BtnReset_ReadListener btn_rrs_Listener = new BtnReset_ReadListener(); resetButton.addActionListener(btn_rrs_Listener); // 返回 BackActionListener back = new BackActionListener(); backButton.addActionListener(back); // 实现countryBox事件监听 CountryBoxListener countryBoxListener = new CountryBoxListener(); countryBox.addActionListener(countryBoxListener); // 实现shengBox事件监听 ShengBoxListener ShengBoxListener = new ShengBoxListener(); shengBox.addActionListener(ShengBoxListener); // 实现monthBox事件监听 MonthBoxListener monthBoxListener = new MonthBoxListener(); monthBox.addActionListener(monthBoxListener); regJf.add(panel); regJf.setVisible(true); regJf.setSize(330,500); regJf.setResizable(true); regJf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // 向JComboBox中添加默认信息 public void addInfoBox() { for(int i=1980,j=0;i!=2010;i++,j++){ year[j]= i+""; } for (int i = 0; i < year.length; i++) { yearBox.addItem(year[i]); } for(int i=1,j=0;i!=13;i++,j++){ month[j]=i+""; } for (int i = 0; i < month.length; i++) { monthBox.addItem(month[i]); } for(int i=1,j=0;i!=32;i++,j++){ day[j]=i+""; } for (int i = 0; i < day.length; i++) { dayBox.addItem(day[i]); } for (int i = 0; i < country.length; i++) { countryBox.addItem(country[i]); } for(int i=0;i<captialCh.length;i++){ shengBox.addItem(captialCh[i]); } for(int i=0;i<cityCh1.length;i++){ shiBox.addItem(cityCh1[i]); } } // 注册事件 class RegButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { RegInfo regInfo = new RegInfo(); // 获取用户输入 String lovename = loveField.getText(); String password = String.valueOf(passField.getPassword()); String password1 = String.valueOf(pass1Field.getPassword()); if (lovename.equals(null) || lovename.length() == 0) { JOptionPane.showMessageDialog(null, "用户名不能为空"); } // 将数据保存到RegInfo else { regInfo.setLoveName(lovename); regInfo.setPassword(password); regInfo.setPassword1(password1); String year = yearBox.getSelectedItem().toString(); String month = monthBox.getSelectedItem().toString(); String day = dayBox.getSelectedItem().toString(); String sex = null; if (boyRb.isSelected()) { sex = boyRb.getText(); } else { sex = girlRb.getText(); } String country = countryBox.getSelectedItem().toString(); String sheng = shengBox.getSelectedItem().toString(); String shi = shiBox.getSelectedItem().toString(); regInfo.setYear(year); regInfo.setMonth(month); regInfo.setDay(day); regInfo.setSex(sex); regInfo.setCountry(country); regInfo.setSheng(sheng); regInfo.setShi(shi); // 注册客户端请求 RegService regService = new RegService(); if(regService.sendReg(regInfo)){ new JOptionPane().showMessageDialog(null, "注册成功"); }else{ new JOptionPane().showMessageDialog(null, "注册失败"); } } } } // 重置 class BtnReset_ReadListener implements ActionListener { public void actionPerformed(ActionEvent e) { Object evt = e.getSource();//获取事件相应按钮 if (evt == resetButton) { loveField.setText(""); passField.setText(""); pass1Field.setText(""); boyRb.setSelected(false); girlRb.setSelected(false); yearBox.setSelectedIndex(0); monthBox.setSelectedIndex(0); dayBox.setSelectedIndex(0); countryBox.setSelectedIndex(0); shengBox.setSelectedIndex(0); shiBox.setSelectedIndex(0); } } } // 返回事件 class BackActionListener implements ActionListener{ public void actionPerformed(ActionEvent e){ LoginView login = new LoginView(); regView.regJf.dispose(); } } // 触发countyBox 事件相应 class CountryBoxListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (String.valueOf(countryBox.getSelectedItem()).equals("美国")) { shengBox.removeAllItems(); // 删除所有项 for (int i = 0; i < captialAr.length; i++) { shengBox.addItem(captialAr[i]); } } if (String.valueOf(countryBox.getSelectedItem()).equals("中国")) { shengBox.removeAllItems(); for (int i = 0; i < captialCh.length; i++) { shengBox.addItem(captialCh[i]); } } } } // 触发shengBox事件响应 class ShengBoxListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (String.valueOf(shengBox.getSelectedItem()).equals("山东")){ shiBox.removeAllItems(); for(int i=0;i<cityCh1.length;i++){ shiBox.addItem(cityCh1[i]); } } if (String.valueOf(shengBox.getSelectedItem()).equals("广东")){ shiBox.removeAllItems(); for(int i=0;i<cityCh2.length;i++){ shiBox.addItem(cityCh2[i]); } } if (String.valueOf(shengBox.getSelectedItem()).equals("辽宁")){ shiBox.removeAllItems(); for(int i=0;i<cityCh3.length;i++){ shiBox.addItem(cityCh3[i]); } } if (String.valueOf(shengBox.getSelectedItem()).equals("亚拉巴马州")){ shiBox.removeAllItems(); for(int i=0;i<cityAr1.length;i++){ shiBox.addItem(cityAr1[i]); } } if (String.valueOf(shengBox.getSelectedItem()).equals("阿拉斯加州")){ shiBox.removeAllItems(); for(int i=0;i<cityAr2.length;i++){ shiBox.addItem(cityAr2[i]); } } if (String.valueOf(shengBox.getSelectedItem()).equals("亚利桑那州")){ shiBox.removeAllItems(); for(int i=0;i<cityAr3.length;i++){ shiBox.addItem(cityAr3[i]); } } } } // 触发monthBox事件响应 class MonthBoxListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (String.valueOf(monthBox.getSelectedItem()) .equals("2")){ dayBox.removeAllItems(); for(int i=1,j=0;i!=30;i++,j++){ day[j]=i+""; dayBox.addItem(day[j]); } } else if(String.valueOf(monthBox.getSelectedItem()) .equals("4")||String.valueOf(monthBox.getSelectedItem()) .equals("6")||String.valueOf(monthBox.getSelectedItem()) .equals("9")||String.valueOf(monthBox.getSelectedItem()) .equals("11")){ dayBox.removeAllItems(); for(int i=1,j=0;i!=31;i++,j++){ day[j]=i+""; dayBox.addItem(day[j]); } } else { dayBox.removeAllItems(); for(int i=1,j=0;i!=32;i++,j++){ day[j]=i+""; dayBox.addItem(day[j]); } } } } } package ldci.com.chatApp.client.service; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.ArrayList; import java.util.Vector; import ldci.com.chatApp.userinfo.SomeBodySays; // 连接服务器,并发送聊天请求,此方法应该实现用户名和此用户说的话的发送。 public class ChatService { // 获得当前登陆用户的名字和说的话,发送给服务器 Socket toServer = null; ObjectInputStream ois = null; ObjectOutputStream oos = null; public String sendChat(SomeBodySays temp) { String message = ""; Vector info = new Vector(); try { toServer = new Socket("127.0.0.1", 7655); ois = new ObjectInputStream(toServer.getInputStream()); oos = new ObjectOutputStream(toServer.getOutputStream()); oos.writeObject("chat"); oos.writeObject(temp); System.out.println("发送成功"); info = (Vector) (ois.readObject()); for(int i=0;i!=info.size();i++){ message += (String)(info.get(i).toString())+"/n"; System.out.println(message); } } catch (ClassNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { } finally { try { if (toServer != null) toServer.close(); } catch (IOException e) { System.out.println(e); e.printStackTrace(); } } return message; } public String getChat() { Vector tempSays = new Vector(); String message = ""; try { toServer = new Socket("127.0.0.1", 7655); ois = new ObjectInputStream(toServer.getInputStream()); oos = new ObjectOutputStream(toServer.getOutputStream()); oos.writeObject("getChat"); tempSays = (Vector)(ois.readObject()); for(int i=0;i!=tempSays.size();i++){ message += (String)(tempSays.get(i).toString()) + "/n"; System.out.println(message); } } catch (IOException e) { } catch (ClassNotFoundException e) { System.out.println(e); e.printStackTrace(); } finally { try { if (toServer != null) toServer.close(); } catch (IOException e) { System.out.println(e); e.printStackTrace(); } } return message; } } package ldci.com.chatApp.client.service; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.net.SocketException; import ldci.com.chatApp.userinfo.UserInfo; // 连接服务器,并发出登陆的请求 public class LoginService{ Socket toServer = null; ObjectInputStream ois = null; ObjectOutputStream oos = null; public boolean sendLogin(UserInfo tempInfo) { System.out.println("进入到了接收登陆对象的方法"); boolean bol = false; try { toServer = new Socket("127.0.0.1", 7655); oos = new ObjectOutputStream(toServer.getOutputStream()); oos.writeObject("login"); oos.writeObject(tempInfo); ois = new ObjectInputStream(toServer.getInputStream()); if (((String) (ois.readObject())).equals("true")) { bol = true; } else { bol = false; } } catch (IOException e) { System.out.println(e); e.printStackTrace(); } catch (ClassNotFoundException e) { System.out.println(e); e.printStackTrace(); }finally { try { if(toServer != null) toServer.close(); } catch (IOException e) { System.out.println(e); e.printStackTrace(); } } return bol; } } package ldci.com.chatApp.client.service; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import ldci.com.chatApp.userinfo.RegInfo; // 连接服务器,并发送注册的请求 public class RegService{ Socket toServer = null; ObjectInputStream ois = null; ObjectOutputStream oos = null; public boolean sendReg(RegInfo tempInfo){ boolean bol= false; try { // System.out.println("进入到了注册方法!"); toServer = new Socket("127.0.0.1", 7655); ois = new ObjectInputStream(toServer.getInputStream()); oos = new ObjectOutputStream(toServer.getOutputStream()); oos.writeObject("reg"); oos.writeObject(tempInfo); String message = (String)(ois.readObject()); if(message.equals("true")){ bol = true; } else{ bol = false; } } catch (IOException e) { } catch (ClassNotFoundException e) { e.printStackTrace(); }finally{ try { if(toServer != null) toServer.close(); } catch (IOException e) { System.out.println(e); e.printStackTrace(); } } return bol; } } package ldci.com.chatApp.userinfo; /* * name; * password; * Rpassword; * sex; * age; * city; * 信息打印功能 * 本地验证 */ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class RegInfo implements java.io.Serializable { private String loveName; private String password; private String password1; private String year; private String month; private String day; private String sex; private String country; private String sheng; private String shi; public RegInfo() { } public String getLoveName() { return loveName; } public void setLoveName(String loveName) { this.loveName = loveName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPassword1() { return password1; } public void setPassword1(String password1) { this.password1 = password1; } public String getDay() { return day; } public void setDay(String day) { this.day = day; } public String getMonth() { return month; } public void setMonth(String month) { this.month = month; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getSheng() { return sheng; } public void setSheng(String sheng) { this.sheng = sheng; } public String getShi() { return shi; } public void setShi(String shi) { this.shi = shi; } public void print() { System.out.println("*******************************"); System.out.println("昵称: " + loveName + "/n" + "生日: " + year + "-" + month + "-" + day + "/n" + "性别:" + sex + "/n" + "所在地:" + country + " " + sheng + " " + shi); System.out.println("*******************************"); } } package ldci.com.chatApp.userinfo; // 把某个人说的话和他的姓名打成一个对象,直接发送到服务器 public class SomeBodySays implements java.io.Serializable{ private String name; private String says; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSays() { return says; } public void setSays(String says) { this.says = says; } public String print(){ return (name + ":" + says); } public String toString(){ return (name + ":" + says); } } package ldci.com.chatApp.userinfo; // 用户信息类,包括用户名和密码。 // 能够进行本地验证 public class UserInfo implements java.io.Serializable { private String name; private String userPassword; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } } package ldci.com.chatApp.server; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Iterator; import javax.swing.JOptionPane; import ldci.com.chatApp.userinfo.RegInfo; import ldci.com.chatApp.userinfo.UserInfo; public class FileData { private ArrayList regInfo = null; public FileData() { boolean bol = readFile(); if (!bol) { regInfo = new ArrayList(); } } public void printInfo() { for (int i = 0; i < regInfo.size(); i++) { ((RegInfo) regInfo.get(i)).print(); } } public boolean checkLogin(UserInfo temp) { boolean bol = false; for (int i = 0; i != regInfo.size(); i++) { RegInfo user = (RegInfo) regInfo.get(i); if (user.getLoveName().equals(temp.getName()) && user.getPassword1().equals(temp.getUserPassword())) { bol = true; break; } else { bol = false; } } return bol; } public boolean checkDouble(RegInfo temp) { boolean bol = true; System.out.println("进入到了CheckDouble方法"); for (int i = 0; i != regInfo.size(); i++) { RegInfo user = (RegInfo) regInfo.get(i); if (user.getLoveName().equals(temp.getLoveName())) { bol = false; } else { continue; } } return bol; } public void addArray(RegInfo tempInfo) { if (tempInfo != null) { regInfo.add(tempInfo); } } public boolean saveFile(RegInfo tempInfo) { boolean bol = false; File file = null; FileOutputStream fos = null; ObjectOutputStream oos = null; System.out.println("进入到了只在注册信息方法"); try { if (checkDouble(tempInfo)) { file = new File("F://个人重要资料(勿删)//regInfo.txt"); fos = new FileOutputStream(file); oos = new ObjectOutputStream(fos); addArray(tempInfo); oos.writeObject(regInfo); oos.flush(); bol = true; } else { System.out.println("用户名已经存在"); bol = false; } } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println(e); return bol; } catch (IOException e) { e.printStackTrace(); System.out.println(e); return bol; } finally { try { if (fos != null) fos.close(); if (oos != null) oos.close(); } catch (IOException e) { e.printStackTrace(); System.out.println(e); } } return bol; } public boolean readFile() { boolean bol = false; File file = null; FileInputStream fis = null; ObjectInputStream ois = null; try { file = new File("F://个人重要资料(勿删)//regInfo.txt"); fis = new FileInputStream(file); ois = new ObjectInputStream(fis); regInfo = (ArrayList) (ois.readObject()); printInfo(); bol = true; if (regInfo == null) { new JOptionPane().showMessageDialog(null, "文件为空"); } } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println(e); } catch (IOException e) { e.printStackTrace(); System.out.println(e); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println(e); } finally { try { if (fis != null) fis.close(); if (ois != null) ois.close(); } catch (IOException e) { System.out.println(e); e.printStackTrace(); } } return bol; } } package ldci.com.chatApp.server; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.ArrayList; import java.util.Iterator; import java.util.Vector; import ldci.com.chatApp.userinfo.RegInfo; import ldci.com.chatApp.userinfo.SomeBodySays; import ldci.com.chatApp.userinfo.UserInfo; // 处理响应用户的登陆,注册,聊天,的请求 public class ProcessRequest { private Vector alChatList = new Vector(); private Socket socket; private ObjectOutputStream oos; private ObjectInputStream ois; private FileData data = new FileData(); SomeBodySays says = new SomeBodySays(); public ProcessRequest(Socket tmp,Vector alChatList) { socket = tmp; this.alChatList = alChatList; process(); } public ProcessRequest() { } public boolean process() { boolean bol = false; try { oos = new ObjectOutputStream(socket.getOutputStream()); ois = new ObjectInputStream(socket.getInputStream()); String req = (String) (ois.readObject()); if ("login".equals(req)) { System.out.println("你正在登陆"); UserInfo clientInfo = (UserInfo) ois.readObject(); boolean bl = prcoessLogin(clientInfo); if (bl) { oos.writeObject("true"); } else { oos.writeObject("false"); } } else if ("reg".equals(req)) { System.out.println("你正在注册"); RegInfo clientInfo = (RegInfo) ois.readObject(); System.out.println("传送注册信息成功"); boolean bl = processReg(clientInfo); if (bl) { System.out.println("成功"); oos.writeObject("true"); } else { System.out.println("失败"); oos.writeObject("false"); } } else if ("chat".equals(req)) { System.out.println("你正在聊天"); // 接收到消息并添加到SomeBodySays集合当中 says = ((SomeBodySays)( ois.readObject())); alChatList.add(says); } else if ("getChat".equals(req)) { System.out.println("获得聊天记录"); if (alChatList.size() != 0) { oos.writeObject(alChatList); } } else { System.out.println("不明请求"); } } catch (IOException e) { System.out.println(e); e.printStackTrace(); } catch (ClassNotFoundException e) { System.out.println(e); e.printStackTrace(); } finally { try { if (socket != null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } return bol; } synchronized public boolean prcoessLogin(UserInfo info) { boolean bl = false; if (data.checkLogin(info)) { bl = true; } else { bl = false; } return bl; } synchronized public boolean processReg(RegInfo info) { boolean bl = false; if (data.checkDouble(info)) { if (data.saveFile(info)) { System.out.println("保存成功"); bl = true; }else{ System.out.println("保存失败"); return false; } } else { System.out.println("CheckDouble失败"); bl = false; } return bl; } } package ldci.com.chatApp.server; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Vector; public class StartServer { private ServerSocket server; private Vector alChat= null; public StartServer(){ try { // 1. 建立端口,绑定服务 server = new ServerSocket(7655); alChat= new Vector(); new ServerThread().start(); } catch (IOException e) { System.out.println("端口占用"+e); } } // 2.写内容部类,进行多客户监听 class ServerThread extends Thread{ public void run(){ for(;;){ // 3.开始监听,监听到套接字,并传递给处理套接字类 System.out.println("开始监听"); try { Socket fromClientSocket = server.accept(); new ProcessRequest(fromClientSocket,alChat); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { StartServer myServer = new StartServer(); } } package ldci.com.chatApp.run; import ldci.com.chatApp.client.view.LoginView; // 运行起来 public class runChatApp { public static void main(String[] args){ LoginView login = new LoginView(); } }