谁有可以集成的JAVA聊天室源代码
1个回答
展开全部
服务器端
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer{
public static void main(String[] args)throws Exception{
ServerSocket svSocket =null;
Vector threads = new Vector();
System.out.println ("listening...");
try {
svSocket = new ServerSocket(5555);
}catch (Exception ex) {
System.out.println ("Server create ServerSocket failed!");
return;
}
try{
int nid = 0;
while(true){
Socket socket = svSocket.accept();
System.out.println ("accept a client");
ServerThread st = new ServerThread(socket,threads);
st.setID(nid++);
threads.add(st);
new Thread(st).start();
for(int i=0;i < threads.size();i++){
ServerThread temp = (ServerThread)threads.elementAt(i);
}
System.out.println ("Listen again...");
}
}catch(Exception ex){
System.out.println ("server is down");
}
}
}
class ServerThread implements Runnable{
private Vector threads;
private Socket socket = null;
private DataInputStream in = null;
private DataOutputStream out = null;
private int nid;
public ServerThread(Socket socket,Vector threads){
this.socket = socket;
this.threads = threads;
try {
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (Exception ex) {
}
}
public void run(){
System.out.println ("Thread is running");
try{
while(true){
String receive = in.readUTF();
if(receive == null)
return;
if(receive.equals("leave")){
for(int i=0;i < threads.size();i++){
ServerThread st = (ServerThread)threads.elementAt(i);
st.write("***"+getID()+"leaving...***");
}
}else{
for(int i=0;i < threads.size();i++){
ServerThread st = (ServerThread)threads.elementAt(i);
st.write("<"+getID()+">: "+receive);
}
}
}
}catch(Exception ex){
threads.removeElement(this);
//ex.printStackTrace();
}
try{
socket.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
public void write(String msg){
synchronized(out){
try{
out.writeUTF(msg);
}catch(Exception ex){
}
}
}
public int getID(){
return this.nid;
}
public void setID(int nid){
this.nid = nid;
}
}
客户端
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;
public class Client1
{
public static void main(String args[])
{
new ChatClient();
}
}
class ChatClient extends Frame implements Runnable,ActionListener
{
Button connection,send;
TextField inputName,inputContent;
TextArea chatResult;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread;
String name="";
public ChatClient()
{
socket=new Socket();
Box box1=Box.createHorizontalBox();
connection=new Button("连接服务器");
send=new Button("发送");
send.setEnabled(false);
inputName=new TextField(6);
inputContent=new TextField(22);
chatResult=new TextArea();
box1.add(new Label("输入昵称"));
box1.add(inputName);
box1.add(connection);
Box box2=Box.createHorizontalBox();
box2.add(new Label("输入聊天内容"));
box2.add(inputContent);
box2.add(send);
connection.addActionListener(this);
send.addActionListener(this);
thread=new Thread(this);
add(box1,BorderLayout.NORTH);
add(box2,BorderLayout.SOUTH);
add(chatResult,BorderLayout.CENTER);
setBounds(10,30,400,280);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==connection)
{
try
{
if(socket.isConnected())
{}
else
{
InetAddress address=InetAddress.getByName("127.0.0.1");
InetSocketAddress socketAddress=new InetSocketAddress(address,5555);
socket.connect(socketAddress);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
name=inputName.getText();
out.writeUTF("姓名"+name);
send.setEnabled(true);
if(!(thread.isAlive()))
thread=new Thread(this);
thread.start();
}
}
catch(IOException ee){}
}
if(e.getSource()==send)
{
String s=inputContent.getText();
if(s!=null)
{
try
{
out.writeUTF(name+":"+s);
}
catch(IOException e1){}
}
}
}
public void run()
{
String s=null;
while(true)
{
try
{
s=in.readUTF();
chatResult.append("\n"+s);
}
catch(IOException e)
{
chatResult.setText("与服务器断开连接");
try
{
socket.close();
}
catch(Exception ep){}
break;
}
}
}
}
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer{
public static void main(String[] args)throws Exception{
ServerSocket svSocket =null;
Vector threads = new Vector();
System.out.println ("listening...");
try {
svSocket = new ServerSocket(5555);
}catch (Exception ex) {
System.out.println ("Server create ServerSocket failed!");
return;
}
try{
int nid = 0;
while(true){
Socket socket = svSocket.accept();
System.out.println ("accept a client");
ServerThread st = new ServerThread(socket,threads);
st.setID(nid++);
threads.add(st);
new Thread(st).start();
for(int i=0;i < threads.size();i++){
ServerThread temp = (ServerThread)threads.elementAt(i);
}
System.out.println ("Listen again...");
}
}catch(Exception ex){
System.out.println ("server is down");
}
}
}
class ServerThread implements Runnable{
private Vector threads;
private Socket socket = null;
private DataInputStream in = null;
private DataOutputStream out = null;
private int nid;
public ServerThread(Socket socket,Vector threads){
this.socket = socket;
this.threads = threads;
try {
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (Exception ex) {
}
}
public void run(){
System.out.println ("Thread is running");
try{
while(true){
String receive = in.readUTF();
if(receive == null)
return;
if(receive.equals("leave")){
for(int i=0;i < threads.size();i++){
ServerThread st = (ServerThread)threads.elementAt(i);
st.write("***"+getID()+"leaving...***");
}
}else{
for(int i=0;i < threads.size();i++){
ServerThread st = (ServerThread)threads.elementAt(i);
st.write("<"+getID()+">: "+receive);
}
}
}
}catch(Exception ex){
threads.removeElement(this);
//ex.printStackTrace();
}
try{
socket.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
public void write(String msg){
synchronized(out){
try{
out.writeUTF(msg);
}catch(Exception ex){
}
}
}
public int getID(){
return this.nid;
}
public void setID(int nid){
this.nid = nid;
}
}
客户端
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;
public class Client1
{
public static void main(String args[])
{
new ChatClient();
}
}
class ChatClient extends Frame implements Runnable,ActionListener
{
Button connection,send;
TextField inputName,inputContent;
TextArea chatResult;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread;
String name="";
public ChatClient()
{
socket=new Socket();
Box box1=Box.createHorizontalBox();
connection=new Button("连接服务器");
send=new Button("发送");
send.setEnabled(false);
inputName=new TextField(6);
inputContent=new TextField(22);
chatResult=new TextArea();
box1.add(new Label("输入昵称"));
box1.add(inputName);
box1.add(connection);
Box box2=Box.createHorizontalBox();
box2.add(new Label("输入聊天内容"));
box2.add(inputContent);
box2.add(send);
connection.addActionListener(this);
send.addActionListener(this);
thread=new Thread(this);
add(box1,BorderLayout.NORTH);
add(box2,BorderLayout.SOUTH);
add(chatResult,BorderLayout.CENTER);
setBounds(10,30,400,280);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==connection)
{
try
{
if(socket.isConnected())
{}
else
{
InetAddress address=InetAddress.getByName("127.0.0.1");
InetSocketAddress socketAddress=new InetSocketAddress(address,5555);
socket.connect(socketAddress);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
name=inputName.getText();
out.writeUTF("姓名"+name);
send.setEnabled(true);
if(!(thread.isAlive()))
thread=new Thread(this);
thread.start();
}
}
catch(IOException ee){}
}
if(e.getSource()==send)
{
String s=inputContent.getText();
if(s!=null)
{
try
{
out.writeUTF(name+":"+s);
}
catch(IOException e1){}
}
}
}
public void run()
{
String s=null;
while(true)
{
try
{
s=in.readUTF();
chatResult.append("\n"+s);
}
catch(IOException e)
{
chatResult.setText("与服务器断开连接");
try
{
socket.close();
}
catch(Exception ep){}
break;
}
}
}
}
追问
我要的是在web页面用的。 不过还是谢谢你!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询