简易聊天室,简易聊天室课程设计
怎么用VBscript制作一个简易聊天室?我姐姐 急要哇..
1、 打开Dreamweaver后,新建站点,选择新建的本地站点,右击创建文件夹,命名为比如chat,创建文件夹后,选择该文件夹,在该文件夹下新建两个ASP文件,分别命名为welcome.asp和chat.asp。2、 双击打开网页,选择welcome.asp网页,该网页制作为欢迎页面。在当前网页中输入文字,比如“欢迎进入聊天室”。可以选中“聊天室”将其字体颜色改为红色,字体大小也可以做适当的更改。将光标移置下一行,将插入栏切换于“表单”插入栏,插入表单。在“插入”菜单中选择插入一行三列的表格,表格的宽度我们设置为400像素,边框设为1,其它的默认。选择第一个单元格,在其中输入“呢称”,在第二单元格中,插入“文本字段”表单,第三单元格插入一个“提交”按钮。设置各表单的属性,首先将第一单元格中的字体居中,选择第二单元格中的“文本字段”,在属性面板中将其名称改为name,字段大小可以设为20。选择第三个单元格中的按钮,将“提交”改为“进入”选择“提交表单”动作(默认)。选择整个表单,在属性面板的动作中加入welcome.asp让其在判断错误后转回到当前页面。表单制作完成后,在绑定面板中“请求变量”,在类型中选择“Request.Form”,名称为“name”,点确定后,再请求“阶段变量”,将名称也设置为“name”,单击确定。将页面切换到代码视图,在顶端%@LANGUAGE=”VBSCRIPT” CODEPAGE=”936”%以下加入以下代码:% If Request.Form(“name”)”” thenSession(“name”)=Request.Form(“name”)Response.Redirect(“chat.asp”)%%End If %3、 打开chat.asp网页,将插入栏切换至表单视图,插入表单。在插入菜单中选择表格,插入一个一行三列的表格,表格宽度设为400像素,边框为1,其它默认。在表格的第二个单元格中插入一个“文本字段”表单。第三个单元格中插入一个“按钮”表单。将第一个单元格的宽度设为80。选择第二个单元格中的“文本字段”,在属性面板中设置名称为say。第三个单元格中的按钮选中,在属性面板中,其名称将“提交”改为“发言”。选中整个表单,在动作中输入chat.asp。制作完成后,选择“绑定面板”点击添加,选择“请求变量”,选择类型为Request.Form,名称为say。再添加“应用程序变量”,名称定义为chat。完成后,拖动相应的变量名称至网页相应的位置,如将Request.Form(”name”)拖动至表格的第一个单元格。将Application.chat拖动至表格的下一行的相应位置。将网页切换置代码视图,在顶端%@LANGUAGE=”VBSCRIPT” CODEPAGE=”936”%以下加入以下代码:%If Request.Form(“name”)=”” ThenResponse.Redirect(“welcome.asp”)%%End If % %If Request.Form(“say”)”” ThenApplication(“chat”)=session(“name”) “说:” Request.Form(“say”) “br” Application(“chat”)%%End If %4、右击选择“保存全部”将网页保存。选择welcome.asp网页,点击浏览。当我们不输任何内容,点击进入时,网页自动返回当前页,输入呢称后,点击进入将进入chat.asp页面。在文本字段中添加内容就可以在下面看到相应的发言内容。至此,简易的聊天室制作完毕。
linux C语言 TCP 多线程 简易聊天室
你accept得到一个新的连接后,再创建线程(把连接socket传给线程),用这个线程专门接收这个连接的数据,就不会有问题了。
急求一个简易聊天室的代码
这个应该可以的拉:
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(j0) 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("BL 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;iclients.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;iclients.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){
}
}
记得加分啊!!!
asp 实现一个简易的网上聊天室
用session实现,不过,你用户名不存数据库,你存哪时?TXT,这个速度太慢了.不建议
用JSP实现一个简易的网页版聊天室。
1.登录页面
文件名:know.jsp
%@?page?language="java"?import="java.util.*"?pageEncoding="utf-8"%
%
String?path?=?request.getContextPath();
String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%
!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"
??head
????base?href="%=basePath%"
????
????title登录/title
????
????meta?http-equiv="pragma"?content="no-cache"
????meta?http-equiv="cache-control"?content="no-cache"
????meta?http-equiv="expires"?content="0"????
??/head
??body
??????form?action="know2.jsp"?method="post"
?????????input?type="text"?name="username"br/
?????????input?type="submit"?value="提交"/
??????/form
??/body
/html
2.聊天页面
文件名:know2.jsp
%@?page?language="java"?import="java.util.*"?pageEncoding="UTF-8"%
%
String?path?=?request.getContextPath();
String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%
!DOCTYPE?HTML
html
??head
????base?href="%=basePath%"
????
????title聊天/title
????meta?http-equiv="pragma"?content="no-cache"
????meta?http-equiv="cache-control"?content="no-cache"
????meta?http-equiv="expires"?content="0"????
????meta?http-equiv="refresh"content="10;url=know2.jsp"
??/head
???style
??????????.container{
????????????position:relative;
????????????top:100px;
????????????margin:0?auto;
????????????width:?500px;
????????????height:?300px;
????????????border:?1px?solid?#aaa;
????????????overflow:?hidden;
??????????}
??????????.usrlist{
????????????width:?100px;
????????????height:?250px;
????????????background-color:?#bbb;
????????????display:?block;
????????????float:?left;
?????????????overflow:?scroll;
??????????}
??????????.chartinfo{
????????????width:?400px;
????????????height:?250px;
????????????background-color:?#ccc;
????????????display:?block;
????????????float:?left;
????????????overflow:?scroll;
??????????}
??????????.send{
????????????width:?500px;
????????????height:?50px;
????????????background-color:?#ddd;
????????????display:?block;
????????????float:?left;
??????????}
??????????select{
????????????width:?50px;
???????????}
???????/style
????%
????????ListString?chartInfo?=?new??ArrayListString();//保存聊天信息的集合
????????ListString?usrs1?=new??ArrayListString();//保存登录用户的集合
????????if(null!=application.getAttribute("chartinfo")){
???????????chartInfo?=??(ListString)application.getAttribute("chartinfo");
????????}
????????if(null!=application.getAttribute("users")){
???????????usrs1??=??(ListString)?application.getAttribute("users");
????????}
????????//form?提交过来的数据
????????String?username?=?request.getParameter("username");
????????String?say?=?request.getParameter("gang");
????????String?to_usr?=?request.getParameter("tousr");
????????
???????????if(null!=username!"".equals(username)){
????????????if(!usrs1.contains(username)){
?????????????usrs1.add(username);
?????????????session.setAttribute("users",username);
????????????}
???????????}
???????application.setAttribute("users",usrs1);//将user1集合放入application
???????
???????String?lgusr?=?"";
???????if(null!=session.getAttribute("users")){
?????????lgusr?=?(String)session.getAttribute("users");
???????}
???????if(lgusr.equals("")||null==lgusr){
????????response.setStatus(response.SC_MOVED_TEMPORARILY);
????????response.setHeader("Location",?"know.jsp");?
???????}
?????????String?chart="";
???????if(null!=to_usrnull!=say!"".equals(to_usr)!"".equals(say)){
???????????chart?=?lgusr+"?对???"+to_usr+"说:?"+say;
????????}
????????
????????if(!"".equals(chart)){
????????????chartInfo.add(chart);
????????}
????????application.setAttribute("chartinfo",?chartInfo);//将聊天信息集合?放入?application
?????%
??????
??body
???div?class="container"
?????div?class="usrlist"
????????%
?????????ListString?usrs??=??(ListString)?application.getAttribute("users");
?????????if(usrs.size()0){
?????????????for(String?u?:?usrs){
????????????????out.print(u);
????????????????out.print("br/");
?????????????}
?????????}
?????????%
?????/div
?????div?class="chartinfo"
????????%
?????//???out.print(chartInfo.size());
????????for(String?ct?:?chartInfo){
??????????out.print(ct);
??????????out.print("br/");
????????}
?????????%
?????/div
?????div?class="send"
?????form?method="post"?action="know2.jsp"
?????????span对??/span
??????????select?name="tousr"?
??????????option/option
?????????????%
?????????????for(String?u?:?usrs){
????????????????if(!u.equals(lgusr)){
??????????????????out.print("");
??????????????????out.print("option?value='"+u+"'"+u+"/option");
????????????????}
????????????????
?????????????}
??????????????%
??????????/select?
???????????span??说?:/span
???????????input?type="text"?name="gang"style="width:?300px;"
???????????input?type="submit"?value="发送"
???????????/form
?????/div
???/div
??/body
/html
这是两个jsp文件,分别是know.jsp 和know2.jsp。你说的功能基本上都达到。
2. 试编写一个ASP程序,实现一个简单的聊天室,能够显示发言人的姓名,发言内容和发
写过一段简易聊天代码,用XMLHttpRequest()。
页面布局、显示文字等,跟一般网页一样。
关键是怎样获得其他聊天者的文字信息。
XMLHttpRequest 对象可在后台获得服务器信息,而不是刷新页面。
试着用这个方法编写代码,不难的