java聊天室代码 谁有已经做好的java聊天室代码.能否发给小弟?在此先谢啦? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我有,不过前提是你必须会RMI的启动工作,也就是启动JNDI服务器.因为这个聊天室是用RMI技术作的.但我还有个类似QQ的简单的时时通讯的程序.这个可以像一般的简单程序一样启动.不过,不好意思我代码里的注释很少.要就说一声. 客户端: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){} } } }} 我要 [email protected] 我对NIO的理解 关于指向NULL会不会被回收的问题 高分求救:RCP程序中调用单态类问题。 新手求助~ 这能证明发生了JAVA内存泄漏了嘛??? 请问现在用java做界面程序的人多吗?公司多吗? 十万个为什莫之“关于接口的实现FilenameFilter接口” 将不定期开放“人人都说Swing丑,唯我独爱它—Swing高仿QQ” 源码 各位高人,,jbuilder7怎样一步一步调试运行程序? 有两个Vector组件,怎么判断如果一个Vector存在的数据,另一个Vector不能有? 如何获取JTextPane中文本的行数 百度面试题,很不错哦
但我还有个类似QQ的简单的时时通讯的程序.这个可以像一般的简单程序一样启动.不过,不好意思我代码里的注释很少.要就说一声.
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){}
}
}
}
}
[email protected]