最近临近期末临近,各种考试接踵而至,最重要的一个专业可要考试,是做一个JAVA的一个聊天室项目,呵呵! 说实话刚刚看这个题目觉的很简单,但是到后面感觉越做越做不起来啊!因为我对线程,流的输入输出,我还有半懂半不懂的,虽然我自己也带了一个类似与项目组的成员,但是他们的不参于,或许即使参与也不会做,这些种种使我都有点要放弃的感觉,但是后来还是坚持,还做出来了!虽然这个大部份还是借鉴别人的程序,但是主题框架还是我自己的!毕竟还是第一次,慢慢来我一定行的!我下面把程序发出来希望大家给点意见,指出点错误来.
聊天室客户端:
public class ClientChatNow extends JApplet implements Runnable
{
//this This procedure produced leeyi
String msg;
String name;
Socket toServer;
PrintWriter streamToServer;
BufferedReader streamFromServer=null;
JPanel panel;
TextArea textMessage;
TextArea textSend;
TextField textFrom;
TextField textTo;
Label labelFrom;
Label labelTo;
Button bSend;
Button bAttach;
Button bCancel;
GridBagLayout gbl;
GridBagConstraints gbc;
boolean isTrue=true;
Thread readThread;
public void init()
{
put();
}
public void put()
{
panel=new JPanel();
gbl=new GridBagLayout();
gbc=new GridBagConstraints();
getContentPane().add(panel);
panel.setLayout(gbl);
panel.setBackground(Color.white);
// this.labelName=new Label("online Chat");
this.textMessage=new TextArea(20,45);
this.labelFrom=new Label("From");
this.textFrom=new TextField(10);
this.labelTo=new Label("To");
this.textTo=new TextField(10);
this.textSend=new TextArea(5,45);
this.bSend=new Button("Send");
this.bAttach=new Button("Attach");
this.bCancel=new Button("Canael");
/*gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=0;
this.gbl.setConstraints(this.labelName,gbc);
panel.add(this.labelName);
*/
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=3;
gbc.gridy=1;
this.gbl.setConstraints(this.textMessage,gbc);
panel.add(this.textMessage);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=3;
this.gbl.setConstraints(this.labelFrom,gbc);
panel.add(this.labelFrom);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=3;
gbc.gridy=3;
this.gbl.setConstraints(this.textFrom,gbc);
panel.add(this.textFrom);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=5;
this.gbl.setConstraints(this.labelTo,gbc);
panel.add(this.labelTo);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=3;
gbc.gridy=5;
this.gbl.setConstraints(this.textTo,gbc);
panel.add(this.textTo);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=3;
gbc.gridy=7;
this.gbl.setConstraints(this.textSend,gbc);
panel.add(this.textSend);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=9;
this.gbl.setConstraints(this.bSend,gbc);
this.bSend.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
send_actionPerformed(e);
}
});
panel.add(this.bSend);
//This procedure produced leeyi
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=2;
gbc.gridy=9;
this.gbl.setConstraints(this.bAttach,gbc);
panel.add(this.bAttach);
this.bAttach.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
bAttach_actionPerformed(e);
}
});
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=3;
gbc.gridy=9;
gbl.setConstraints(this.bCancel,gbc);
panel.add(this.bCancel);
this.bCancel.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
bCancel_actionPerformed(e);
}
});
}
public void start()
{
try
{
Socket toserver =new Socket("",1001);
if(toServer==null)
{
processMessage("连接成功!!!!!/n");
}
streamToServer=new PrintWriter(toserver.getOutputStream());
streamFromServer=new BufferedReader(new InputStreamReader(toserver.getInputStream()));
}
catch(IOException e)
{
processMessage(e.toString());
processMessage("很遗憾!!与服务器连接失败!!!!!!/n");
}
readThread=new Thread(this);
readThread.start();
}
public void run()
{
while(isTrue)
{
try
{
name=streamFromServer.readLine();
msg=streamFromServer.readLine();
if(msg!=null&&name!=null)
{
textMessage.append(name+":"+msg+"/n");
}
Thread.sleep(1000);
}
catch(IOException e)
{
processMessage(e.toString());
}
catch(InterruptedException ei)
{
processMessage(ei.toString());
}
}
}
private void send_actionPerformed(ActionEvent e)
{//连接按钮
sendName();
sendMessage();
}
private void bAttach_actionPerformed(ActionEvent e)
{
setNameTo();
}
private void bCancel_actionPerformed(ActionEvent e)
{//连接按钮
exit();
}
public void setNameTo()
{
this.textTo.setText(name);
}
public void sendMessage()
{
String message=textSend.getText();
streamToServer.println(message);
streamToServer.flush();
textSend.setText("");
}
public void sendName()
{
String name=this.textFrom.getText();
streamToServer.println(name);
streamToServer.flush();
}
public void processMessage(String msg)
{
this.textMessage.append(msg);
textMessage.append("/n");
}
public void exit()
{
try
{
this.streamToServer.println("Client exit!");
this.streamToServer.flush();
}
catch(Exception exc){}
try
{
toServer.close();
this.streamFromServer.close();
this.streamToServer.close();
}
catch(IOException ioe)
{}
finally
{
System.exit(0);
}
}
public void paint(Graphics g)
{
g.drawString("欢迎光临聊天室", 1,3);
}
}
聊天室服务器端:
public class AppServer implements Runnable
{
//李野制作
ServerSocket server;
Socket fromClient;
BroadcastThread broadcastThread;//广播线程
Thread chatAcceptThread;//聊天输入线程
Vector clients;//客户向量
Vector clientsInfor;//客户信息向量
static int index=0;
BufferedReader streamFromClient;
PrintWriter streamToClient;
int i=0;
public AppServer()
{
serverListen();
}
private void serverListen()//建立服务器监听
{
try
{
server=new ServerSocket(1001);//监听1001端口
System.out.println("服务器启动.........");
}
catch(IOException e)
{
System.out.println("服务器已启动...../n 请关闭原服务器..........");
}
clients=new Vector();
clientsInfor=new Vector();
chatAcceptThread=new Thread(this);
this.chatAcceptThread.start();
broadcastThread=new BroadcastThread(this);
this.broadcastThread.start();
}
public void run()
{
try
{
while(true)
{
fromClient=server.accept();//接受客户端连接
Connection con=new Connection(fromClient,this,index);
clients.add(con);//客户向量添加CON类
i++;
index++;
System.out.println("the Thread is"+i);
}
}
catch(IOException e)
{
System.out.println("不能接受客户的连接...........!");
}
}
public static void main(String a[])
{
new AppServer();
}
}
class Connection extends Thread
{
AppServer app2;
Socket fromClients;
PrintWriter streamToClient;//将流写入到客户端
BufferedReader streamFromClient=null;//从客户端接受流
boolean isTrue=true;
int index2;
String infor;
Vector inforStack;
public Connection(Socket fromClient,AppServer app,int index)
{
fromClients=fromClient;
app2=app;
index2=index;
inforStack=new Vector();
try
{
this.streamToClient=new PrintWriter(fromClients.getOutputStream());//建立输出到客户端的流输出
this.streamFromClient=new BufferedReader(new InputStreamReader(fromClients.getInputStream())); //建立接受客户端的流输入
}
catch(IOException e)
{
System.out.println("不能输入输出客户流........");
try
{
fromClients.close();//捕捉接受客户连接关闭
}
catch(IOException er)
{
}
return;
}
this.start();
}
public void run()
{
try
{
while(isTrue)
{
infor=this.streamFromClient.readLine();
if(infor!=null)
{
writeInformation(infor);//把信息写入践,以倍广播播出
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
}
}
}
catch(IOException e)
{
}
finally
{
try
{
this.streamFromClient.close();
this.streamToClient.close();
this.fromClients.close();
// app2.clients.remove(index2);
AppServer.index--;
}
catch(IOException e)
{
}
}
}
public void writeInformation(String infor)
{
this.inforStack.add(infor);//把信息写入践
}
public void stopRun()
{
isTrue=false;
}
public void sendInformation(String str)//发送信息
{
// try
// {
this.streamToClient.println(str);
this.streamToClient.flush();
// }
// catch(IOException es)
// {
// System.out.println("不能写入到客户端");
// }
}
}
class BroadcastThread extends Thread//广播线程
{
AppServer app2;
Vector chatClients;//连接线程信息
Vector msgStack;//信息践
Vector clientMsg;//记录客户发送的信息
Connection comThread1;//建立connection的类
Connection comThread2;
String string;//将信息写入inforstack践
String clientName;//客户线程的名字
String broadcastInfor;//广播信息=ClientName+String
public BroadcastThread(AppServer app)
{
app2=app;
chatClients=app2.clients;
}
public void broadcast(String str)//广播
{
for(int k=0;k<chatClients.size();k++)
{
comThread2=(Connection)chatClients.get(k);
comThread2.sendInformation(str);
}
}
public void run()
{
try
{
while(true)
{
for(int i=0;i<chatClients.size();i++)
{
comThread1=(Connection)chatClients.get(i);
msgStack=comThread1.inforStack;//得到每个连接的信息践
for(int j=0;j<msgStack.size();j++)//读取每个连接线程的信息践并把信息发送出去
{
string=(String)msgStack.get(j);//得到客户信息和message
broadcastInfor=string;
broadcast(broadcastInfor);
}
msgStack.removeAllElements();//在Inforstack中清除线程
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
}
}
}
catch(Exception e)
{
}
}
}