急求一个简易聊天室的代码

要求在MyEclipse里部署运行成功就可以了要求主要步骤的解释安装调试的解释成功运行给300分~~!!!... 要求在MyEclipse里部署运行成功就可以了
要求主要步骤的解释 安装调试的解释
成功运行给300分~~!!!
展开
 我来答
chensensenchen
2008-01-03 · TA获得超过227个赞
知道小有建树答主
回答量:301
采纳率:0%
帮助的人:144万
展开全部
这个应该可以的拉:

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class ChatClient extends Applet{
protected boolean loggedIn;//登入状态
protected Frame cp;//聊天室框架
protected static int PORTNUM=7777; //缺省端口号7777
protected int port;//实际端口号
protected Socket sock;
protected BufferedReader is;//用于从sock读取数据的BufferedReader
protected PrintWriter pw;//用于向sock写入数据的PrintWriter
protected TextField tf;//用于输入的TextField
protected TextArea ta;//用于显示对话的TextArea
protected Button lib;//登入按钮
protected Button lob;//登出的按钮
final static String TITLE ="Chatroom applet>>>>>>>>>>>>>>>>>>>>>>>>";
protected String paintMessage;//发表的消息
public ChatParameter Chat;
public void init(){
paintMessage="正在生成聊天窗口";
repaint();
cp=new Frame(TITLE);
cp.setLayout(new BorderLayout());
String portNum=getParameter("port");//呢个参数勿太明
port=PORTNUM;
if (portNum!=null) //书上是portNum==null,十分有问题
port=Integer.parseInt(portNum);
//CGI
ta=new TextArea(14,80);
ta.setEditable(false);//read only attribute
ta.setFont(new Font("Monospaced",Font.PLAIN,11));
cp.add(BorderLayout.NORTH,ta);
Panel p=new Panel();
Button b;
//for login button
p.add(lib=new Button("Login"));
lib.setEnabled(true);
lib.requestFocus();
lib.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
login();
lib.setEnabled(false);
lob.setEnabled(true);
tf.requestFocus();//将键盘输入锁定再右边的文本框中
}
});
//for logout button
p.add(lob=new Button ("Logout"));
lob.setEnabled(false);
lob.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
logout();
lib.setEnabled(true);
lob.setEnabled(false);
lib.requestFocus();
}
});
p.add(new Label ("输入消息:"));
tf=new TextField(40);
tf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(loggedIn){
//pw.println(Chat.CMD_BCAST+tf.getText());//Chat.CMD....是咩野来?
int j=tf.getText().indexOf(":");
if(j>0) pw.println(Chat.CMD_MESG+tf.getText());
else
pw.println(Chat.CMD_BCAST+tf.getText());
tf.setText("");//勿使用flush()?
}
}
});
p.add(tf);
cp.add(BorderLayout.SOUTH,p);
cp.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
//如果执行了setVisible或者dispose,关闭窗口
ChatClient.this.cp.setVisible(false);
ChatClient.this.cp.dispose();
logout();
}
});
cp.pack();//勿明白有咩用?
//将Frame cp放在中间
Dimension us=cp.getSize(),
them=Toolkit.getDefaultToolkit().getScreenSize();
int newX=(them.width-us.width)/2;
int newY=(them.height-us.height)/2;
cp.setLocation(newX,newY);
cp.setVisible(true);
paintMessage="Window should now be visible";
repaint();
}
//登录聊天室
public void login(){
if(loggedIn) return;
try{
sock=new Socket(getCodeBase().getHost(),port);
is=new BufferedReader(new InputStreamReader(sock.getInputStream()));
pw=new PrintWriter(sock.getOutputStream(),true);
}catch(IOException e){
showStatus("Can't get socket: "+e);
cp.add(new Label("Can't get socket: "+e));
return;}
//构造并且启动读入器,从服务器读取数据,输出到文本框中
//这里,长成一个线程来避免锁住资源(lockups)
new Thread (new Runnable(){
public void run(){
String line;
try{
while(loggedIn &&((line=is.readLine())!=null))
ta.appendText(line+"\n");
}catch(IOException e){
showStatus("我的天啊,掉线了也!!!!");
return;
}
}
}).start();
//假定登录(其实只是打印相关信息,并没有真正登录)
// pw.println(Chat.CMD_LOGIN+"AppletUser");
pw.println(Chat.CMD_LOGIN+"AppletUser");
loggedIn =true;

}
//模仿退出的代码
public void logout(){
if(!loggedIn)
return;
loggedIn=false;
try{
if(sock!=null)
sock.close();
}catch(IOException ign){
// 异常处理哦
}
}
//没有设置stop的方法,即使从浏览器跳到另外一个网页的时候
//聊天程序还可以继续运行
public void paint(Graphics g){
Dimension d=getSize();
int h=d.height;
int w=d.width;
g.fillRect(0,0,w,2);
g.setColor(Color.black);
g.drawString(paintMessage,10,(h/2)-5);
}
}

