📄 frame1.java~58~
字号:
package chatserver;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.io.*;
import java.util.Vector;
import java.util.StringTokenizer;
import java.lang.Thread;
/**
* <p>Title: chat</p>
* <p>Description: chat application(for mywife lihuan)</p>
* <p>Copyright: Copyright (c) CAUC 2005</p>
* <p>Company: CSTA</p>
* @author zhangbin
* @version 1.0
*/
public class Frame1 extends JFrame {
JPanel XYLayout;
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JPanel jPanel2 = new JPanel();
JButton send = new JButton();
JButton link = new JButton();
JButton sysexit = new JButton();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JPanel jPanel3 = new JPanel();
PaneLayout paneLayout1 = new PaneLayout();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
List list1 = new List();
JLabel jLabel1 = new JLabel();
BorderLayout borderLayout2 = new BorderLayout();
JPanel jPanel4 = new JPanel();
JLabel jLabel5 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel6 = new JLabel();
XYLayout xYLayout2 = new XYLayout();
JScrollPane jScrollPane2 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
static ServerSocket serverSocket=null;//建立服务器的socket
static Socket socket=null; //用来储荐一个套接字
BufferedReader cin=null;//用来接收一个客户端发来的数据流
PrintWriter cout=null;//用来实现向户端发送信息流
static Vector clients=new Vector(20);//用Vertor向量数组存储连接客户变量
static int active_connects=0;//记录目前连接的客户数
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
XYLayout = (JPanel) this.getContentPane();
XYLayout.setLayout(xYLayout1);
this.setContentPane(XYLayout);
this.setSize(new Dimension(400, 300));
this.setTitle("chartServer");
XYLayout.setBackground(Color.white);
XYLayout.setForeground(Color.white);
jPanel1.setPreferredSize(new Dimension(250, 150));
jPanel1.setLayout(borderLayout1);
send.setText("发送");
send.addActionListener(new Frame1_send_actionAdapter(this));
link.setText("启动");
link.addActionListener(new Frame1_link_actionAdapter(this));
sysexit.setText("断开");
sysexit.addActionListener(new Frame1_sysexit_actionAdapter(this));
jTextField2.addActionListener(new Frame1_jTextField2_actionAdapter(this));
jPanel3.setLayout(paneLayout1);
jLabel2.setText("连接情况");
jLabel3.setText("IP地址");
jLabel1.setText("用户各");
jPanel2.setLayout(borderLayout2);
jPanel4.setLayout(xYLayout2);
jLabel5.setText("消息类型");
jLabel4.setText("信息内容");
jLabel6.setText("信息来源");
jTextField1.setText("");
jTextArea1.setEditable(false);
jTextArea1.setText("");
jTextField2.setText("");
XYLayout.add(jPanel1, new XYConstraints(3, 0, 395, 101));
jPanel1.add(jScrollPane1, BorderLayout.CENTER);
jPanel1.add(jPanel3, BorderLayout.NORTH);
jScrollPane1.getViewport().add(list1, null);
jPanel3.add(jLabel2, new PaneConstraints("jLabel2", "jLabel2", PaneConstraints.ROOT, 0.5f));
jPanel3.add(jLabel1, new PaneConstraints("jLabel1", "jLabel2", PaneConstraints.LEFT, 0.3106796f));
jPanel3.add(jLabel3, new PaneConstraints("jLabel3", "jLabel2", PaneConstraints.LEFT, 0.4514768f));
XYLayout.add(jPanel2, new XYConstraints(3, 105, 393, 110));
jPanel2.add(jPanel4, BorderLayout.CENTER);
jPanel4.add(jLabel5, new XYConstraints(0, 0, 127, 22));
jPanel4.add(jLabel4, new XYConstraints(251, 0, 142, 22));
jPanel4.add(jLabel6, new XYConstraints(127, 0, 103, 21));
jPanel4.add(jScrollPane2, new XYConstraints(-1, 22, 393, 81));
jScrollPane2.getViewport().add(jTextArea1, null);
XYLayout.add(sysexit, new XYConstraints(321, 247, 70, 21));
XYLayout.add(link, new XYConstraints(242, 249, 74, 19));
XYLayout.add(jTextField2, new XYConstraints(1, 247, 234, -1));
XYLayout.add(jTextField1, new XYConstraints(2, 220, 311, -1));
XYLayout.add(send, new XYConstraints(322, 220, 67, 19));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
//系统启动
void link_actionPerformed(ActionEvent e) {
if(serverSocket==null){
try{
serverSocket=new ServerSocket(4000);
jTextArea1.append("系统提示:服务器开始启动~~~~~,请等待! \n");
}catch(IOException ee){
jTextArea1.append("系统提示:服务器"+ee.toString() +"\n");
}
}//end if
if(serverSocket != null){
jTextArea1.append("系统提示:服务器已经启动!\n");
Thread a=new watch();
a.start();
}
}
//服务器端发送消息
void send_actionPerformed(ActionEvent e) {
StringBuffer info=new StringBuffer("MSG");
info.append(":"+"BROAD:"+"SERVER say::");
info.append(jTextField1.getText());
sendClients(info);
jTextField1.setText("");
}
//群发
public static synchronized void sendClients(StringBuffer msg){
for(int i=0;i<clients.size();i++){
Client c=(Client)clients.elementAt(i);
c.send(msg);
}
}
//单发
public static synchronized void sendtoclient(StringBuffer msg,String name){
for(int i=0;i<clients.size();i++){
Client c=(Client)clients.elementAt(i);
if(c.name.equalsIgnoreCase(name)){
c.send(msg);
break;
}//end if
}//end for
}
//一个用户名只能登陆一次
/*public static boolean checkName(Client newClient){
for(int i=0;i<clients.size();i++){
Client c=(Client)clients.elementAt(i);
if(( c!=newClient) &&c.equals(newClient.name)){
return false;
}//end if
return true;
}//end for
}*/
//监听用户,刷新用户列表
public static void notifyRoom(){
StringBuffer people=new StringBuffer("PEOPLE");
for(int i=0;i<clients.size();i++){
Client c = (Client) clients.elementAt(i);
people.append(":"+c.name);
}
sendClients(people);
}
//断开用户连接
public synchronized void disconnect(Client c){
try {
jTextArea1.append("系统消息:聊天服务器"+c.name +"断开连接!"+"\n");
active_connects--;
c.send(new StringBuffer("QUIT"+":"+c.name));
clients.removeElement(c);
jTextField2.setText("目前已有"+clients.size()+"用户连接");
c.socket.close();
}catch(IOException e){
jTextArea1.append("系统消息:error!"+e.toString());
}//end try
}
//服务器端断开
void sysexit_actionPerformed(ActionEvent e) {
sendClients(new StringBuffer("QUIT"+":SERVER"));
closeAll();
System.exit(0);
}//end ysexit_actionPerformed
//服务器端强行关闭所有连接
public void closeAll(){
while(clients.size()>0){
Client c=(Client)clients.firstElement();
try{
c.socket.close();
}catch(IOException e){
jTextArea1.append("系统消息:服务器error!"+e.toString());
}
finally{
clients.removeElement(c);
}
}
}
/*
*启动一个对于登录用户监听的线程
**/
class watch extends Thread{
public void run(){
int i=0;
while(true){
if(clients.size()<20){
try{
socket=serverSocket.accept();
}catch(IOException ee){
jTextArea1.append("系统消息:聊天服务器"+ee.toString()+"用户连接失败");
}//end try
do{
Client c = new Client(socket);
clients.addElement(c);
if( true){//checkName(c)){
int num=active_connects+1;
active_connects++;
jTextField2.setText("目前已有"+num+"用户连接!");
Client listdata=(Client)clients.elementAt(i);
//在list1中添加用户登陆信息
list1.add(listdata.name+" "+listdata.ip+" "+"已经连接",i);
c.start();
notifyRoom();
}else{
c.ps.println("FAULTNAME");
disconnect(c);
}
i++;
break;
}while(i<clients.size());//end do-while
}//end if
else{
try{
Thread.sleep(400);
}catch(InterruptedException e){}
}
}//end while
}//end run
}///:~end class watch
/*
*客户连接的client类,处理客户端要发送的消息
*/
class Client extends Thread{
String name;//用户存储客房的连接姓名
String ip;//用户的连接ip
//DataInputStream dis;//客户端发送来的数据流
//PrintStream ps;//客户端发送信息的打印流
BufferedReader dis;
PrintWriter ps;//客户端发送信息的打印流
Socket socket;//存储连接的套接字
public Client(Socket s){
socket=s;
try{
dis = new BufferedReader(new InputStreamReader(s.getInputStream()));
ps = new PrintWriter(s.getOutputStream());
String info = dis.readLine();
StringTokenizer stinfo = new StringTokenizer(info, ":");
String head = stinfo.nextToken();
if (stinfo.hasMoreTokens()) {
name = stinfo.nextToken();
} //end if
if (stinfo.hasMoreTokens()) {
ip = stinfo.nextToken();
} //end if
jTextArea1.append("系统消息:" + name + "/" + ip + " 已经连接!"+"\n");
}//end try
catch(IOException ee){
jTextArea1.append("系统消息:服务器error!"+ee.toString());
}
}
public void run(){
while(true){
String line=null;
try{
line=dis.readLine();
}catch(IOException e){
jTextArea1.append("系统消息:聊天服务器"+e.toString());
disconnect(this);
notifyRoom();
return;
}//end try
if(line==null){
disconnect(this);
notifyRoom();
return;
}
StringTokenizer st=new StringTokenizer(line,":");
String keyword=st.nextToken();
String broadcast=st.nextToken();
//判断是不是客户端发送的消息
if(keyword.equalsIgnoreCase("MSG")){
StringBuffer message = new StringBuffer("MSG:");
message.append(broadcast + ":");
message.append(st.nextToken("\0"));
if (broadcast.equalsIgnoreCase("BROAD")) { //客户发送的消息类型是群发
sendClients(message);
}
else { //客户发送的消息类型是单发
sendtoclient(message, broadcast);
}
}//end if
else if(keyword.equalsIgnoreCase("QUIT")){
list1.clear();
disconnect(this);
for(int i=0;i<clients.size();i++){
Client listdata1= (Client) clients.elementAt(i);
list1.add(listdata1.name+" "+listdata1.ip+" 已经连接",i);
}//end for
notifyRoom();
this.stop();
}//end else
}//end while
}
//向客户端以打印流的方式发送消息
public void send(StringBuffer msg){
ps.println(msg);
ps.flush();
}
}///:~
}
class Frame1_jTextField2_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jTextField2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
//adaptee.jTextField2_actionPerformed(e);
}
}
class Frame1_link_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_link_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.link_actionPerformed(e);
}
}
class Frame1_send_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_send_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.send_actionPerformed(e);
}
}
class Frame1_sysexit_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_sysexit_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.sysexit_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -