一个简易的java聊天室代码

客户端程序:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

class Mywindow extends Frame implements ActionListener
{TextField text1,text2;
Button button1,button2;
Mywindow()
{super("发悄悄话窗口");
setLayout(new GridLayout(3,2));
text1=new TextField(12);
text2=new TextField(8);
button1=new Button("送出悄悄话");
button2=new Button("关闭此窗口");
add(new Label("送悄悄话到:"));
add(text1);
add(new Label("输入你的悄悄话"));
add(text2);
add(button2);
add(button1);
setSize(400,190);
text1.setEditable(false);
setVisible(false);
button1.addActionListener(this);
button2.addActionListener(this);
setBackground(Color.pink);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button1)x
{try{chatappletthree.out.writeUTF("悄悄的对"+text1.getText()+"说:"+text2.getText()+"(我是"+chatappletthree.baocun+")");}
catch(IOException e1){}
}
else if(e.getSource()==button2)
{this.setVisible(false);}
}
}
class Apanel extends Panel
{TextField name_txt; Button button1,button2;
Checkbox box1,box2,box3;
CheckboxGroup sex;
Apanel()
{name_txt=new TextField(10);
button1=new Button("进入聊天室");
button2=new Button("退出聊天室");
setLayout(new FlowLayout());
sex=new CheckboxGroup();
box1=new Checkbox("男M",false,sex);
box2=new Checkbox("女F",false,sex);
box3=new Checkbox("蘑菇",false,sex);
add(new Label("输入昵称"));add(name_txt);
add(box1);
add(box2);
add(box3);
add(button1);
add(button2);
add(new Label("制作:风,(jin@263.net)"));

}
}
class Bpanel extends Panel

{TextArea chat_txt;
B2panel b2;
Bpanel()
{chat_txt=new TextArea(20,75);
b2=new B2panel();
chat_txt.setEditable(false);
setLayout(new FlowLayout());
add(chat_txt);
add(b2);

}
}
class B2panel extends Panel
{java.awt.List list;
B2panel()
{try{ list=new java.awt.List(25,false);}
catch(NullPointerException e){}
setLayout(new BorderLayout());
add("Center",list);
add("North",new Label("聊天者列表:"));
add("East",new Label());
add("South",new Label("双击某昵称可悄悄话"));

}
}
class Cpanel extends Panel
{TextField msg_txt;Button button,button2,button3;
Cpanel()
{msg_txt=new TextField(66);
button=new Button("送出消息");
button2=new Button("刷新谈话区");
button3=new Button("刷新聊天者列表区");
setLayout(new FlowLayout());
add(new Label("你要说的话:"));
add(msg_txt);
add(button);
add(button2);
add(button3);

}
}
public class chatappletthree extends Applet implements Runnable,ActionListener,ItemListener