聊天室服务器端
import java.net.*;
import java.io.*;
import java.util.*;

public class ChatServer{
//聊天室管理员ID
protected final static String CHATMASTER_ID="ChatMaster";
//系统信息的分隔符
protected final static String SEP=": ";
//服务器的Socket
protected ServerSocket servSock;
//当前客户端列表
protected ArrayList clients;
//调试标记
protected boolean DEBUG=false;
public ChatParameter Chat;

//主方法构造一个ChatServer,没有返回值
public static void main(String[] argv){
System.out.println("Chat server0.1 starting>>>>>>>>>>>>>>>>");
ChatServer w=new ChatServer();
w.runServer();
System.out.println("***ERROR*** Chat server0.1 quitting");

}

//构造和运行一个聊天服务
ChatServer(){
Chat=new ChatParameter();
clients=new ArrayList();
try{
servSock=new ServerSocket(7777);//实有问题拉,不过可能是他自己定义既一个class.
System.out.println("Chat Server0.1 listening on port:"+7777);
}catch(Exception e){
log("IO Exception in ChatServer.<init>");
System.exit(0);
}
}

public void runServer(){
try{
while(true){
Socket us=servSock.accept();
String hostName=us.getInetAddress().getHostName();
System.out.println("Accpeted from "+hostName);
//一个处理的线程
ChatHandler cl=new ChatHandler(us,hostName);
synchronized(clients){
clients.add(cl);
cl.start();
if(clients.size()==1)
cl.send(CHATMASTER_ID,"Welcome!You are the first one here");
else{
cl.send(CHATMASTER_ID,"Welcome!You are the latest of"+
clients.size()+" users.");
}
}
}
}catch(Exception e){
log("IO Exception in runServer:"+e);
System.exit(0);
}
}

protected void log(String s){
System.out.println(s);
}

//处理会话的内部的类
protected class ChatHandler extends Thread {
//客户端scoket
protected Socket clientSock;
//读取socket的BufferedReader
protected BufferedReader is ;
//在socket 上发送信息行的PrintWriter
protected PrintWriter pw;
//客户端出主机
protected String clientIP;
//句柄
protected String login;

public ChatHandler (Socket sock,String clnt)throws IOException {
clientSock=sock;
clientIP=clnt;
is=new BufferedReader(
new InputStreamReader(sock.getInputStream()));
pw=new PrintWriter (sock.getOutputStream(),true);

}

//每一个ChatHandler是一个线程,下面的是他的run()方法
//用于处理会话
public void run(){
String line;

try{
while((line=is.readLine())!=null){
char c=line.charAt(0);//我顶你老母啊 ,果只Chat.CMD咩xx冇定义 扑啊///!!!
line=line.substring(1);
switch(c){
//case Chat.CMD_LOGIN:
case 'l':
if(!Chat.isValidLoginName(line)){
send(CHATMASTER_ID,"LOGIN"+line+"invalid");
log("LOGIN INVALID from:"+clientIP);
continue;
}

login=line;
broadcast(CHATMASTER_ID,login+" joins us,for a total of"+
clients.size()+" users");
break;

// case Chat.CMD_MESG:
case 'm':
if(login==null){
send(CHATMASTER_ID,"please login first");
continue;
}

int where =line.indexOf(Chat.SEPARATOR);
String recip=line.substring(0,where);
String mesg=line.substring (where+1);
log("MESG: "+login+"--->"+recip+": "+mesg);
ChatHandler cl=lookup(recip);
if(cl==null)
psend(CHATMASTER_ID,recip+"not logged in.");
else
cl.psend(login,mesg);

break;

//case Chat.CMD_QUIT:
case 'q':
broadcast(CHATMASTER_ID,"Goodbye to "+login+"@"+clientIP);
close();
return;//ChatHandler结束

// case Chat.CMD_BCAST:
case 'b':
if(login!=null)
broadcast(login,line);
else
log("B<L FROM"+clientIP);
break;

default:
log("Unknow cmd"+c+"from"+login+"@"+clientIP);
}
}
}catch(IOException e){
log("IO Exception :"+e);
}finally{
//sock 结束,我们完成了
//还不能发送再见的消息
//得有简单的基于命令的协议才行
System.out.println(login+SEP+"All Done");
synchronized(clients){
clients.remove(this);
if(clients.size()==0){
System.out.println(CHATMASTER_ID+SEP+
"I'm so lonely I could cry>>>>>");
}else if(clients.size()==1){
ChatHandler last=(ChatHandler)clients.get(0);
last.send(CHATMASTER_ID,"Hey,you are talking to yourself again");
}
else{
broadcast(CHATMASTER_ID,"There are now"+clients.size()+" users");
}
}
}
}
protected void close(){
if(clientSock==null){
log("close when not open");
return;
}
try{
clientSock.close();
clientSock=null;
}catch(IOException e){
log("Failure during close to "+clientIP);
}
}
//发送一条消息给用户
public void send(String sender,String mesg){
pw.println(sender+SEP+"*>"+mesg);
}
//发送私有的消息
protected void psend(String sender ,String msg){
send("<*"+sender+"*>",msg);
}
//发送一条消息给所有的用户
public void broadcast (String sender,String mesg){
System.out.println("Broadcasting"+sender+SEP+mesg);
for(int i=0;i<clients.size();i++){
ChatHandler sib=(ChatHandler)clients.get(i);
if(DEBUG)
System.out.println("Sending to"+sib);
sib.send(sender,mesg);
}
if(DEBUG) System.out.println("Done broadcast");
}
protected ChatHandler lookup(String nick){
synchronized(clients){
for(int i=0;i<clients.size();i++){
ChatHandler cl=(ChatHandler)clients.get(i);
if(cl.login.equals(nick))
return cl;
}
}
return null;
}
//将ChatHandler对象转换成一个字符串
public String toString(){
return "ChatHandler["+login+"]";
}
}
}

