📄 chatserver.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.event.*;
class Mess implements Serializable
{
public String name,pwd;
};
class document implements Serializable
{
public String name,pwd/*,age,old,tel,num,sex,si*/;
};
public class chatServer extends JFrame implements Runnable
{
JPanel jp;
TextArea tao,tai;
JButton jb,jb_q;
ServerSocket sd,sz,sc;
Socket sk;
Date d;
String ob;
String user;
//Icon i,i2;
JLabel jl;
JTextField jtf;
Thread thr;
Vector v;
Vector ve;
JList jlist;
String lm;
String na;
//String vip,veip;
public chatServer()
{
super("服务器");
jp=new JPanel();
setContentPane(jp);
jp.setBackground(new Color(27,228,163));
setDefaultCloseOperation(EXIT_ON_CLOSE);
tao=new TextArea("",18,30,tao.SCROLLBARS_BOTH);
//tao.setEditable(false);
tao.setBackground(new Color(190,250,231));
tai=new TextArea("",7,30,tai.SCROLLBARS_BOTH);
//i=new ImageIcon("f:/04912111/project/logowin.gif");
//i2=new ImageIcon("f:/04912111/project/fs.gif");
jl=new JLabel("聊天记录");
jb=new JButton("发送");
jb.setMnemonic('l');
jb_q=new JButton("清空");
jb_q.setMnemonic('z');
jb_q.setBackground(new Color(123,221,133));
jb.setBackground(new Color(123,221,133));
jtf=new JTextField(20);
v=new Vector();
ve=new Vector();
jlist=new JList(v);
jp.add(jl);
jp.add(tao);
jp.add(tai);
jp.add(jb);jp.add(jb_q);jp.add(jtf);
jp.add(jlist);
setSize(250,600);
setVisible(true);
setLocation(400,100);
//setResizable(false);
try
{
sd=new ServerSocket(1001);
new yz();
sz=new ServerSocket(1002);
new zc();
sc=new ServerSocket(1003);
new chat();
}
catch(Exception e)
{
}
thr=new Thread(this);
thr.start();
}
public void run()
{
Date d;
try
{
while(true)
{
d=new Date();
// String time=d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
jtf.setText(""+" "+d);
thr.sleep(1000);
}
}
catch(Exception e){}
//thr.sleep(1000);
}
class yz extends Thread
{
Socket sk;
//String vip;/////////////+++++++
public yz()
{
this.start();
}
public void run()
{
try
{
while(true)
{
sk=sd.accept();
new connect(sk);
}
}
catch(Exception e)
{
}
}
class connect extends Thread
{
RandomAccessFile raf;
Socket sk;
Mess m=new Mess();
String n;
public connect(Socket s)
{
sk=s;
this.start();
}
public void run()
{
try
{
while(true)
{
//System.out.println("11");
ObjectInputStream oip=new ObjectInputStream(sk.getInputStream());
m=(Mess)oip.readObject();
//System.out.println("12");
// ob=m.name;
System.out.println("allright");
/*String*/ na=m.name;
String pw=m.pwd;
String n="<"+na+":"+pw+">";////这种格式写入data(注册时)
raf=new RandomAccessFile("data.txt","rw");
System.out.println("13");
raf.seek(0);
try
{
while(true)
{
String r=raf.readLine();
if(n.equals(r))
{
String re="s";
DataOutputStream dos=new DataOutputStream(sk.getOutputStream());
// vip=sk.getInetAddress().getHostAddress();//////////+++
dos.writeUTF(re);
v.addElement(na);
////////////////////////////new cv();//////////////+++++++++++++++++++++++++
JOptionPane.showMessageDialog(null,na+"进入房间");
jlist.setListData(v);//////////////llllistdata set$$$$$$
break;
}
if(r==null)
{
String re="f";
DataOutputStream dos=new DataOutputStream(sk.getOutputStream());
dos.writeUTF(re);
break;
}
System.out.println("14");
}
}
catch(Exception e){}
}
}
catch(Exception e)
{
// JOptionPane.showMessageDialog(null,na+"away");
}
}/////run's
};
};
class cv//////////////////+++++++++++++++++++++++
{
String vip;
Socket socv;
//String vip;
public cv(/*String vip*/)
{
//this.vip=vip;
try
{
int count=ve.size();
for(int i=0;i<=count;i++)
{
vip=ve.elementAt(i).toString();
socv=new Socket(vip,1005);
sendv();
System.out.println("v ok111");
}
}
catch(Exception e){}
}
public void sendv()
{
//ObjectOutputStream oos;
DataOutputStream dos;
String vto;
try
{
//oos=new ObjectOutputStream(socv.getOutputStream());
//oos.writeObject(v);
int co=v.size();
for(int i=0;i<=co;i++)
{
vto=v.elementAt(i).toString();
dos=new DataOutputStream(socv.getOutputStream());
dos.writeUTF(vto);
}
System.out.println("send v ok");
}
catch(Exception e){}
finally
{
try
{
socv.close();
}
catch(Exception ee)
{
}
}
}
};
class cve
{
Socket socve;
String veip;
public cve(/*String vip*/)
{
//this.vip=vip;
try
{
int count=ve.size();
for(int i=0;i<=count;i++)
{
veip=ve.elementAt(i).toString();
socve=new Socket(veip,1006);
sendve();
System.out.println("ve ok");
}
//System.out.println("ve ok");
}
catch(Exception e){}
}
public void sendve()
{
//ObjectOutputStream oos;
DataOutputStream dos;
String vto;
try
{
//oos=new ObjectOutputStream(socv.getOutputStream());
//oos.writeObject(v);
int co=ve.size();
for(int i=0;i<=co;i++)
{
vto=ve.elementAt(i).toString();
dos=new DataOutputStream(socve.getOutputStream());
dos.writeUTF(vto);
}
System.out.println("send ve ok");
}
catch(Exception e){}
finally
{
try
{
socve.close();
}
catch(Exception ee)
{
}
}
}
};
class zc extends Thread///////////////////////注册
{
Socket sk;
public zc()
{
this.start();
}
public void run()
{
try
{
while(true)
{
sk=sz.accept();
new connect(sk);
}
}
catch(Exception e)
{
}
}
class connect extends Thread
{
RandomAccessFile raf;
document doc=new document();
Socket sk;
String n;
public connect(Socket s)
{
sk=s;
this.start();
}
public void run()
{
try
{
//RandomAccessFile raf;
while(true)
{
ObjectInputStream ois=new ObjectInputStream(sk.getInputStream());
doc=(document)ois.readObject();
String na=doc.name;
String pw=doc.pwd;
String n="<"+na+":"+pw+">"+"\n";
String n2="<"+na+":"+pw+">";
raf=new RandomAccessFile("data.txt","rw");
raf.seek(0);
try
{
while(true)
{
String read=raf.readLine();
if(n2.equals(read))
{
System.out.println("200,zc err");
String re="regist error";
DataOutputStream dos=new DataOutputStream(sk.getOutputStream());
dos.writeUTF(re);
break;
}
if(read==null)
{
raf.writeBytes(n);
System.out.println("203");
DataOutputStream dos=new DataOutputStream(sk.getOutputStream());
dos.writeUTF("regist finish");
break;
}
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
}
}
}
catch(Exception e)
{
//DataOutputStream dos=new DataOutputStream(sk.getOutputstream());
//dos.writeBytes("error in connection");
}
}
};
};
class chat extends Thread implements ActionListener,ListSelectionListener
{
Socket sk,skv;
String mess,a,test,ip,deip,deve;
int b,dect;
//Vector ve;
boolean flag;
public chat()
{
//ve=new Vector();
jb.addActionListener(this);
jb_q.addActionListener(this);
jlist.addListSelectionListener(this);
this.start();
}
public void valueChanged(ListSelectionEvent lse)
{
a=jlist.getSelectedValue().toString();
b=jlist.getSelectedIndex();
JOptionPane.showMessageDialog(null,a+b);
int aaaaa=ve.size();
JOptionPane.showMessageDialog(null,""+b);
JOptionPane.showMessageDialog(null,""+aaaaa);
test=ve.elementAt(b).toString();
JOptionPane.showMessageDialog(null,test+"111");
ob=a;
try
{
skv=new Socket(test,1004);
System.out.println("test 1004 404");
}
catch(Exception e){}
}
public void actionPerformed(ActionEvent ae)
{
Object o=ae.getSource();
if(o==jb)
{
send();
tai.setText("");
tai.requestFocus();
}
if(o==jb_q)
{
tao.setText("");
tai.requestFocus();
}
}
public void run()
{
try
{
while(true)
{
sk=sc.accept();
connect c=new connect(sk);
}
}
catch(Exception e)
{
}
}
class connect extends Thread
{
Socket sk;
public connect(Socket s)
{
System.out.println("connect 433");
sk=s;
ip=sk.getInetAddress().getHostAddress();
ve.addElement(ip);//////////////////$$$$$$$$$$$$$$$
new cv();////////////++++++++++
new cve();//////////////////////+++++++++++++++++ ok
int size=ve.size();
System.out.println(size);
this.start();
}
public void run()
{
try
{
System.out.println("run 447");
while(true)
{
d=new Date();
int coun=ve.size();
String seev;
int ctt;
for( ctt=0;;){
seev=ve.elementAt(ctt).toString();
if(seev.equals(ip))
{
//seev=v.elementAt(ctt).toString();
break;
}
else continue;
}
seev=v.elementAt(ctt).toString();
String ft=seev+" "+"to Server";
// String ft=ob+" "+"to Server";
String time=""+ft+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
DataInputStream dis=new DataInputStream(sk.getInputStream());
mess=dis.readUTF();
if(mess.equals("输入错误"))
{
deip=sk.getInetAddress().getHostAddress();
int count=ve.size();
for(int ct=0;ct<=count;ct++)
{
deve=ve.elementAt(ct).toString();
if(deip.equals(deve))
{
dect=ct;
if(ob.equals(v.elementAt(dect).toString()))
{
ob=null;
}
break;
}
else
continue;
}
String ay=v.elementAt(dect).toString();
JOptionPane.showMessageDialog(null,ay+"离开");
ve.removeElementAt(dect);
v.removeElementAt(dect);
new cv();////////////++++++++++
new cve();//////////////////+++++++++++
jlist.setListData(v);/////////////////listDataset$$$$$$$$$$
}
else
tao.append(time+"\n"+mess+"\n");
}
}
catch(Exception e)
{}
finally
{
try
{
sk.close();
System.out.println("test 1004 500");
}
catch(Exception e){}
}
}
};
public void send()
{
DataOutputStream dos;
String to=tai.getText().trim();
if(to.equals(""))
{
//tai.requestFocus();
JOptionPane.showMessageDialog(null,"输入字符不能为空");
}
else
{
try
{
d=new Date();
String ft="Server to"+" "+ob;
String time=""+ft+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
if(ob==null)
{
JOptionPane.showMessageDialog(null,"请选择对象");
}
else
tao.append(time+"\n"+to+"\n");
System.out.println("419");
dos=new DataOutputStream(skv.getOutputStream());
dos.writeUTF(to);
System.out.println("422");
}
catch(Exception e)
{
System.out.println("11"+e.getMessage());
}
}
}
};
public static void main(String[] args)
{
new chatServer();
System.out.println("Hello World!");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -