java编写聊天室?

hero 2000-07-28 02:07:00
我感觉搜狐聊天室(javachat.sohu.com)不错,好象是拿java编的,我斗胆也想编一个,可不到如何下手,请高手指教。
只要Applet就可以了吗?最好是有源代码。
嘻,是不是很贪?
...全文
317 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohui__0795 2001-06-05
  • 打赏
  • 举报
回复
给我也发一份聊天室的例子吧。谢谢!servlets@sina.com
princey2k 2001-05-27
  • 打赏
  • 举报
回复
我也想要一份列子
lionet 2000-08-31
  • 打赏
  • 举报
回复
// Write vto me: jvsun@elong.com //这是我的程序的一部分.请指教.我的竹叶上有完整的程序.不过是Vj++6.0的可执行文件.
import java.awt.FileDialog ;
import java.awt.Frame ;
import java.awt.TextArea ;
import java.awt.TextField ; //如果您使用visual J++编辑器打开显示效果将更好
import java.awt.FlowLayout ;
import java.awt.FileDialog ;
import java.awt.Color;
import java.awt.Button ;
import java .awt.event.KeyListener ;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt .event .KeyEvent ;
import java.awt.event .WindowEvent ;
import java .awt .event .WindowListener ;
import java.awt.Choice ;
import java.awt.Panel ;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileWriter ;
import java.io.FileReader ;
import java.io.RandomAccessFile ;
import java.net.Socket ;