public class ChatParameter {
public static final char CMD_BCAST='b';
public static final char CMD_LOGIN='l';
public static final char CMD_MESG='m';
public static final char CMD_QUIT='q';
public static final char SEPARATOR=':';//?????
public static final int PORTNUM=7777;
public boolean isValidLoginName(String line){
if (line.equals("CHATMASTER_ID"))
return false;
return true;
}
public void main(String[] argv){
}
}
记得加分啊!!!
百度网友88fd03b
2008-01-01 · TA获得超过3532个赞
知道小有建树答主
回答量:755
采纳率:0%
帮助的人:492万
展开全部
<html>
<head>
<title>小小聊天室</title>
</head>
<%
Session("user_name")=Request("user_name") '将用户昵称保存到session中

'下面几句返回来访者信息
Dim sayStr
sayStr="来自" & Request.ServerVariables("Remote_Addr")& "的" '来访者IP地址
sayStr=sayStr & "<b>" & Session("user_name") & "</b>" '来访者昵称
sayStr=sayStr & "于" & Time & "大驾光临" '来访时间
sayStr="<font color='red'>" & sayStr & "</font>" '红色显示

'下面几句将来访信息保存到Application中
Application.Lock '先锁定
Application("show")= sayStr & "<br>" & Application("show") '返回聊天信息
Application("user_online")=Application("user_online")+1 '在线人数加1 Application.Unlock '解除锁定
%>
<frameset rows="*,14%">
<frame name="main" src="main.asp" >
<frame name="input" src="input.asp">
<noframes>
<body scroll="no">
<p>此网页使用了框架,但您的浏览器不支持框架。</p>
</body>
</noframes>
</frameset>
</html>
**********************以上为CHAT.ASP****************
<%
'下面几句返回离开者信息
Dim sayStr
sayStr="来自" & Request.ServerVariables("Remote_Addr")& "的" '来访者IP地址
sayStr=sayStr & "<b>" & Session("user_name") & "</b>" '来访者昵称
sayStr=sayStr & "于" & Time & "高兴而去" '离开时间
sayStr="<font color='red'>" & sayStr & "</font>" '红色显示
'下面几句将来访信息保存到application中
Application.Lock '先锁定
Application("show")= sayStr & "<br>" & Application("show")
Application("user_online")=Application("user_online")-1 '在线人数减1
Application.Unlock '解除锁定
Response.Redirect "index.asp" '重定向回首页
%>
******以上为exit.asp****************
<html>
<head>
<title>聊天室</title>
<Script Language="JavaScript">
<!--
function check_Null(){
if (document.form1.user_name.value==""){
alert("昵称不能为空!");
return false;
}
return true;
}
// -->
</Script>
</head>
<body >
<h2 align="center">小小聊天室</h2>
<center>
现在共有<%=Application("user_online")%>人在线<p>
<form method="post" action="chat.asp" name="form1" onsubmit="JavaScript: return check_Null();">
请输入昵称<input type="text" name="user_name" size="10">
<input type="submit" value=" 进 入 ">
</form>
</center>
</body>
</html>
************以上为index.asp*******************
<html>
<head>
<title>发言区</title>
</head>
<body bgcolor="#FFFFCC" topmargin="0" leftmargin="0" onload="document.form1.say.focus();">
<form method="post" action="" name="form1">
发言:
<input type="text" name="say" size=40 >
<input type="submit" value="发言" >
<br>
颜色:
<select name="mycolor" style="font-size:12px">
<% If Request("mycolor")="#000000" Then %>
<option value="#000000" style="color:#000000" selected>黑色</option>
<% Else %>
<option value="#000000" style="color:#000000" >黑色</option>
<% End If %>
<% If Request("mycolor")="#FF0000" Then %>
<option value="#FF0000" style="color:#FF0000" selected>红色</option>
<% Else %>
<option value="#FF0000" style="color:#FF0000" >红色</option>
<% End If %>
<% If Request("mycolor")="#FFFF00" Then %>
<option value="#FFFF00" style="color:#FFFF00" selected>黄色</option>
<% Else %>
<option value="#FFFF00" style="color:#FFFF00" >黄色</option>
<% End If %>
<% If Request("mycolor")="#3333FF" Then %>
<option value="#3333FF" style="color:#3333FF" selected>蓝色</option>
<% Else %>
<option value="#3333FF" style="color:#3333FF" >蓝色</option>
<% End If %>
</select>
表情:
<select name="myemote" style="font-size:12px">
<option value="无" selected>无
<option value="回眸一笑">回眸一笑
<option value="幸灾乐祸">幸灾乐祸
<option value="号啕大哭">号啕大哭
<option value="拳打脚踢">拳打脚踢
<option value="气急败坏">气急败坏
<option value="温柔细语">温柔细语
<option value="得意非凡">得意非凡
<option value="大惊失色">大惊失色
</select>

