发个java聊天室代码,需要的看下 一个聊天室实现代码,希望对大家有所帮助,分享一下连接在这里http://download.csdn.net/my或者http://download.csdn.net/detail/lxybelieve/3764363 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 GetMessage.java服务端:import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.ServerSocket;import java.net.Socket;import javax.swing.JLabel;import javax.swing.JTextArea;public class GetMessage extends Thread{ private int i; String v; JLabel label=null; private JTextArea text; public GetMessage(int i,JTextArea text) { this.i=i; this.text=text; } public void run(){ try { ServerSocket so = new ServerSocket(i); Socket s = so.accept(); while(true){ InputStreamReader i = new InputStreamReader(s.getInputStream()); BufferedReader b = new BufferedReader(i); v= b.readLine(); text.append("对方说"+v+"\n"); } } catch (IOException e) { //label.setText("对方已经下线"); text.append("对方下线了"); } }}SendMessage.java客户端:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.Socket;import java.net.UnknownHostException;import javax.swing.JLabel;import javax.swing.JTextArea;import javax.swing.JTextField;public class SendMessage extends Thread { private String ip; private int i; Socket s = null; JLabel label=null; JTextField text; JTextArea text1; public SendMessage(String ip,int i,JTextArea text1) { // TODO Auto-generated constructor stub this.ip=ip; this.i=i; this.text1=text1; } public void run(){ while(true){ try { s = new Socket(ip,i); text1.setText("连接成功"+"\n"); break; } catch (Exception e) { try { Thread.sleep(1000); } catch (InterruptedException e1) { System.out.println("出错了。"); } } } } public void send(String message) { try { PrintStream p = new PrintStream(s.getOutputStream()); p.println(message); } catch (Exception e1) { System.out.println("异常"+e1.getMessage()); } } }Test.java 简单的界面和测试类import java.awt.*;import java.awt.event.*;import java.io.ByteArrayInputStream;import java.io.InputStream;import java.util.*;import javax.swing.*;import javax.swing.event.*;class WindowTextArea extends JFrame implements ActionListener{ String s; JTextArea text1; JTextArea text2; JButton button1,button2,button3; SendMessage t2; GetMessage t1; JLabel lable1,lable2; JTextField text; WindowTextArea() { this.s=s; lable1=new JLabel("对方ip"); text=new JTextField(20); text1=new JTextArea(6,18); text2=new JTextArea(6,18); text2.setEditable(false); button1=new JButton("发送"); button2=new JButton("关闭"); button3=new JButton("确定ip"); setBounds(100,100,450,300); setVisible(true); Container con=getContentPane(); con.setLayout(new FlowLayout()); con.add(lable1); con.add(text); con.add(button3); con.add(new JScrollPane(text1)); con.add(new JScrollPane(text2)); con.add(button1); con.add(button2); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); con.validate(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void runthread(String ip) { t1 = new GetMessage(4066,text2); t1.start(); t2=new SendMessage(ip,4066,text2); t2.start(); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==button2) { System.exit(0); } if(e.getSource()==button1) { text2.append(text1.getText()+"\n"); t2.send(text1.getText()); text1.setText(""); } if(e.getSource()==button3) { s=text.getText(); runthread(s); } } }public class Test { /** * @param args */ public static void main(String[] args) { new WindowTextArea(); } } new WindowTextArea(); } }向对方发送信息之前必须输入对方ip和点击确定ip按钮,文本区显示“连接成功”然后可以发送信息 视频聊天室其实也很简单,用flash+java+red5。(*^__^*) 嘻嘻……网上有源码。 import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.ServerSocket;import java.net.Socket;import javax.swing.JLabel;import javax.swing.JTextArea;public class GetMessage extends Thread{private int i;String v;JLabel label=null;private JTextArea text;public GetMessage(int i,JTextArea text) {this.i=i;this.text=text;}public void run(){try {ServerSocket so = new ServerSocket(i);Socket s = so.accept();while(true){InputStreamReader i = new InputStreamReader(s.getInputStream());BufferedReader b = new BufferedReader(i);v= b.readLine();text.append("对方说"+v+"\n");}} catch (IOException e) {//label.setText("对方已经下线");text.append("对方下线了");}}}SendMessage.java客户端:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.Socket;import java.net.UnknownHostException;import javax.swing.JLabel;import javax.swing.JTextArea;import javax.swing.JTextField;public class SendMessage extends Thread {private String ip;private int i;Socket s = null;JLabel label=null;JTextField text;JTextArea text1;public SendMessage(String ip,int i,JTextArea text1) {// TODO Auto-generated constructor stubthis.ip=ip;this.i=i;this.text1=text1;} public void run(){ while(true){try {s = new Socket(ip,i);text1.setText("连接成功"+"\n");break;} catch (Exception e) {try {Thread.sleep(1000);} catch (InterruptedException e1) {System.out.println("出错了。");}} } } public void send(String message) {try {PrintStream p = new PrintStream(s.getOutputStream());p.println(message);} catch (Exception e1) {System.out.println("异常"+e1.getMessage());}} }Test.java 简单的界面和测试类import java.awt.*;import java.awt.event.*;import java.io.ByteArrayInputStream;import java.io.InputStream;import java.util.*;import javax.swing.*;import javax.swing.event.*;class WindowTextArea extends JFrame implements ActionListener{ String s;JTextArea text1; JTextArea text2; JButton button1,button2,button3; SendMessage t2; GetMessage t1; JLabel lable1,lable2; JTextField text; WindowTextArea() { this.s=s; lable1=new JLabel("对方ip"); text=new JTextField(20); text1=new JTextArea(6,18); text2=new JTextArea(6,18); text2.setEditable(false); button1=new JButton("发送"); button2=new JButton("关闭"); button3=new JButton("确定ip"); setBounds(100,100,450,300); setVisible(true); Container con=getContentPane(); con.setLayout(new FlowLayout()); con.add(lable1); con.add(text); con.add(button3); con.add(new JScrollPane(text1)); con.add(new JScrollPane(text2)); con.add(button1); con.add(button2); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); con.validate(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void runthread(String ip) {t1 = new GetMessage(4066,text2);t1.start();t2=new SendMessage(ip,4066,text2);t2.start(); } public void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource()==button2){System.exit(0);}if(e.getSource()==button1){ text2.append(text1.getText()+"\n"); t2.send(text1.getText()); text1.setText("");}if(e.getSource()==button3){ s=text.getText();runthread(s); }}}public class Test {/*** @param args*/public static void main(String[] args) {new WindowTextArea();}}new WindowTextArea();}}向对方发送信息之前必须输入对方ip和点击确定ip按钮,文本区显示“连接成功”然后可以发送信息 为什么我的while(left > 1) 一直报错?? 一条正则表达式,为什么报错,请高手指点下。 JSP使用JSF,但是页面如何判断SESSION 如何让JTable的一个单元格进入编辑状态 java中写代码可以用AnyType么? 请教red hat linux 8.0 下安装jdk?! 对JAVA单线程的一点疑惑? 有两个文本框,当在第一个文本框内输入boy时,回车,第二个文本框显示男子 java中的lookfeel如何使用 这个要怎么写 ASCII转换Unicode求详解 关于java强制类型转换的溢出问题 --- 求解
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;import javax.swing.JLabel;
import javax.swing.JTextArea;
public class GetMessage extends Thread{
private int i;
String v;
JLabel label=null;
private JTextArea text;
public GetMessage(int i,JTextArea text) { this.i=i;
this.text=text;
}
public void run(){
try {
ServerSocket so = new ServerSocket(i);
Socket s = so.accept();
while(true){
InputStreamReader i = new InputStreamReader(s.getInputStream());
BufferedReader b = new BufferedReader(i);
v= b.readLine();
text.append("对方说"+v+"\n");
}
} catch (IOException e) {
//label.setText("对方已经下线");
text.append("对方下线了");
}
}
}SendMessage.java客户端:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SendMessage extends Thread {
private String ip;
private int i;
Socket s = null;
JLabel label=null;
JTextField text;
JTextArea text1;
public SendMessage(String ip,int i,JTextArea text1) {
// TODO Auto-generated constructor stub
this.ip=ip;
this.i=i;
this.text1=text1;
}
public void run(){
while(true){
try {
s = new Socket(ip,i);
text1.setText("连接成功"+"\n");
break;
} catch (Exception e) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
System.out.println("出错了。");
}
}
}
}
public void send(String message)
{
try {
PrintStream p = new PrintStream(s.getOutputStream()); p.println(message);
} catch (Exception e1) {
System.out.println("异常"+e1.getMessage());
}
}
}Test.java 简单的界面和测试类
import java.awt.*;import java.awt.event.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.*;import javax.swing.*;
import javax.swing.event.*;
class WindowTextArea extends JFrame implements ActionListener
{
String s;
JTextArea text1;
JTextArea text2;
JButton button1,button2,button3;
SendMessage t2;
GetMessage t1;
JLabel lable1,lable2;
JTextField text; WindowTextArea()
{ this.s=s;
lable1=new JLabel("对方ip");
text=new JTextField(20);
text1=new JTextArea(6,18);
text2=new JTextArea(6,18);
text2.setEditable(false);
button1=new JButton("发送");
button2=new JButton("关闭");
button3=new JButton("确定ip");
setBounds(100,100,450,300);
setVisible(true);
Container con=getContentPane();
con.setLayout(new FlowLayout());
con.add(lable1);
con.add(text);
con.add(button3);
con.add(new JScrollPane(text1));
con.add(new JScrollPane(text2));
con.add(button1);
con.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
con.validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void runthread(String ip)
{
t1 = new GetMessage(4066,text2);
t1.start();
t2=new SendMessage(ip,4066,text2);
t2.start();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==button2)
{
System.exit(0);
}
if(e.getSource()==button1)
{
text2.append(text1.getText()+"\n");
t2.send(text1.getText());
text1.setText("");
}
if(e.getSource()==button3)
{
s=text.getText(); runthread(s);
}
}
}
public class Test { /**
* @param args
*/
public static void main(String[] args) {
new WindowTextArea();
}
}
new WindowTextArea();
}
}向对方发送信息之前必须输入对方ip和点击确定ip按钮,文本区显示“连接成功”
然后可以发送信息
视频聊天室其实也很简单,用flash+java+red5。(*^__^*) 嘻嘻……网上有源码。
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;import javax.swing.JLabel;
import javax.swing.JTextArea;
public class GetMessage extends Thread{
private int i;
String v;JLabel label=null;
private JTextArea text;
public GetMessage(int i,JTextArea text) {this.i=i;
this.text=text;
}public void run(){
try {
ServerSocket so = new ServerSocket(i);
Socket s = so.accept();
while(true){
InputStreamReader i = new InputStreamReader(s.getInputStream());
BufferedReader b = new BufferedReader(i);
v= b.readLine();
text.append("对方说"+v+"\n");
}
} catch (IOException e) {
//label.setText("对方已经下线");
text.append("对方下线了");
}
}
}SendMessage.java客户端:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SendMessage extends Thread {
private String ip;
private int i;
Socket s = null;
JLabel label=null;
JTextField text;
JTextArea text1;
public SendMessage(String ip,int i,JTextArea text1) {
// TODO Auto-generated constructor stub
this.ip=ip;
this.i=i;
this.text1=text1;}
public void run(){
while(true){
try {
s = new Socket(ip,i);
text1.setText("连接成功"+"\n");
break;
} catch (Exception e) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
System.out.println("出错了。");
}
}
}
}
public void send(String message)
{
try {PrintStream p = new PrintStream(s.getOutputStream());p.println(message);
} catch (Exception e1) {
System.out.println("异常"+e1.getMessage());
}
}
}Test.java 简单的界面和测试类
import java.awt.*;import java.awt.event.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.*;import javax.swing.*;
import javax.swing.event.*;
class WindowTextArea extends JFrame implements ActionListener
{ String s;
JTextArea text1;
JTextArea text2;
JButton button1,button2,button3;
SendMessage t2;
GetMessage t1;
JLabel lable1,lable2;
JTextField text; WindowTextArea()
{ this.s=s;
lable1=new JLabel("对方ip");
text=new JTextField(20);
text1=new JTextArea(6,18);
text2=new JTextArea(6,18);
text2.setEditable(false);
button1=new JButton("发送");
button2=new JButton("关闭");
button3=new JButton("确定ip");
setBounds(100,100,450,300);
setVisible(true);
Container con=getContentPane();
con.setLayout(new FlowLayout());
con.add(lable1);
con.add(text);
con.add(button3);
con.add(new JScrollPane(text1));
con.add(new JScrollPane(text2));
con.add(button1);
con.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
con.validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void runthread(String ip)
{
t1 = new GetMessage(4066,text2);
t1.start();
t2=new SendMessage(ip,4066,text2);
t2.start();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==button2)
{
System.exit(0);
}
if(e.getSource()==button1)
{
text2.append(text1.getText()+"\n");
t2.send(text1.getText());
text1.setText("");
}
if(e.getSource()==button3)
{
s=text.getText();runthread(s);
}
}
}
public class Test {/**
* @param args
*/
public static void main(String[] args) {new WindowTextArea();}
}
new WindowTextArea();}
}向对方发送信息之前必须输入对方ip和点击确定ip按钮,文本区显示“连接成功”
然后可以发送信息