CS模式聊天软件

来源:互联网 发布:在vb中cstr是什么意思 编辑:程序博客网 时间:2024/04/29 22:06
package sserver;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.ScrollPaneLayout;public class waitsocket extends JFrame implements ActionListener{JScrollPane errorinfor;ScrollPaneLayout layout;int portnum=8008;Boolean hasbooted=false;public JTextArea show,errorinforshow;JLabel tip;JButton boot,port;JTextField papapa;public  waitsocket(){super.setTitle("控制台");super.setBounds(400,100,400,500);super.setVisible(true);super.setLayout(null);papapa=new JTextField();errorinfor=new JScrollPane();layout=new ScrollPaneLayout();errorinforshow=new JTextArea();errorinfor.setLayout(layout);show=new JTextArea();errorinfor.setViewportView(errorinforshow);this.add(errorinfor);this.add(papapa);errorinfor.setBounds(10,170,365,250);tip=new JLabel("当前连接数:");port=new JButton("修改主机端口");boot=new JButton("启动服务器程序");super.add(tip);super.add(show);super.add(port);super.add(boot);papapa.setBounds(250, 20, 60, 30);papapa.setText(String.valueOf(this.portnum));port.setBounds(40,60,300,40);boot.setBounds(40,120,300,40);port.addActionListener(this);boot.addActionListener(this);this.setResizable(false);tip.setBounds(20,20,100,30);show.setBounds(100, 20, 100, 30);show.setText(String.valueOf(0));}public static void main(String[]args){waitsocket w=new waitsocket();}@Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource()==port){this.portnum=Integer.parseInt(JOptionPane.showInputDialog(null,"请重设监听端口号","8008"));this.papapa.setText(String.valueOf(this.portnum));this.errorinforshow.append("已修改端口"+String.valueOf(this.portnum)+"\n");}// TODO Auto-generated method stubelse if(e.getSource()==boot&&hasbooted==false){mc m=new mc(show,errorinforshow,portnum,this);m.start();this.hasbooted=true;}}}package cclient;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.Socket;import java.text.SimpleDateFormat;import java.util.Date;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.swing.DefaultListModel;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JPasswordField;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.ScrollPaneLayout;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;class login implements ActionListener {transfer t;JButton modify, modifyport;JFrame frame;JLabel id;JLabel password;JTextField idin;JTextField passwordin;JButton login;JButton register;mainwindow m;static Boolean haveconnected = false;static int port = 8008;public login(transfer t) {this.t = t;String ip = "0.0.0.0";modify = new JButton("高级");frame = new JFrame("登录界面");id = new JLabel("用户名");password = new JLabel("密码");idin = new JTextField();passwordin = new JTextField();login = new JButton("登录");register = new JButton("注册");modifyport = new JButton("端口");frame.setLayout(null);frame.add(id);frame.add(modify);frame.add(idin);frame.add(password);frame.add(passwordin);frame.add(login);frame.add(register);frame.add(modifyport);modifyport.setBounds(340, 40, 60, 20);modifyport.addActionListener(this);modify.setBounds(150, 150, 100, 40);id.setBounds(20, 20, 100, 40);idin.setBounds(120, 20, 200, 40);password.setBounds(20, 80, 100, 40);passwordin.setBounds(120, 80, 200, 40);login.setBounds(40, 150, 100, 40);register.setBounds(260, 150, 100, 40);login.addActionListener(this);register.addActionListener(this);modify.addActionListener(this);frame.setBounds(400, 200, 400, 250);frame.setVisible(true);frame.setResizable(false);}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif (e.getSource() == modify || e.getSource() == modifyport) {if (e.getSource() == modify) {String temp = JOptionPane.showInputDialog("请重新输入服务器IP");Pattern p1 = Pattern.compile("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$");Matcher m1 = p1.matcher(temp);if (m1.matches())t.user.ip = temp;elseJOptionPane.showMessageDialog(null, "格式错误", "系统提示",JOptionPane.ERROR_MESSAGE);} else {String temp = JOptionPane.showInputDialog("重置端口");Pattern p1 = Pattern.compile("^[0-9]{4}$");Matcher m1 = p1.matcher(temp);if (m1.matches())this.port = Integer.parseInt(temp);elseJOptionPane.showMessageDialog(null, "格式错误", "系统提示",JOptionPane.ERROR_MESSAGE);}} else {if (haveconnected == false) {try {t.user.s = new Socket(t.user.ip, this.port);System.out.println("succeed");} catch (IOException e1) {JOptionPane.showMessageDialog(null, "接入服务器失败", "系统提示",JOptionPane.ERROR_MESSAGE);// TODO Auto-generated catch blocke1.printStackTrace();}try {t.user.in = new BufferedReader(new InputStreamReader(t.user.s.getInputStream()));t.user.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((t.user.s.getOutputStream()))), true);} catch (IOException e1) {JOptionPane.showMessageDialog(null, "通信异常", "系统提示",JOptionPane.ERROR_MESSAGE);// TODO Auto-generated catch blocke1.printStackTrace();}haveconnected = true;}if (e.getSource() == login) {String idtemp = idin.getText();String password = passwordin.getText();t.login(idtemp, password);t.getmessage();if (t.waitecho() == false) {JOptionPane.showMessageDialog(null, "用户名或密码错误", "系统提示",JOptionPane.ERROR_MESSAGE);} else {t.getclient();System.out.println("getclient");mainwindow m = new mainwindow(t, t.user.name, t.user.id);frame.dispose();m.setfriendlist(t.user.friend);}} else if (e.getSource() == register) {registerfront f = new registerfront(t);}}}class mainwindow extends Thread implements ActionListener,ListSelectionListener {String na;String i;JLabel name;JLabel id;JLabel tip;JButton addfriends;JButton refresh;JScrollPane pane;JFrame main;JButton request;JList friend;DefaultListModel friendlist;transfer t;communicating window[];int count = 0;public void run() {while (true) {t.getmessage();t.orderparse(this);}}public mainwindow(transfer t, String na, String i) {this.t = t;window = new communicating[8];name = new JLabel("昵称:" + na);id = new JLabel("用户名:" + i);tip = new JLabel("好友列表");request = new JButton("发起");refresh = new JButton("刷新");addfriends = new JButton("添加");pane = new JScrollPane();friend = new JList();friend.setBackground(Color.yellow);main = new JFrame();main.setBounds(800, 80, 300, 600);main.setTitle("DD2015-version1.0");main.getContentPane().setBackground(Color.ORANGE);main.add(id);main.add(name);pane.add(friend);main.add(pane);main.add(refresh);main.add(addfriends);main.add(request);main.add(tip);name.setBounds(20, 10, 100, 20);id.setBounds(20, 40, 200, 20);tip.setBounds(120, 60, 60, 20);pane.setBounds(10, 90, 280, 400);addfriends.setBounds(10, 500, 80, 40);request.setBounds(100, 500, 80, 40);refresh.setBounds(190, 500, 80, 40);refresh.addActionListener(this);addfriends.addActionListener(this);request.addActionListener(this);request.setEnabled(false);main.setResizable(false);main.setLayout(null);main.setVisible(true);friend.addListSelectionListener(this);main.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent we) {main.setVisible(false);t.quit();System.exit(0);}});this.start();}public void setfriendlist(String[] f) {friendlist = new DefaultListModel();int i = 0, num = Integer.parseInt(t.user.friendnumber);while (i < num) {friendlist.addElement(t.user.friend[i]);i++;}friend.setModel(friendlist);pane.getViewport().add(friend);}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif (e.getSource() == addfriends) {String temp = JOptionPane.showInputDialog("请输入联系人用户名");t.addfriend(temp);} else if (e.getSource() == refresh) {t.refresh();} else if (e.getSource() == request) {int i = 0, j = 0;while (window[i] != null) {i++;if (i > 7)i = 0;j++;if (j > 15)break;}int index = friend.getSelectedIndex();String id = t.user.friend[index].substring(0, 10);String name = t.user.friend[index].substring(11,t.user.friend[index].indexOf(' ',11));window[i] = new communicating(id, name, t);}}@Overridepublic void valueChanged(ListSelectionEvent arg0) {request.setEnabled(true);// TODO Auto-generated method stub}}public static class communicating implements ActionListener {JScrollPane out;JScrollPane in;ScrollPaneLayout layout;ScrollPaneLayout layin;JFrame main;JLabel id;JLabel name;JTextArea input;JTextArea output;JButton send;String obid, obname;Date date;SimpleDateFormat sdf;transfer t;public communicating(String iid, String nname, transfer t) {this.t = t;this.obid = iid;this.obname = nname;date = new Date();sdf = new SimpleDateFormat("kk:mm:ss");layin = new ScrollPaneLayout();layout = new ScrollPaneLayout();out = new JScrollPane();out.setLayout(layin);in = new JScrollPane();in.setLayout(layout);id = new JLabel(obid);name = new JLabel(obname);main = new JFrame("正在会话");input = new JTextArea();output = new JTextArea();send = new JButton("发送");out.setViewportView(output);main.setLayout(null);main.setBounds(300, 100, 550, 500);main.getContentPane().setBackground(Color.pink);main.setResizable(false);main.add(id);main.add(name);in.setViewportView(input);main.add(in);main.add(out);main.add(send);send.addActionListener(this);id.setBounds(130, 10, 80, 20);name.setBounds(20, 10, 100, 20);in.setBounds(10, 300, 400, 100);out.setBounds(10, 40, 400, 250);send.setBounds(320, 420, 60, 30);output.setEditable(false);main.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {if (e.getSource() == send) {String temp, time = sdf.format(date);temp = input.getText();input.setText("");output.append(t.user.name + " " + time + temp + "\n");t.sendtalk(obid, obname, temp);}// TODO Auto-generated method stub}}}package cclient;public class maincontrol {public static void main(String[] args) {client user = new client();transfer t = new transfer(user);login onlogin = new login(t);}}package cclient;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;public class registerfront extends JFrame implements ActionListener {JLabel id, name, password, passagain;JTextField namein, passwordin, passagainin, idin;JButton register;transfer t = null;public registerfront(transfer t) {this.t = t;this.setBounds(400, 200, 350, 240);this.setLayout(null);id = new JLabel("用户名");name = new JLabel("昵称");password = new JLabel("密码");passagain = new JLabel("重输密码");idin = new JTextField();namein = new JTextField();passwordin = new JTextField();passagainin = new JTextField();register = new JButton("注册");this.add(id);this.add(name);this.add(password);this.add(passagain);this.add(idin);this.add(namein);this.add(passwordin);this.add(passagainin);this.add(register);register.addActionListener(this);idin.setText("10位纯数字");passwordin.setText("10位以内字母或数字");namein.setText("10位以内英文");id.setBounds(20, 20, 60, 30);idin.setBounds(100, 20, 150, 30);password.setBounds(20, 60, 60, 30);passwordin.setBounds(100, 60, 150, 30);passagain.setBounds(20, 100, 60, 30);passagainin.setBounds(100, 100, 150, 30);register.setBounds(240, 150, 60, 40);name.setBounds(20, 150, 40, 30);namein.setBounds(100, 150, 100, 30);this.setTitle("注册界面");this.setResizable(false);this.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent arg0) {// TOO Auto-generated method stubString id, password, passagain, name;Boolean flag = false;StringBuffer tip = new StringBuffer("");String matchid = "^[0-9]{10}$", matchpass = "^[A-Za-z0-9]{1,10}$", matchname = "^[A-Za-z0-9]{1,10}$";id = idin.getText();password = passwordin.getText();passagain = passagainin.getText();name = namein.getText();Pattern p1 = Pattern.compile(matchid);Pattern p2 = Pattern.compile(matchpass);Pattern p3 = Pattern.compile(matchname);Matcher p11 = p1.matcher(id);Matcher p22 = p2.matcher(password);Matcher p33 = p3.matcher(name);if (p11.matches() != true) {tip.append("用户名格式错误+");flag = true;}if (p22.matches() != true) {flag = true;flag = true;tip.append("密码格式错误+");}if (p33.matches() != true) {flag = true;tip.append("昵称格式错误+");}if (password.equals(passagain) != true) {tip.append("两次密码输入不一致");flag=true;}if (flag)JOptionPane.showMessageDialog(null, tip, "系统提示",JOptionPane.ERROR_MESSAGE);else {t.register(id , password , name );t.getmessage();if (t.waitecho() == true) {JOptionPane.showMessageDialog(null, "注册成功", "系统提示",JOptionPane.INFORMATION_MESSAGE);this.dispose();} elseJOptionPane.showMessageDialog(null, "注册失败,用户名已存在", "系统提示",JOptionPane.ERROR_MESSAGE);}}}package cclient;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import javax.swing.JOptionPane;import cclient.login.communicating;import cclient.login.mainwindow;public class transfer {client user;public transfer(client user) {this.user = user;}public void register(String id, String password, String name) {System.out.println("sendbegin");user.out.write(order.register);user.out.write(id + "\n");user.out.write(password + "\n");user.out.write(name + "\n");user.out.write(order.register);user.out.flush();System.out.println("sendover");}public void login(String id, String password) {user.out.write(order.login);user.out.write(id + "\n");user.out.write(password + "\n");user.out.write(order.login);user.out.flush();}public void modifyname(String name) {user.out.write(order.modifyname);user.out.write(name);user.out.write(order.modifyname);user.out.flush();}public void addfriend(String id) {user.out.write(order.addfriend);user.out.write(id + "\n");user.out.write(order.addfriend);user.out.flush();}public void quit() {user.out.write(order.quit);user.out.write(order.quit);user.out.flush();}public void request(String id) {user.out.write(order.request);user.out.flush();}public void refresh() {user.out.write(order.refresh);user.out.write(order.refresh);user.out.flush();}public String getmessage() {try {user.get = user.in.readLine();while (user.get == null) {user.get = user.in.readLine();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return user.get;}public void orderparse(mainwindow m) {System.out.println("orderparsing");if (user.get.equals(getorder.send))getrefresh(m);else if (user.get.equals(getorder.talk))talk(m);}public void talk(mainwindow m) {System.out.println("gettalkingg");Date date = new Date();SimpleDateFormat sf = new SimpleDateFormat("kk:mm:ss");String time = sf.format(date);String id, name, text;id = getmessage();System.out.println(id);name = getmessage();System.out.println(name);text = getmessage();System.out.println(text);getmessage();int i = 0;for (i = 0; i < 8; i++) {if (m.window[i] != null) {if (m.window[i].obid.equals(id)) {m.window[i].output.append(name + " " + time + ":" + text+ "\n");break;}} else if (m.window[i] == null) {m.window[i] = new communicating(id, name, this);System.out.println("chuangjianlea"+i);m.window[i].output.append(name + " " + time + ":" + text + "\n");break;}}}private void getrefresh(mainwindow m) {// TODO Auto-generated method stubgetclient();m.setfriendlist(user.friend);}public synchronized void sendtalk(String id, String name, String text) {user.out.write(order.talk);user.out.write(id + "\n");System.out.println(id + "fasong");user.out.write(name + "\n");user.out.write(text + "\n");user.out.write(order.talk);user.out.flush();}public Boolean waitecho() {if (user.get.equals("1"))return true;elsereturn false;}public void getclient() {int i = 0, f = 0;while (getorder.send.equals(getmessage()) == true);user.id = user.get;System.out.println(user.get);user.name = getmessage();System.out.println(user.get);user.friendnumber = getmessage();System.out.println(user.get);i = Integer.parseInt(user.friendnumber);while (f < i) {getmessage();user.friend[f] = user.get;System.out.println(user.get);f++;}getmessage();}}package sserver;import java.io.BufferedReader;import java.io.PrintWriter;import java.net.Socket;class order {final static String register = "00000\n";final static String login = "00001\n";final static String addfriend = "00010\n";final static String refresh = "00011\n";final static String request = "00100\n";final static String quit = "00101\n";final static String modifyname = "00110\n";final static String talk = "00111\n";final static String send = "01000\n";}class getorder {final static String register = "00000";final static String login = "00001";final static String addfriend = "00010";final static String refresh = "00011";final static String request = "00100";final static String quit = "00101";final static String modifyname = "00110";final static String talk = "00111";final static String send = "01000";}class reducedclient {String id = null;String name = "路人甲";String friendnumber = "1";String online = "offline";}public class client extends reducedclient {String ip = "0.0.0.0";String password = "";String send = null;String get = null;Socket s = null;String friend[];BufferedReader in = null;PrintWriter out = null;public client(String id) {this.id = id;friend = new String[50];}public client() {friend = new String[50];}public void clone(client temp) {// TODO Auto-generated method stubthis.id = temp.id;this.name = temp.name;this.password = temp.password;}public synchronized void modifyfriendnum(client f) {friend[Integer.parseInt(friendnumber)]=f.id+" "+f.name+" "+"offline";this.friendnumber = String.valueOf(Integer.parseInt(this.friendnumber) + 1);}}package sserver;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.Socket;import javax.swing.JOptionPane;public class clientservice extends Thread {static Boolean running = true;int thisthread;client user;dbservice d = null;fileservice f = null;Boolean orderparsing = false;Boolean ontalking = false;clientservice[] cs;mc m;public clientservice(Socket s, clientservice[] cs, mc m, int thisthread) {this.thisthread = thisthread;this.m = m;this.cs = cs;user = new client();this.d = d;this.user.s = s;f = new fileservice();d = new dbservice();try {user.in = new BufferedReader(new InputStreamReader(user.s.getInputStream()));user.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((user.s.getOutputStream()))), true);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void run() {// TODO Auto-generated method stubwhile (running) {getmessage();orderparse();}}public void orderparse() {m.tip("orderparse-running");if (user.get.equals(getorder.register))getregister();else if (user.get.equals(getorder.login))getlogin();else if (user.get.equals(getorder.addfriend))getaddfriend();else if (user.get.equals(getorder.refresh))getrefresh();else if (user.get.equals(getorder.talk))communication();else if (user.get.equals(getorder.quit))getquit();}public void getquit() {m.tip("getquit-runnging-" + user.id);try {m.num--;m.show.setText(String.valueOf(m.num));user.in.close();user.out.close();user.s.close();this.running=false;cs[thisthread].finalize();} catch (Throwable e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void getrefresh() {m.tip("getrefresh-runging");getmessage();onlinecheck();sendclient();}public void communication() {m.tip("communicaiton-running-" + user.id);int n;String id = getmessage();String name = getmessage();String text = getmessage();getmessage();n = getfriendthread(id);if (n != -1)cs[n].exchange(user.id,user.name,text);}public synchronized void exchange( String id,String name,String text) {m.tip("exchange-running");user.out.write(order.talk);user.out.write(id+ "\n");user.out.write(name+ "\n");user.out.write(text + "\n");user.out.write(order.talk);user.out.flush();m.tip("exchangerunning-over");}public void echo(Boolean flag) {if (flag == true){user.out.write("1\n");user.out.flush();m.tip("echo-running-true");}else{user.out.write("0\n");user.out.flush();m.tip("echo-running-false");}}public String getmessage() {if (user.in != null)try {user.get = user.in.readLine();while (user.get == null) {System.out.println("failed to read" + user.get);user.get = user.in.readLine();}m.tip("getmessage-running: " + user.get);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return user.get;}public void getregister() {m.tip("getregister-running-" + user.id);String id, password, name;id = getmessage();password = getmessage();name = getmessage();getmessage();user.id = id;user.password = password;user.name = name;user.friendnumber = "1";if (!d.checkdb(id)) {if (d.insertuser(user)) {if (f.create(user))if (f.addfriend(user, user))echo(true);} elseecho(false);} elseecho(false);}public void getlogin() {m.tip("getlgoin-running-" + user.id);String id, password;client temp = new client();id = getmessage();password = getmessage();getmessage();user.id = id;user.password = password;if (d.checkdb(id)) {temp = d.inquery(id);if (user.password.equals(temp.password)) {user.name = temp.name;user.friendnumber = temp.friendnumber;f.getfriend(user);echo(true);sendclient();} elseecho(false);} elseecho(false);}public void sendclient() {user.out.write(order.send);user.out.write(order.send);user.out.write(user.id + "\n");user.out.write(user.name + "\n");user.out.write(user.friendnumber + "\n");String temp = null;int i = 0;while ((temp = user.friend[i]) != null) {user.out.write(temp + "\n");i++;}user.out.write(order.send);user.out.flush();m.tip("sendclient-running");}public void getaddfriend() {client temp = new client();String obid = getmessage();if (d.checkdb(obid))if (f.checkhasadded(user.id, obid) == false) {m.tip("addfriend-running-" + user.id);temp = d.inquery(obid);f.addfriend(user, temp);f.addfriend(temp, user);onlinemodify(user, temp);}getmessage();}public void onlinemodify(client user, client temp) {String num1;int num11, idindex;num1 = d.getitem(user.id, "friendnumber");num11 = Integer.parseInt(num1);user.friend[num11] = temp.id + " " + temp.name + " " + temp.online;user.modifyfriendnum(temp);d.modify(user.id, "friendnumber", String.valueOf(num11 + 1));num1 = d.getitem(temp.id, "friendnumber");num11 = Integer.parseInt(num1);d.modify(temp.id, "friendnumber", String.valueOf(num11 + 1));if ((idindex = minionlinecheck(temp.id)) != -1) {cs[idindex].user.modifyfriendnum(user);}m.tip("onlinemodify-running");}public int getfriendthread(String id) {m.tip("getfriendthread-running");int i;for (i = 0; i < 500; i++) {if (cs[i] != null)if (cs[i].user.id.equals(id))return i;}return -1;}public void onlinecheck() {int friendcount = Integer.parseInt(user.friendnumber);int i, j;for (j = 0; j < friendcount; j++) {user.friend[j] = user.friend[j].substring(0,user.friend[j].indexOf(' ', 12))+ " offline";for (i = 0; i < 500; i++) {if (cs[i] != null) {if (cs[i].user.id.equals(user.friend[j].substring(0, 10)))user.friend[j] = cs[i].user.id + " " + cs[i].user.name+ " " + "online";}}}m.tip("onlinecheck-running");}public int minionlinecheck(String id) {int i = 0;for (i = 0; i < 500; i++) {if (cs[i] != null)if (cs[i].user.id.equals(id)) {m.tip("minionlinecheck-running-online");return i;}}m.tip("minionlinecheck-running-offline");return -1;}}package sserver;import java.beans.Statement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import javax.sql.rowset.CachedRowSet;public class dbservice {String driver="com.mysql.jdbc.Driver";String dbname="jdbc:mysql://localhost:3306/test";    Connection con=null;//Statement stm=null;//ResultSet rs=null;//CachedRowSet cache=null;static int statementcount=0;public dbservice(){try {Class.forName(driver);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.print("error");}try {con=DriverManager.getConnection(dbname,"root","");} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.print("error");}}public client inquery(String id) {client result=new client();String sql="select*from user where id=";java.sql.Statement stm=null;ResultSet re=null;try { stm=con.createStatement();re=stm.executeQuery(sql+id);re.next();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {result.id=re.getString("id");result.name=re.getString("name");result.friendnumber=re.getString("friendnumber");result.online=re.getString("online");result.ip=re.getString("ip");result.password=re.getString("password");re.close();stm.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return result;}public boolean insertuser(client user) {try {PreparedStatement stm=con.prepareStatement("INSERT INTO user VALUES(?,?,?,?,?,?)");stm.setString(1, user.id);stm.setString(2, user.name);stm.setString(3, user.friendnumber);stm.setString(4, user.online);stm.setString(5, user.ip);stm.setString(6, user.password);stm.executeUpdate();stm.close();} catch (SQLException e) {e.printStackTrace();return false;// TODO Auto-generated catch block}return true;}public void modify(String id,String item,String value) {String sql="UPDATE user set "+item+"=? "+"where id=?";try {PreparedStatement stm=con.prepareStatement(sql);stm.setString(1,value);stm.setString(2, id);stm.executeUpdate();stm.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public Boolean checkdb(String id){ResultSet rs=null;java.sql.Statement stm=null;try {stm=con.createStatement();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}try{rs=stm.executeQuery("select*from user where id="+id);rs.next();rs.getString("id");return true;}catch(Exception e){return false;}}public String getitem(String id,String item){PreparedStatement stm;ResultSet rs=null;String temp=null;String sql="select*from user where id=?";try {stm=con.prepareStatement(sql);stm.setString(1, id);rs=stm.executeQuery();rs.next();temp=rs.getString(item);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return temp;}}package sserver;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;public class fileservice {String directory = "D:\\friend\\";public fileservice() {File f = new File("D:\\friend");if (f.exists()) {if (f.isDirectory() == false)f.mkdir();} elsef.mkdir();}public Boolean create(client user) {File f = new File(directory + user.id);if (f.exists())return false;else {try {f.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return true;}public Boolean addfriend(client user, client friend) {File f = new File(directory + user.id);if (f.exists()) {try {PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(directory + user.id, true)));// out.newLine();out.println(friend.id + " " + friend.name + " " + friend.online);out.flush();out.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return true;} elsereturn false;}public Boolean getfriend(client user) {File f = new File(directory + user.id);if (f.exists()) {BufferedReader in;try {in = new BufferedReader(new FileReader(directory + user.id));int i = 0;while ((user.friend[i++] = in.readLine()) != null);in.close();} catch (IOException e) {e.printStackTrace();return false;// TODO Auto-generated catch block}}return true;}public Boolean checkhasadded(String user, String friend) {String temp = null;BufferedReader in;try {in = new BufferedReader(new FileReader(directory + user));int i = 0;while ((temp = in.readLine()) != null) {if (friend.equals(temp.substring(0, 10))) {in.close();return true;}}in.close();;} catch (IOException e) {e.printStackTrace();return false;// TODO Auto-generated catch block}return false;}}package sserver;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.ScrollPaneLayout;public class mc extends Thread {waitsocket w;long errorcount = 0;static int count = 0;Socket sss;Boolean hasbooted = false;clientservice[] cs;ServerSocket s = null;dbservice d = null;static int num = 0;int portnum;public JTextArea show, errorinforshow;public void init() {d = new dbservice();cs = new clientservice[500];try {s = new ServerSocket(portnum);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();this.tip("端口被占");w.hasbooted = false;}}public mc(JTextArea show, JTextArea error, int portnum, waitsocket w) {this.w = w;this.show = show;this.errorinforshow = error;this.portnum = portnum;}public void run() {init();this.tip("监听已启动");while (true) {try {if ((sss = s.accept()) != null)System.out.println("succeeddcon");while (cs[count] != null) {count++;if (count > 500)count = 0;}cs[count] = new clientservice(sss, cs, this, count);cs[count].start();num++;count++;this.show.setText(String.valueOf(num));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}// TODO Auto-generated method stub}}public void tip(String temp) {errorcount++;this.errorinforshow.append(errorcount + ":" + temp + "\n");}} package sserver;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.ScrollPaneLayout;public class waitsocket extends JFrame implements ActionListener{JScrollPane errorinfor;ScrollPaneLayout layout;int portnum=8008;Boolean hasbooted=false;public JTextArea show,errorinforshow;JLabel tip;JButton boot,port;JTextField papapa;public  waitsocket(){super.setTitle("控制台");super.setBounds(400,100,400,500);super.setVisible(true);super.setLayout(null);papapa=new JTextField();errorinfor=new JScrollPane();layout=new ScrollPaneLayout();errorinforshow=new JTextArea();errorinfor.setLayout(layout);show=new JTextArea();errorinfor.setViewportView(errorinforshow);this.add(errorinfor);this.add(papapa);errorinfor.setBounds(10,170,365,250);tip=new JLabel("当前连接数:");port=new JButton("修改主机端口");boot=new JButton("启动服务器程序");super.add(tip);super.add(show);super.add(port);super.add(boot);papapa.setBounds(250, 20, 60, 30);papapa.setText(String.valueOf(this.portnum));port.setBounds(40,60,300,40);boot.setBounds(40,120,300,40);port.addActionListener(this);boot.addActionListener(this);this.setResizable(false);tip.setBounds(20,20,100,30);show.setBounds(100, 20, 100, 30);show.setText(String.valueOf(0));}public static void main(String[]args){waitsocket w=new waitsocket();}@Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource()==port){this.portnum=Integer.parseInt(JOptionPane.showInputDialog(null,"请重设监听端口号","8008"));this.papapa.setText(String.valueOf(this.portnum));this.errorinforshow.append("已修改端口"+String.valueOf(this.portnum)+"\n");}// TODO Auto-generated method stubelse if(e.getSource()==boot&&hasbooted==false){mc m=new mc(show,errorinforshow,portnum,this);m.start();this.hasbooted=true;}}}