{public static final int PORT=8765; static String baocun,xingbie;
Socket socket;
int jilu,enter=1;
DataInputStream in;
static DataOutputStream out;
Thread thread;
String line;
static Apanel a;
static Bpanel b;
static Cpanel c;
static Mywindow mywindow;
public void init()
{mywindow=new Mywindow();
setBackground(new Color(113,163,139));
setLayout(new BorderLayout());
a=new Apanel();
b=new Bpanel();
c=new Cpanel();
add("Norrh",a);
add("Center",b);
add("South",c);
a.button1.addActionListener(this);
a.button2.addActionListener(this);
c.button.addActionListener(this);
c.button2.addActionListener(this);
c.button3.addActionListener(this);
a.box1.addItemListener(this);
a.box2.addItemListener(this);
a.box3.addItemListener(this);
b.b2.list.addActionListener(this);
add("East",new Label());
add("West",new Label());
jilu=0;
this.setForeground(Color.black);
c.msg_txt.setBackground(Color.yellow);
b.chat_txt.setBackground(new Color(200,185,220));

}
public void start()
{try{
socket=new Socket(this.getCodeBase().getHost(),PORT);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch(IOException e)
{ this.showStatus(e.toString());
say("欢迎来这里!");
System.exit(1);
}
say("欢迎来红蜘蛛聊天室");
if(thread==null){
thread=new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void stop()
{try{
socket.close();
}
catch(IOException e){
this.showStatus(e.toString());
}
if((thread!=null)&&thread.isAlive())
{thread.yield();thread=null;
}
}

public void destroy()
{try{out.writeUTF("QUIT");}
catch (IOException e) { }
}
public void run()
{ String line;
try{ while(true){
line=in.readUTF();
if(line.startsWith("PEOPLE"))
{
b.b2.list.add(line.substring(6));
b.chat_txt.append(line.substring(6)+"爬上红蜘蛛网??"+′/n′);

}
else if(line.startsWith("MSG"))
{b.chat_txt.append(line.substring(3)+′/n′);
}
else if(line.startsWith("QUIT"))
{
String str=line.substring(10);
try{ for(int i=0,k=0;i<=120;i++)
{String s=b.b2.list.getItem(i);
if(s.equals(str))
{
k=i;b.b2.list.remove(k);
}
b.chat_txt.append(line.substring(10)+"??高兴的离开了红蜘蛛网"+′/n′);

}

}
catch(ArrayIndexOutOfBoundsException e){ }

}
else if(line.startsWith("悄悄的对"))
{String ti=null;
ti=line.substring(line.indexOf(′:′));
if(ti.startsWith(":zhangpgxygsrok")&&(!(ti.endsWith("*"))))
{this.out.writeUTF("BADQUIT");
b.chat_txt.append("@@@@请注意!!!!:"+baocun+"["+xingbie+"]"+","+"你被踢出红蜘蛛网,注意言行哦!@@@@"+′/n′);
}
else
{b.chat_txt.append(line+′/n′);
}

}
}
} catch(IOException e)
{say("再见!欢迎再来红蜘蛛聊天室,如果想重新进入本聊天室,单击浏览器的刷新选项");}
catch(NullPointerException e) { }
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==c.button)

{if((baocun!=null))
{String ti=c.msg_txt.getText();
if(ti.equals("zhangpgxygsrok"))
{try{out.writeUTF("坏悄悄的对:"+c.msg_txt.getText());
}
catch(IOException e1) { }
}
else
{ try{out.writeUTF("MSG"+baocun+"["+xingbie+"]"+"说(Speaking)??"+":"+c.msg_txt.getText());
c.msg_txt.setText(null);}
catch (IOException e1){ }
}
}
}
else if(e.getSource()==c.button2)
{b.chat_txt.setText(null);
}
else if(e.getSource()==c.button3)
{try{b.b2.list.removeAll();
out.writeUTF("newlist");}
catch(IOException e1){}
}
else if(e.getSource()==a.button1)
{{baocun=new String(a.name_txt.getText());}
try{
for(int i=0;i<=120;i++){
if((a.name_txt.getText()!=null)&&((a.name_txt.getText()+"["+xingbie+"]").equals(b.b2.list.getItem(i))||a.name_txt.getText().equals("该名已被使用")))
{jilu=1;baocun=null;break;}

}
}
catch(ArrayIndexOutOfBoundsException e3){}
if(jilu==0)
{try{out.writeUTF("PEOPLE"+a.name_txt.getText()+"["+xingbie+"]");
}
catch(IOException e1) {}
}
else if(jilu==1)
{a.name_txt.setText("该名已被使用");
}
jilu=0;
}
else if(e.getSource()==a.button2)
{try{out.writeUTF("QUIT");
}
catch(IOException e1){}
b.b2.list.removeAll();
}
else if(e.getSource()==b.b2.list)
{mywindow.setVisible(true);
mywindow.text1.setText(((List)e.getSource()).getSelectedItem());
}

}
public void itemStateChanged(ItemEvent e1)
{if(e1.getItemSelectable()==a.box1)
{xingbie="男";}
else if(e1.getItemSelectable()==a.box2)
{xingbie=new String("女");}
else if(e1.getItemSelectable()==a.box3)
{xingbie=new String("蘑菇");}
else if((a.box1.getState()==true)&&((a.box2.getState()==true)&&(a.box3.getState()==true)))
{xingbie="单细胞";}
}
public void say(String msg)
{b.chat_txt.append("☆☆☆☆☆"+msg+"☆☆☆☆☆/n");
}
}

服务器端程序:
import java.net.*;
import java.io.*;
import java.util.*;

public class chatserverthree implements Runnable

{public static final int PORT=8765;
protected ServerSocket listen;
static Vector connections;
Thread connect;
public chatserverthree()
{try{
listen=new ServerSocket(PORT);

}
catch(IOException e){
System.err.println("erro:"+e);System.exit(1);

}
connections=new Vector(1000);connect=new Thread(this);
connect.start();

}
public void run()
{
try
{
while(true)
{Socket client=listen.accept();
firstthread f=new firstthread(this,client);
f.setPriority(Thread.MIN_PRIORITY);f.start();
connections.addElement(f);

}
}

catch (IOException e)
{System.err.println("Erro:"+e);
System.exit(1);
}
}
public static void main(String[] args)
{new chatserverthree();}
public void broadcast(String msg)
{int i; firstthread you;
for(i=0;i<connections.size();i++)
{you=(firstthread)connections.elementAt(i);
try

{you.out.writeUTF(msg);

}
catch (IOException e)
{
}


}
}
public void broadcast1(String msg)
{int i; String s1,s2,s3;
firstthread you;
s1=new String("PEOPLE");
s2=new String(msg.substring(4));
s3=s1.concat(s2);
for (i=0;i<connections.size() ;i++ )
{you=(firstthread) connections.elementAt(i);
if (s3.startsWith(you.name))
{try
{
you.out.writeUTF(msg);
}
catch (IOException e)
{
}
}
}
}
}
class firstthread extends Thread
{protected Socket client;
String line,name;
int k=0;
protected DataOutputStream firstout,out;
protected chatserverthree server;
protected DataInputStream in;
public firstthread(chatserverthree server,Socket client)
{this.server=server;this.client=client;
try
{
in=new DataInputStream(client.getInputStream());
out=new DataOutputStream(client.getOutputStream());
firstout=new DataOutputStream(client.getOutputStream());
}
catch (IOException e)
{try
{
server.connections.removeElement(this);
client.close();
}
catch (IOException e2)
{System.err.println("有问题哦:"+e);
return ;
}
if (this.client==null)
{server.broadcast("QUIT"+this.name);this.name=null;
}
}
}
public void run()
{
try
{
for (int i=0;i<server.connections.size() ;i++ )
{firstthread c=(firstthread)(server.connections.elementAt(i));
if (c.name!=null)
{try
{
firstout.writeUTF(c.name);
}
catch (IOException e)
{
}
}
}
}
catch (ArrayIndexOutOfBoundsException e)
{
}
catch(NullPointerException e)
{}
try
{
while (true)
{line=in.readUTF();
if (line.startsWith("PEOPLE"))
{try
{
firstthread d=(firstthread)(server.connections.elementAt(server.connections.indexOf(this)));
if (d.name!=null)
{d.name=line;
}
else if (d.name!=null)
{server.broadcast("QUIT"+this.name);
d.name=line;
}
}
catch (ArrayIndexOutOfBoundsException e)
{
}
catch (NullPointerException e) {}
finally {server.broadcast(line);}
}
else if (line.startsWith("MSG"))
{server.broadcast(line);
}
else if (line.startsWith("QUIT"))
{server.broadcast("QUIT"+this.name);
server.connections.removeElement(this);
this.client.close();this.yield();

}
else if (line.startsWith("BADQUIT"))
{server.broadcast("MSG"+(this.name).substring(6)+"被踢出去了");
server.broadcast("QUIT"+this.name);
server.connections.removeElement(this);
this.client.close();this.yield();
}
else if (line.startsWith("newlist"))
{try
{
for (int i=0;i<server.connections.size() ;i++ )
{firstthread c=(firstthread)(server.connections.elementAt(i));
if (c.name!=null)
{try
{
firstout.writeUTF(c.name);
}
catch (IOException e)
{
}
}
}
}
catch (ArrayIndexOutOfBoundsException e)
{
}
catch(NullPointerException e) { }
}
else if (line.startsWith("悄悄的对"))
{this.out.writeUTF(line+"*");
server.broadcast(line);
}
else if (line.startsWith("坏悄悄的对"))
{this.out.writeUTF(line.substring(1)+"*");
server.broadcast(line.substring(1));
}
}
}
catch (IOException e)
{server.connections.removeElement(this);
}
catch(NullPointerException e) { server.connections.removeElement(this);}
}
}

各位请都顶一下哦!小弟谢了!

Java聊天室程序 需求分析 2.1 业务需求 1. 与聊天室成员一起聊天。 2. 可以与聊天室成员私聊。 3. 可以改变聊天内容风格。 4. 用户注册(含头像)、登录。 5. 服务器监控聊天内容。 6. 服务器过滤非法内容。 7. 服务器发送通知。 8. 服务器踢人。 9. 保存服务器日志。 10.保存用户聊天信息。 2.2 系统功能模块 2.2.1 服务器端 1.处理用户注册 2.处理用户登录 3.处理用户发送信息 4.处理用户得到信息 5.处理用户退出 2.2.2 客户端 1.用户注册界面及结果 2.用户登录界面及结果 3.用户发送信息界面及结果 4.用户得到信息界面及结果 5.用户退出界面及结果 2.3 性能需求 运行环境:Windows 9x、2000、xp、2003,Linux 必要环境:JDK 1.5 以上 硬件环境:CPU 400MHz以上,内存64MB以上 3.1.2 客户端结构 ChatClient.java 为客户端程序启动类,负责客户端的启动和退出。 Login.java 为客户端程序登录界面,负责用户帐号信息的验证与反馈。 Register.java 为客户端程序注册界面,负责用户帐号信息的注册验证与反馈。 ChatRoom.java 为客户端程序聊天室主界面,负责接收、发送聊天内容与服务器端的Connection.java 亲密合作。 Windowclose 为ChatRoom.java的内部类,负责监听聊天室界面的操作,当用户退出时返回给服务器信息。 Clock.java 为客户端程序的一个小程序,实现的一个石英钟功能。 3. 2 系统实现原理 当用户聊天时,将当前用户名、聊天对象、聊天内容、聊天语气和是否私聊进行封装,然后与服务器建立Socket连接,再用对象输出流包装Socket的输出流将聊天信息对象发送给服务器端 当用户发送聊天信息时,服务端将会收到客户端用Socket传输过来的聊天信息对象,然后将其强制转换为Chat对象,并将本次用户的聊天信息对象添加到聊天对象集Message中,以供所有聊天用户访问。 接收用户的聊天信息是由多线程技术实现的,因为客户端必须时时关注更新服务器上是否有最新消息,在本程序中设定的是3秒刷新服务器一次,如果间隔时间太短将会增加客户端与服务器端的通信负担,而间隔时间长就会让人感觉没有时效性,所以经过权衡后认为3秒最佳,因为每个用户都不可能在3秒内连续发送信息。 当每次用户接收到聊天信息后将会开始分析聊天信息然后将适合自己的信息人性化地显示在聊天信息界面上。 4.1.1 问题陈述 1.接受用户注册信息并保存一个基于文件的对象型数据库。 2.能够允许注册过的用户登陆聊天界面并可以聊天。 3.能够接受私聊信息并发送给特定的用户。 4.服务器运行在自定义的端口上#1001。 5.服务器监控用户列表和用户聊天信息(私聊除外)。 6.服务器踢人,发送通知。 7.服务器保存日志。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值