(把前面的一大段废话删了,因为后来觉得写个QQ聊天也没什么大不了的,就直奔主题吧~)
(补个链接吧:http://download.csdn.net/detail/tangcaijun/4804760)
学java一个月了,写了这个小东东.....
仿QQ聊天软件 chat2_1版本更新内容
1、支持广播群聊功能,可提取电脑用户名作为区分名称
2、新增优化登录和登录ing界面(高仿QQ2012)
3、每台计算机上可支持多个客户端同时运行
4、暂不开放帐号申请,暂无好友、个人信息保存(些版本尚未添加数据库)
5、默认端口依然是: 8888端口(用此高端口可避免和一般软件相互占用端口造成登录失败)
6、优化服务端,新增系统消息显示,可时时监控服务运行状态
7、服务端新增聊天监控,可观察各客户端的聊天信息
8、把验证登录信息移至服务器端
注:此版本暂无连接数据库,所以统一登录QQ为724447804(登录窗口中有保存为默认帐号)密码:a
(这次聊天界面暂时没怎么优化,呵呵,嗯,先将就下吧.....下一版本有空再弄弄吧,这次主要是想和正在学着java的同学交流分享下编程的一些东西而已,希望高手见了,不要太见笑好了....)
登录窗口
登录中窗口
server服务器窗口
分享下代码,希望有兴趣的同志可以看看这东西,这次的2_1版本中还有很多弊端,但聊天框基本上都写了的
这次代码中还有很多不成熟的地方,希望能得到一些高手们的建议吧,先谢谢给评论的同志们啦.....呵呵~~~
//---------------------------------------client客户端------------------------------------------------------//
//---------- 1、login.java -----------------------
package chat_2;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
* 广石化计算机11-3班
* choldrim(财俊)
*
* @author 神奇物种
*
*/
@SuppressWarnings("serial")
public class login extends JFrame{
public static Main_1 m = null;
private JLabel applyLabel, retrpwdLabel; //申请帐号 找回密码
private JTextField qq_tf; //帐号输入框
private JPasswordField pwd_pwf; //密码输入框
private JCheckBox recdpwd_chkb, autologin_chkb; //记住密码, 自动登录
private JLabel portrait_lab; //头像
public static int port = 8888; //端口
public static String ip = "127.0.0.1";
//界面设置
public void login_interface() {
this.setTitle("Chat_2020");
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
Panel p1 = new Panel();
p1.setLayout(null);
//头像
portrait_lab = new JLabel();
portrait_lab.setIcon(new ImageIcon("pic//portrait.png"));
portrait_lab.setBounds(20, 5, 82, 82);
p1.add(portrait_lab);
//qq and password 输入
qq_tf= new JTextField("724447804");
qq_tf.setBounds(110, 10, 160, 20);
qq_tf.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loginAction(e);
}
});
p1.add(qq_tf);
pwd_pwf= new JPasswordField();
pwd_pwf.setBounds(110, 40, 160, 20);
pwd_pwf.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loginAction(e);
}
});
p1.add(pwd_pwf);
//申请帐号
applyLabel = new JLabel();
applyLabel.setText("注册帐号");
applyLabel.setBounds(290, -8, 50, 50);
applyLabel.setFont(new Font("宋体", Font.PLAIN, 12));
applyLabel.setForeground(Color.blue);
applyLabel.addMouseListener(new mouse());
applyLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
p1.add(applyLabel);
//找回密码
retrpwdLabel = new JLabel();
retrpwdLabel.setText("找回密码");
retrpwdLabel.setBounds(290, 25, 80, 50);
retrpwdLabel.setFont(new Font("宋体", Font.PLAIN, 12));
retrpwdLabel.setForeground(Color.blue);
retrpwdLabel.addMouseListener(new mouse());
retrpwdLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
p1.add(retrpwdLabel);
//记住密码
recdpwd_chkb = new JCheckBox();
recdpwd_chkb.setText("记住密码");
recdpwd_chkb.setBounds(115, 65, 80, 20);
p1.add(recdpwd_chkb);
//自动登录
autologin_chkb = new JCheckBox();
autologin_chkb.setText("自动登录");
autologin_chkb.setBounds(195, 65, 80, 20);
p1.add(autologin_chkb);
//多帐号
JButton moreidButton = new JButton();
moreidButton.setIcon(new ImageIcon("pic//moreIdButton.PNG"));
moreidButton.setBounds(15, 100, 70, 25);
p1.add(moreidButton);
//设置
JButton setButton = new JButton("设置");
//setButton.setIcon(new ImageIcon("pic//moreIdButton.PNG"));
setButton.setBounds(100, 100, 70, 25);
setButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//new SetPort();
//或用:
ip = JOptionPane.showInputDialog("请输入服务器 ip");
port = Integer.parseInt(JOptionPane.showInputDialog("请输入端口"));
}
});
p1.add(setButton);
//登录
JButton loginButton = new JButton();
loginButton.setIcon(new ImageIcon("pic//loginbutton.png"));
loginButton.setBounds(280, 100, 70, 25);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loginAction(e);
}
});
p1.add(loginButton);
//北部图片
JLabel mainJLabel = new JLabel();
mainJLabel.setIcon(new ImageIcon("pic//qq2020.PNG"));
add(mainJLabel, BorderLayout.NORTH);
//图标
Image logoImage = Toolkit.getDefaultToolkit().getImage("pic//qqlogo.jpg");
setIconImage(logoImage);
//大小
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(screensize.width/3, screensize.height/3);
setSize(379, 280);
//添加中部panel
cp.add(p1, BorderLayout.CENTER);
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
//label点击事件
private class mouse extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
JLabel labeltype = (JLabel)e.getSource();
if(labeltype == applyLabel){
System.out.println("apply for an account");
}
super.mouseClicked(e);
}
}
//登录事件
protected void loginAction(ActionEvent e) {
if(qq_tf.getText().trim().equals("") || qq_tf.getText().trim().length()<6 || qq_tf.getText().trim().length()>12){
JOptionPane.showMessageDialog(null, "请输入正确的账号", "提示", JOptionPane.INFORMATION_MESSAGE);
}
else if(pwd_pwf.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "请输入密码", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
//ChkLogin chkLogin = new ChkLogin(qq_tf.getText().trim(), pwd_pwf.getText().trim());
m = new Main_1(qq_tf.getText().trim(), pwd_pwf.getText().trim(), port, ip);
setVisible(false);
}
}
public static void main(String[] args) {
new login().login_interface();
}
}
//---------- 2、chklogin.java -----------------------
package chat_2;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
/**
* 广石化计算机11-3班
* choldrim(财俊)
* @author 神奇物种
*
*/
public class ChkLogin {
public static Socket clientSocket =null; //
public DataInputStream dis =null; //输入流
public DataOutputStream dos =null; //输出流
public ChkLogin(String qq, String pwd ){
try {
clientSocket = new Socket(Main_1.ip, Main_1.port);
dos = new DataOutputStream(clientSocket.getOutputStream());
dis = new DataInputStream(clientSocket.getInputStream());
dos.writeUTF(Main_1.qq+"&"+Main_1.pwd); //把QQ和pwd(密码)传给服务器,用&符号作为分界
Main_1.login_tag = dis.readInt(); //读取服务器返回的参数
} catch (UnknownHostException e) {
Main_1.login_tag = 0;
JOptionPane.showMessageDialog(null, "连接服务器失败,请检查端口和ip是否正确", "提示", JOptionPane.INFORMATION_MESSAGE);
disconnect();
System.exit(0);
}
catch (ConnectException e) {
Main_1.login_tag = 0;
JOptionPane.showMessageDialog(null, "连接服务器失败", "提示", JOptionPane.INFORMATION_MESSAGE);
disconnect();
System.exit(0);
}catch (IOException e) {
JOptionPane.showMessageDialog(null, "程序内部发生错误", "错误", JOptionPane.ERROR_MESSAGE);
disconnect();
e.printStackTrace();
}
}
protected void disconnect() {
if(clientSocket != null){
try {
dos.flush();
dos.close();
dis.close();
clientSocket.close();
} catch (IOException e1) {e1.printStackTrace();}
}
}
}
//-----------------3、main.java------------------
package chat_2;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* 广石化计算机11-3班
* choldrim(财俊)
* @author 神奇物种
*
*/
@SuppressWarnings("serial")
public class Main_1 extends JFrame{
public static String qq;
public static String pwd;
public static int port =8888;
public static String ip ="127.0.0.1";
public static int login_tag;//登录状态标志 0登录失败 1帐号或密码错误 2登录成功
private login lg =new login();
private ChkLogin chk =null;
public Main_1(){};
@SuppressWarnings("static-access")
public Main_1(String qq, String pwd, int port, String ip){
this.qq = qq;
this.pwd = pwd;
this.port = port;
this.ip = ip;
Container cp = getContentPane(); //内容层
JPanel p1 = new JPanel(); //组件面板
p1.setLayout(null);
//北部图片
JLabel top_pic = new JLabel();
top_pic.setIcon(new ImageIcon("pic//qq2020_main.png"));
cp.add(top_pic, BorderLayout.NORTH);
//显示帐号
JLabel qqLabel = new JLabel(qq);
JLabel lgingLabel = new JLabel("正在登录...");
qqLabel.setBounds((300-qq.length()*10)/2, 10, 100, 20);
qqLabel.setFont(new Font("宋体", Font.PLAIN, 15));
lgingLabel.setFont(new Font("宋体", Font.PLAIN, 13));
lgingLabel.setBounds(110, 25, 100, 20);
p1.add(lgingLabel);
p1.add(qqLabel);
//取消登录
JButton cancelButton = new JButton();
cancelButton.setIcon(new ImageIcon("pic/cancellogin.png"));
setTitle("取消");
cancelButton.setBounds(100, 50, 90, 30);
cancelButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
lg.m.setVisible(false);
new login().login_interface();
super.mouseClicked(e);
}
});
p1.add(cancelButton);
//登录动画
JLabel loginflash = new JLabel();
loginflash.setIcon(new ImageIcon("pic//Logining_Vip.gif"));
loginflash.setBounds(30, 90, 220, 150);
p1.add(loginflash);
//基本frame设置
add(p1, BorderLayout.CENTER);
Image logoImage = Toolkit.getDefaultToolkit().getImage("pic//qqlogo.jpg");
setTitle("chat_2020");
setIconImage(logoImage);
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Dimension scrnsize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(scrnsize.width*2/3, 10, 300, 620);
//关闭窗体时须关闭连接等等一些设置
addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
//.....
if(chk != null)chk.disconnect();
System.exit(0);
super.windowClosed(e);
}
});
setVisible(true);
new Thread(new loginconnect()).start();
// try {
// new Thread(new loginconnect()).join(20);
// } catch (InterruptedException e1) {
// JOptionPane.showMessageDialog(null, "连接服务器超时", "提示", JOptionPane.INFORMATION_MESSAGE);
// System.exit(0);
// }
};
//登录线程
class loginconnect implements Runnable{
@Override
public void run() {
new ChkLogin(qq, pwd);
//延迟2秒,用于显示登录界面。。。可不用这个。。
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(login_tag == 0){
JOptionPane.showMessageDialog(null, "登录失败", "提示", JOptionPane.INFORMATION_MESSAGE);
lg.m.setVisible(false);
new login().login_interface();
return;
}
if(login_tag == 1){
JOptionPane.showMessageDialog(null, "密码错误", "提示", JOptionPane.INFORMATION_MESSAGE);
lg.m.setVisible(false);
new login().login_interface();
return;
}
if (login_tag == 2) {
lg.m.setVisible(false);
new ChatClient().chatInterface();
System.out.println("登录成功。。。...");
}
}
}
//临时main方法
// public static void main(String[] args){
// new Main_1("123", "123", 8888, "127.0.0.1");
// }
}
// ----------------- 4、chatclient.java-----------------
package chat_2;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.JFrame;
/**
* 广石化计算机11-3班
* choldrim(财俊)
*
* chatClient和chatServer都是前阵子写的
* 可能编程习惯和命名上没有很统一,但还是必要的地方都已经注有注释了
*
* @author 神奇物种
*
*/
@SuppressWarnings("serial")
public class ChatClient extends JFrame{
Socket s =ChkLogin.clientSocket;
DataOutputStream dos =null;
DataInputStream dis = null;
TextField tf = new TextField();
TextArea ta =new TextArea();
private boolean isconneted = false;// is it connected to the server...
public void chatInterface() //create chat frame...
{
setTitle(Main_1.qq+"---广播群聊");
Toolkit tk = Toolkit.getDefaultToolkit();
setIconImage(tk.getImage("pic/qqlogo1.png"));
Dimension screensize = tk.getScreenSize();
setLocation(screensize.width/3, screensize.height/3);
add(tf,BorderLayout.CENTER);
add(ta,BorderLayout.NORTH);
ta.setEditable(false);
ta.setBackground(Color.white);
pack();
this.addWindowListener(new WindowAdapter(){
//@Override
public void windowClosing(WindowEvent we)
{
disconnet(); //don't forget this even you want to exit
System.exit(0);
}
});
//添加监听,enter发送信息
tf.addActionListener(new tfListener());
setVisible(true);
start();
RecvThread(); //add receive thread....(添加接收信息线程)
}
private void start() {
try {
isconneted = true;
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
} catch (ConnectException e) {
System.out.println("server is out of running...plz wait...");
System.exit(0);
}catch (IOException e) {
e.printStackTrace();
}
}
private void disconnet() {
try {
if(dos != null)dos.close();
if(s != null)s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class tfListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText().trim();
tf.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private void RecvThread() {
new Thread(new Runnable() {
public void run() {
try {
while (isconneted) {
String str = dis.readUTF();
ta.setText(ta.getText() + '\n' + str);
}
} catch (SocketException e) {
System.out.println("server has exited plz try again later...");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
// public static void main(String[] args) {
// new ChatClient().chatInterface();
// }
}
//-----------------------------------------------------------------server服务器端--------------------------------------------------//
//------------------5、server.java-------------------------//
package server;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
/**
* 广石化计算机11-3班
* cholrim(财俊)
* @author 神奇物种
*
*/
@SuppressWarnings("serial")
public class ChatServer extends JFrame{
private boolean started = false; //服务器是否已启动
private boolean bConneted = false; // is connected to client...
ServerSocket ss = null; //服务端套接字
ArrayList<Client> clients = new ArrayList<Client>(); //用来装client对像的容器
public static int login_tag; //登录状态标志 0无登录 1帐号或密码错误 2登录成功
private JTextPane jta =new JTextPane(); //消息框
private JTextField jtf_port =new JTextField(); //端口设置输入框
private JTextPane messagePane = new JTextPane();
public String qq;
public String pwd;
public int port = 8888;
//---------------------------------服务器界面----------------------------------------------
public void serverInterface(int port) {
this.port = port;
setTitle("qq2020_server");
setIconImage(Toolkit.getDefaultToolkit().getImage("pic/StdLoginBanner.png"));
Container cp = getContentPane();//获取内容面板
//消息
JPanel jp_msg = new JPanel(); //消息框面板
jp_msg.setLayout(new BorderLayout());
jta.setEditable(false);
jta.setText("系统消息:\n");
jp_msg.add(jta, BorderLayout.CENTER);
//设置
JPanel jp_set = new JPanel(new BorderLayout()); //设置面板
JPanel p1 = new JPanel(null);
JLabel jlb_setPort = new JLabel("设置监听端口为:");
jlb_setPort.setFont(new Font("幼圆", Font.BOLD, 16));
JButton jb_port = new JButton("确定");
jlb_setPort.setBounds(50, 10, 200, 50);
jtf_port.setBounds(190, 23, 50, 28);
jb_port.setBounds(250, 23, 60, 28);
p1.add(jlb_setPort);
p1.add(jtf_port);
p1.add(jb_port);
jp_set.add(p1, BorderLayout.CENTER);
JButton runButton = new JButton("启动服务器");//启动服务器按钮
runButton.addActionListener(new serverRuning());
jp_set.add(runButton, BorderLayout.SOUTH);
jtf_port.addActionListener(new changePort());
jb_port.addActionListener(new changePort());
//菜单栏
setJMenuBar(new menu());
JTabbedPane jtab = new JTabbedPane();
jtab.addTab("系统消息", jp_msg);
jtab.add("聊天信息", messagePane); //监听聊天信息
jtab.addTab("设置", jp_set);
cp.add(jtab, BorderLayout.CENTER);
//设置frame大小
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(scrSize.width/3, scrSize.height/3, 500, 400);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
System.exit(0);
};
});
start(port);
}
//---------------------------------启动服务器按钮-----------------------------------------
class serverRuning implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(started == true){
JOptionPane.showMessageDialog(null, "服务器已启动");
}else {
//start(port);
}
}
}
//---------------------------------改变端口的输入与按钮的监听器-------------------------------
class changePort implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(started == false){
port = Integer.parseInt(jtf_port.getText().trim());
}
else {
JOptionPane.showMessageDialog(null, "服务器启动后不能再更改端口");
}
}
}
//---------------------------------菜单栏---------------------------------------------------
class menu extends JMenuBar{
public menu(){
JMenu systemMenu = new JMenu("系统");
JMenu funcMenu = new JMenu("功能");
JMenu helpMenu = new JMenu("help");
JMenuItem aboutusMenuItem = new JMenuItem("关于神奇物种");
JMenuItem fileMenuItem = new JMenuItem("文件");
JMenuItem choiceMenuItem = new JMenuItem("选项");
JMenuItem exitMenuItem = new JMenuItem("退出");
JMenuItem skinMenuItem = new JMenuItem("皮肤");
systemMenu.add(fileMenuItem);
systemMenu.add(choiceMenuItem);
systemMenu.add(exitMenuItem);
funcMenu.add(skinMenuItem);
helpMenu.add(aboutusMenuItem);
add(systemMenu);
add(funcMenu);
add(helpMenu);
}
}
//---------------------------------启动服务器的主要方法---------------------------------------
public void start(int port) {
try {
ss = new ServerSocket(port);
started = true;
jta.setText(jta.getText()+" server has run....\n");
} catch (BindException e) {
JOptionPane.showMessageDialog(null, "服务器端口被占用,请关闭相关程序再启动服务器");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
while (started) {
try {
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
String str1 = dis.readUTF();
String str2[] = str1.split("&");
qq = str2[0];
pwd = str2[1];
new Chklogin_server(qq, pwd);
if(login_tag == 1){
dos.writeInt(login_tag);
dos.flush();
jta.setText(jta.getText()+" 该用户输入的帐号或密码错误...\n");
}
if (login_tag == 2) {
dos.writeInt(login_tag);
dos.flush();
jta.setText(jta.getText()+" 用户: "+s.getInetAddress().getLocalHost().getHostName()+" 验证通过..\n");
Client c = new Client(s);
new Thread(c).start(); // run the 线程
clients.add(c);
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if(ss != null)ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//---------------------------------接收和处理client的类-------------------------------------
class Client implements Runnable { // many线程处理
private DataOutputStream dos = null;
private DataInputStream dis = null;
private Socket s;
public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConneted = true;
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
String str;
try {
while (bConneted) {
str = dis.readUTF();
messagePane.setText(messagePane.getText()+s.getInetAddress().getLocalHost().getHostName()+" 说:\n "+str+"\n");
for(int i = 0;i<clients.size();i++){
Client c = clients.get(i);
c.send(str);
}
System.out.println(str);
}
} catch (EOFException e) {
System.out.println("a Client exit!");
try {
jta.setText(jta.getText()+" Client "+s.getInetAddress().getLocalHost().getHostName()+" exit!\n");
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null)
dis.close();
if (s != null)
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//转发聊天信息
private void send(String str) {
try {
dos.writeUTF(s.getInetAddress().getLocalHost().getHostName() +" : "+ str);
} catch (IOException e) {
clients.remove(this); //#@&^%%$## this 是指调用send的对像
jta.setText(jta.getText()+" a client exited when server was sending the message\n");
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new ChatServer().serverInterface(8888);
}
}
//----------------------------------6、chklogin_server.java--------------------------
package server;
/**
* 广石化计算机11-3班
* choldrim(财俊)
*
* @author 神奇物种
*
*/
public class Chklogin_server {
public Chklogin_server(String qq, String pwd){
/**
*
*
*可用连接数据库或记事本方式验证。。
*
*此处先只用简单的验证带过。。
*
*/
if(!qq.equals("724447804") || !pwd.equals("a")){
ChatServer.login_tag = 1;
return;
}
if(qq.equals("724447804") && pwd.equals("a")){
ChatServer.login_tag = 2;
return;
}
ChatServer.login_tag = 0;
}
}