<a href="exit.asp" target="_top">离开聊天室</a>
</form>
<%
IF Request("say")<>"" Then
'获取发言的有关信息
Dim say,mycolor,myemote,user_name '声明变量待用
user_name=Session("user_name") '返回用户昵称
say=Server.HtmlEncode(Request("say")) '返回发言,并用HtmlEncode编码
mycolor=Request("mycolor") '返回本次发言的颜色
myemote=Request("myemote") '返回本次发言表情
If myemote="无" Then
myemote=""
End If

'下面三句将得到本次发言的字符串
Dim sayStr
sayStr="<small>" & user_name & Time & myemote & "说:</small>"
sayStr=sayStr & "<font color='" & mycolor & "'>" & say & "</font>"

'下面几句将本次发言信息保存到Application中
Application.Lock '先锁定
Application("show")= sayStr & "<br>" & Application("show")
If Len(Application("show"))>10000 Then '如果发言总长度超过10000个字符,则截断为10000
Application("show")=Mid(Application("show"),1,10000)
End If
Application.Unlock '解除锁定
End If
%>
</body>
</html>
**************以上为input.asp***************
<html>
<head>
<title> 发言区</title>
<meta http-equiv="refresh" content="5">
</head>
<body>
<%
Response.Write Application("show") '显示聊天内容
%>
</body>
</html>
************以上为main.asp***************

参考资料: http://www.downcode.com/downcode/j_10922_6b6790b4.shtml

本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友36a72af79
2008-01-01 · 超过25用户采纳过TA的回答
知道答主
回答量:323
采纳率:0%
帮助的人:81.7万
展开全部
asp 的可以吗?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
weileniya
2008-01-01 · TA获得超过615个赞
知道小有建树答主
回答量:1030
采纳率:0%
帮助的人:585万
展开全部
好牛比啊!!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式