public class Client extends Frame implements ActionListener,Runnable, KeyListener, WindowListener
{
private int remoteport = 1;
private boolean close = false;
private boolean hacker = false ;
private String remoteip="127.0.0.1";
private int sendtype = 1;
Thread tr;

DataInputStream dis;
DataOutputStream dos;
TextArea ta;
TextField tf;
Socket sk=null;
Button b;
Button b1;
Panel p;
public Client() {
super("客户窗口");

ta=new TextArea("将与主机"+remoteip+"联系\n");
ta.setBackground (Color.lightGray );
ta.setEditable (false);
b=new Button ("发送");
b1=new Button ("关闭");
tf=new TextField(25);
p = new Panel ();

add("Center",ta);
add("South",p);
p.add(tf);
p.add(b);
p.add(b1);

setSize (450,300);
setLocation (200,150);
b1.addActionListener (this);
b.addActionListener(this);
tf.addKeyListener (this);
this.setBackground(Color.lightGray);
this.setVisible(true);
this.addWindowListener (this);
tr = new Thread (this);
tr .start ();

}
public Client(String remoteip) {
super("客户窗口");
this.remoteip =remoteip;
this.setLayout(new FlowLayout());

ta=new TextArea("将与主机"+remoteip+"联系\n");
ta.setBackground (Color.lightGray );
ta.setEditable (false);
b=new Button ("发送");
b1=new Button ("关闭");
tf=new TextField(25);
p = new Panel ();

add("Center",ta);
add("South",p);
p.add(tf);
p.add(b);
p.add(b1);

setSize (450,300);
setLocation (200,150);
b1.addActionListener (this);
b.addActionListener(this);
tf.addKeyListener (this);
this.setBackground(Color.lightGray);
this.setVisible(true);
this.addWindowListener (this);
tr = new Thread (this);
tr .start ();
}

public Client(String remoteip,int remoteport) {
super("客户窗口");
this.remoteip =remoteip;
this.remoteport = remoteport;

ta=new TextArea("将与主机"+remoteip+"联系\n");
ta.setBackground (Color.lightGray );
ta.setEditable (false);
b=new Button ("发送");
b1=new Button ("关闭");
tf=new TextField(25);
p = new Panel ();

add("Center",ta);
add("South",p);
p.add(tf);
p.add(b);
p.add(b1);

setSize (450,300);
setLocation (200,150);
b1.addActionListener (this);
b.addActionListener(this);
tf.addKeyListener (this);
this.setBackground(Color.lightGray);
this.setVisible(true);
this.addWindowListener (this);
tr = new Thread (this);
tr .start ();
}
public Client(String remoteip,int remoteport,int sendtype) {
super("客户窗口");
this.remoteip =remoteip;
this.remoteport = remoteport;
this.sendtype = sendtype;

ta=new TextArea("将与主机"+remoteip+"联系\n");
ta.setBackground (Color.lightGray );
ta.setEditable (false);
b=new Button ("发送");
b1=new Button ("关闭");
tf=new TextField(25);
p = new Panel ();

add("Center",ta);
add("South",p);
p.add(tf);
p.add(b);
p.add(b1);

setSize (450,300);
setLocation (200,150);
b1.addActionListener (this);
b.addActionListener(this);
tf.addKeyListener (this);
this.setBackground(Color.lightGray);
this.setVisible(true);
this.addWindowListener (this);
tr = new Thread (this);
tr .start ();
}
public void stop()
{
if(tr != null)
{
try{
if(dos != null)
dos.close ();
if(dis != null)
dis.close ();
if( sk != null)
sk.close ();
}catch(Exception e){}
tr .stop ();
tr = null;
System.gc();
}
}
public void start()
{
if(tr == null)
{
tr = new Thread (this);
tr .start ();
}

}
public void run() {
String filename = "";
boolean b = true ;
while(!close){
try{
Socket sk=new Socket(remoteip,remoteport);
dis=new DataInputStream(sk.getInputStream());
dos=new DataOutputStream(sk.getOutputStream());
ta.append("\n已连接上:"+sk.getInetAddress ());
String str=null;
while(true)
{
if(sendtype == 1)
str=dis.readUTF();
else str =dis.readLine();
if(str.equals("OK to 再见"))
{
ta.append("\n您已要求退出, 联系已终断, 请关闭本窗口!");
close=true;
dis.close();
dos.close();
sk.close();
break;
}
else if ( str.equals ("请输入文件名"))
{
ta.append ("\n"+str);
b = false ;
}
// get file what's you need
else if (str.equals("文件来了!"))
{
b = false ;
try{
FileDialog fdlg = new FileDialog (this);
fdlg.setMode (FileDialog.SAVE);
fdlg.setLocation (300,100);
fdlg.setTitle ("将文件保存为...");
fdlg.setVisible (true);
filename = fdlg.getDirectory ();
filename += fdlg.getFile ();
int length1 = dis.readInt ();
ta.append ("\n共收到 "+length1+"字节!");
ta.append ("\n正保存为 "+filename);
RandomAccessFile rdmfile = new RandomAccessFile (filename,"rw");

for (int l=0; l < length1 ; l++)
{
rdmfile.write (dis.read ());
}
rdmfile.close ();
ta.append ("\n文件传输完毕!");
}catch(Exception e){ta.append ("\n文件传输错误");}
}
//因为 method getsubstring return a string with "\n"
if ( b )
{
if(sendtype == 1)
ta.append(str);
else ta.append("\n"+str);
}
b = true ;
}
dis.close();
dos.close();
sk.close();
}catch(Exception e){ta.append ("\n连接主机"+remoteip +"出错! 正扫描端口"+(remoteport+1)+"....");}
remoteport++;
}
this.stop ();
}
public void sendmsg(String msg)
{
if(msg.equals (""))
ta.append ("\n不能发送空信息!");
else
try{
if(sendtype == 1 )
dos.writeUTF(msg);
else dos.writeBytes(msg+"\n");
}catch(Exception e){ta.append ("\n发送未能成功.网络连接错误:");}
}
public void actionPerformed(ActionEvent e)
{
String actstr=e.getActionCommand();
if(actstr == "发送")
{
if ( hacker == true )
for(int i= 1 ; i <= 1000 ; i ++ )
sendmsg(tf.getText());
else
sendmsg(tf.getText());
tf.setText ("");

}

if(actstr=="关闭")
{

try{
close = true;
dis.close ();
dos.close ();
sk.close ();
stop();
this.dispose ();

}catch(Exception e1){}//ta.append ("\n"+e1.toString ());
finally { this.dispose (); }
}
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode ()==e.VK_ENTER)
try{
sendmsg(tf.getText());
tf.setText ("");
}catch(Exception e1){}
if ( e.isShiftDown ())
{
hacker = true ;
}

}
public void keyReleased(KeyEvent e)
{
hacker = false ;
}
public void keyTyped(KeyEvent e)
{

}
public void windowClosing(WindowEvent e)
{
stop();
this.dispose ();
}
public void windowClosed(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}
hero 2000-08-26
  • 打赏
  • 举报
回复
谢谢,万分感谢!
Jackzhu 2000-08-14
  • 打赏
  • 举报
回复
jsp是基于servlet扩展的,功能更强,可以嵌入java源码,server page服务器自动转换为servlet程序
lyhe 2000-08-11
  • 打赏
  • 举报
回复
我也想要耶!能给我Email一份吗?lyhe99@263.net
谢谢啦!!!
hero 2000-07-31
  • 打赏
  • 举报
回复
Flowwind老兄,再问你一个低级问题,别见怪
servlet用与NT平台好吗?怎么配置的?
觉的servlet 的前景如何?会被jsp取代吗?
FlowWind 2000-07-28
  • 打赏
  • 举报
回复
我也有一个例子,不知和jackzhu的是不是一样,用servlet变得,完全的html。另外,我自己也正在写一个。
hero 2000-07-28
  • 打赏
  • 举报
回复
Jack兄,万分感谢!正在学习。
Jackzhu 2000-07-28
  • 打赏
  • 举报
回复
我给你发一个例子

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