java聊天室代码

yxqc 2003-11-22 08:48:40
谁有已经做好的java聊天室代码.能否发给小弟?在此先谢啦?
...全文
455 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sky19900523 2010-06-21
  • 打赏
  • 举报
回复
牛人一个
purple_dream 2010-06-21
  • 打赏
  • 举报
回复
kankan
cbenny 2003-11-23
  • 打赏
  • 举报
回复
我要
robinhood11.student@sina.com
foryouever 2003-11-22
  • 打赏
  • 举报
回复
客户端:
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.awt.*;

public class ClientPoint extends JFrame implements ActionListener,KeyListener{
static int SERVERPORT=8088;
static String SERVERADDRESS="127.0.0.1";
private BufferedReader cin=null;
private PrintWriter cout=null;
private Socket client=null;
public String clientname;
private JPanel jPanel1=new JPanel();
private JTextField jTextSendInfo=new JTextField(38);
private JTextArea jTextGetInfo=new JTextArea(16,33);
public JScrollPane areaScrollPane=new JScrollPane(jTextGetInfo);
private Button Send=new Button("发送");
private Button Link=new Button("连接");

public ClientPoint(){
super("聊天室^_^");
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
setSize(480,465);
jPanel1.add("South",jTextSendInfo);
jPanel1.setBackground(new Color(75, 234, 166));
jTextGetInfo.setBackground(new Color(122,150,223));
jTextGetInfo.setFont(new java.awt.Font("Dialog", 0, 15));
jTextGetInfo.setForeground(new Color(176, 42, 0));
this.setResizable(false);
Send.setBackground(Color.pink);
Send.setForeground(Color.blue);
Send.setBounds(new Rectangle(92, 400, 90, 37));
Link.setBackground(Color.pink);
Link.setForeground(Color.blue);
areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setAutoscrolls(true);
jTextSendInfo.setText("");
jPanel1.add("North",areaScrollPane);
jTextGetInfo.setEditable(false);
jPanel1.add("South",Send);
Send.setEnabled(false);
jPanel1.add("South",Link);
Send.addActionListener(this);
jTextSendInfo.addKeyListener(this);
Link.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e){
if(!jTextSendInfo.getText().equals("")){
clientname=jTextSendInfo.getText();
connect();
Link.setEnabled(false);
Send.setEnabled(true);
jTextSendInfo.setText("");
}
else JOptionPane.showMessageDialog(null,"警告:用户名不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE);
}});
this.setContentPane(jPanel1);
}
public void connect(){
try{
client=new Socket(SERVERADDRESS,SERVERPORT);
cout=new PrintWriter(client.getOutputStream());
cin=new BufferedReader(
new InputStreamReader(client.getInputStream()));
cout.println("欢迎["+clientname+"]进入聊天室");
cout.flush();
Receiver r=new Receiver();
r.start();
}catch(Exception e){

e.printStackTrace();
}
}
protected void processWindowEvent(WindowEvent e){
if(e.getID()==WindowEvent.WINDOW_CLOSING){
quit();
}
super.processWindowEvent(e);
}
public static void main(String[] args){
ClientPoint cp=new ClientPoint();
cp.show();

}

class Receiver extends Thread{
public void run(){
String msg=null;
JScrollBar sb;
try{
msg=cin.readLine();
while(true){
jTextGetInfo.append(msg+"\n");
sb=areaScrollPane.getVerticalScrollBar();
sb.setValue(sb.getMaximum()+50);
jTextSendInfo.setEnabled(true);
msg=cin.readLine();
}
}catch(Exception e){
Send.setEnabled(false);
}
}
}
void quit(){
try{
cout.println("["+clientname+"]离开聊天室");
cout.flush();
cout.println("exit");
cout.flush();
System.out.print("lianjieduankai");
cin.close();
cout.close();
client.close();
}catch(Exception e){}
finally{System.exit(0);
}
}

public void actionPerformed(ActionEvent e){
if(!jTextSendInfo.getText().equals("")){
cout.println("["+clientname+"]:"+jTextSendInfo.getText());
cout.flush();
jTextSendInfo.setText("");
jTextSendInfo.setFocusable(true);
}
else JOptionPane.showMessageDialog(null,"警告:消息不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE);
}

public void keyPressed(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode()==KeyEvent.VK_ENTER)
{
if(!jTextSendInfo.getText().equals(""))
{
if(cout==null)
{
clientname=jTextSendInfo.getText();
connect();
Link.setEnabled(false);
Send.setEnabled(true);
jTextSendInfo.setText("");
}
else
{
cout.println("["+clientname+"]:"+jTextSendInfo.getText());
cout.flush();
jTextSendInfo.setText("");
jTextSendInfo.setFocusable(true);
}
}
else
{
if (cout==null)
{
JOptionPane.showMessageDialog(null,"警告:用户名不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE);
}
else JOptionPane.showMessageDialog(null,"警告:消息不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE);
}



}
}

}

服务器端:

import java.net.*;
import java.io.*;

public class ServerPoint extends Thread{
static int SERVERPORT=8088;
private Socket client;
public static int i=0;
public static String[] vct=new String[10];
public ServerPoint(){}
public void run(){
ClientThread.log("服务器端程序启动...");
try{
ServerSocket server=new ServerSocket(SERVERPORT);
while(true){
client=server.accept();
ClientThread ct=new ClientThread(client);
ct.start();
}
}catch(Exception e){
e.printStackTrace();
ClientThread.log("服务器端程序关闭。。。");
System.exit(0);
}
}
public static void main(String[] args){

ServerPoint sp=new ServerPoint();
sp.start();
}
public static synchronized void message(String msg){
vct[i]=msg;
i=(i+1)%10;
vct[i]="*";

}
}
class ClientThread extends Thread{
private static int ii=0;
private Socket s;
String msg=null;
ServerSocket serverSocket=null;
Socket socket=null;
BufferedReader cin=null;
PrintWriter cout=null;
public ClientThread(Socket s){
this.s=s;
}
public void run(){
try{
cin=new BufferedReader(
new InputStreamReader(s.getInputStream()));

cout=new PrintWriter(s.getOutputStream());
SendToAll sta=new SendToAll();
sta.start();
msg=cin.readLine();
ii++;
System.out.println("some connect us!聊天室里总共有"+ii+"个人");
while(!msg.equals("exit")){
ServerPoint.message(msg);
msg=cin.readLine();
}

if(msg.equals("exit"))
{ --ii;
System.out.print("someone exit\n聊天室里总共有"+ii+"个人");
cin.close();
cout.close();
s.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
static void log(String strInfo){
System.out.println(strInfo);
}
class SendToAll extends Thread{
private int j=-1;
public void run(){
while(true)
{
try{
sleep(500);
if(j==-1){
if(!ServerPoint.vct[0].equals("*")){
cout.println(ServerPoint.vct[0]);
cout.flush();
j=1;
}
else {
cout.println(ServerPoint.vct[1]);
cout.flush();
j=2;
}
}
while(!ServerPoint.vct[j].equals("*"))
{

cout.println(ServerPoint.vct[j]);
cout.flush();
j=(j+1)%10;

}
}catch(Exception e){}
}
}
}
}
noratong 2003-11-22
  • 打赏
  • 举报
回复
我有,不过前提是你必须会RMI的启动工作,也就是启动JNDI服务器.因为这个聊天室是用RMI技术作的.
但我还有个类似QQ的简单的时时通讯的程序.这个可以像一般的简单程序一样启动.

不过,不好意思我代码里的注释很少.

要就说一声.

62,614

社区成员

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

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