| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 515 人关注过本帖
标题:[求助]帮改个聊天室程序
只看楼主 加入收藏
磨难岛
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-6-18
收藏
 问题点数:0 回复次数:1 
[求助]帮改个聊天室程序

   下面是我的代码,本来是没button2和textField1,我加上去了,但没加好,出现错误,能帮我修正下么????
聊天室的服务端:
package chatserver2;


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatRoomServer extends Frame implements Runnable{
Panel panel;
ScrollPane sPanel;
TextArea textArea;
TextField textField1;
Button button1,button2;


//net
ServerSocket serverSock;
public final static int DEFAULT_PORT=6666;//默认端口号
Thread chatAcceptThread;//启动接受连接的线程
BroadcastThread broadcastThread;//广播thread; run when server is listening
java.util.Vector clients;//记录连接的线程
java.util.Vector clientsInfor;//记录连接线程的信息
public static int index=0;

public ChatRoomServer(){
try{
jbInit();
}catch(Exception e){
e.printStackTrace();
}
serverListen();//服务器开始监听
}

private void jbInit(){//初始化界面
panel=new Panel();
sPanel=new ScrollPane();
textArea=new TextArea("server information:\n");
textField1=new TextField("input Message here and send to server");
button1=new Button("退出");
button2=new Button("发送");

sPanel.add(textArea);

button1.addActionListener(new java.awt.event.ActionListener(){//退出按钮注册
public void actionPerformed(ActionEvent e){
button1_actionPerformed(e);
}
});
panel.add(button1);

button2.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
button2_actionPerformed(e);
}
});
textField1.addKeyListener(new textField1_KeyAdapter(this));
panel.add(textField1);
panel.add(button2);

this.addWindowListener(new ChatFrame_WindowAdapter(this));//注册到继承了窗口适配器的类
this.setSize(600,600);
this.setLayout(new BorderLayout());
this.add(sPanel,BorderLayout.CENTER);
this.add(panel,BorderLayout.SOUTH);
this.show();

}
public void sendInformation(){
out.println(textField1.getText());
out.flush();

}
private void button1_actionPerformed(ActionEvent e){
exit();
}
private void button2_actionPerformed(ActionEvent e){//发送按钮
sendInformation();
}
public void processMsg(String str){//
textArea.append(str);
}

private void serverListen(){
try{
serverSock=new ServerSocket(DEFAULT_PORT);
}catch(IOException e){
processMsg(e.toString());
processMsg("server failed!\n");
}
processMsg("server listening on port:"+DEFAULT_PORT);
clients=new java.util.Vector();
clientsInfor=new java.util.Vector();

chatAcceptThread=new Thread(this);//启动接受连接的线程
chatAcceptThread.start();

broadcastThread=new BroadcastThread(this);//广播线程
broadcastThread.start();

//还有一个回收无用连接thread 的线程
}

public void run(){//接受连接并记录线程信息
int i=0;
try{
while(true){
Socket clientSock=serverSock.accept();
CommunicateThread ct=new CommunicateThread(clientSock,this,index);//创建线程保持连接
clients.add(ct);//record Communicate Thread;
i++;
index++;//version2
clientsInfor.add("Thread-"+i);
processMsg("Thread-"+i+"join in\n");

}
}catch(IOException e){
processMsg(e.toString());
}
}

public void exit(){

broadcastThread.broadcast("Server exit!");
try{
serverSock.close();
}catch(IOException ioe){}
finally{
System.exit(0);
}
}

public static void main(String[] args){
ChatRoomServer chat=new ChatRoomServer();
}
}
class CommunicateThread extends Thread{//保持连接线程
protected Socket clientSock;
protected BufferedReader in=null;
protected PrintWriter out;
ChatRoomServer chatFrame;
boolean isTrue=true;//run()
java.util.Vector inforStack;
int index2;//

public CommunicateThread(Socket Sock,ChatRoomServer cFrame,int index){
clientSock=Sock;
chatFrame=cFrame;
index2=index;
inforStack=new java.util.Vector();
try{
in=new BufferedReader(new InputStreamReader(clientSock.getInputStream()));
out=new PrintWriter(clientSock.getOutputStream());
}catch(IOException ei){
try{
clientSock.close();
}catch(IOException ei2){ }
chatFrame.processMsg(ei.toString());
return;
}
this.start();
}

public void run(){
String infor;
try{
while(isTrue){
infor=in.readLine();
if(infor.equals("Client exit!")){
writeInformation(infor);//把信息写到信息栈,以倍广播出去
stopRun();
}else if(infor!=null){
writeInformation(infor);
}//else break;
try{
Thread.sleep(100);//version2
}catch(InterruptedException ex){}
}
}catch(IOException e){ ;}
finally{
try{
in.close();
out.close();
clientSock.close();
chatFrame.clients.remove(index2);//在clients中清除本线程序
ChatRoomServer.index--;//
}catch(IOException ei){;}
}

}

public void writeInformation(String infor){//写信息栈
inforStack.add(infor);
}

private void stopRun(){//终止线程
isTrue=false;
}

public void sendInformation(String str){//发送信息
try{
out.println(str);
out.flush();
}catch(Exception e){;}
}
}
class BroadcastThread extends Thread{//广播线程
ChatRoomServer chatFrame2;
java.util.Vector chatClients;//连接线程信息
java.util.Vector msgStack;//信息栈
java.util.Vector clientMsg;//记录客户发送的信息
CommunicateThread comThread1;
CommunicateThread comThread2;
String string;//information in inforStack
String clientName;//client thread name
String broadcastInfor;//broadcast information=clientName+string;

public BroadcastThread(ChatRoomServer cFrame){
chatFrame2=cFrame;
chatClients=chatFrame2.clients;
clientMsg=chatFrame2.clientsInfor;
//this.start();
}

public void broadcast(String str){//广播
for(int k=0;k<chatClients.size();k++){//send to everyone分别调用每个连接线程,发送信息
comThread2=(CommunicateThread)chatClients.get(k);
comThread2.sendInformation(str);
}
}

public void run(){
try{
while(true){
for(int i=0;i<chatClients.size();i++){
comThread1=(CommunicateThread)chatClients.get(i);
msgStack=comThread1.inforStack;//得到每个连接的信息栈
clientName=(String)clientMsg.get(i);//客户名
//读取每个连接线程的信息栈并把信息发送出去
for(int j=0;j<msgStack.size();j++){
string=(String)msgStack.get(j);
broadcastInfor=clientName+"->"+string;
broadcast(broadcastInfor);
}
//clear the inforStack
msgStack.removeAllElements();//清除以发送的信息

}
try{
Thread.sleep(100);//version2
}catch(InterruptedException ex){}
}
}catch(Exception e){}
}

}
//处理窗口关闭事件的适配器
class ChatFrame_WindowAdapter extends java.awt.event.WindowAdapter{
ChatRoomServer chatFrame;
public ChatFrame_WindowAdapter(ChatRoomServer chatFrame){
this.chatFrame=chatFrame;
}
public void windowClosing(WindowEvent e){//exit program
chatFrame.exit();//reference to the method exit() in ChatRoomServer.
}
}

搜索更多相关主题的帖子: 聊天室 import java awt 
2007-06-20 17:57
磨难岛
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-6-18
收藏
得分:0 
button2和textField1加上去,能发送消息和写消息!!本人急用!!做好了通知我,我QQ:70548574
2007-06-20 17:59
快速回复:[求助]帮改个聊天室程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032193 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved