谁有Java聊天室的程序啊,帮帮忙吧
还有,说说怎么运行的,要用到什么软件,我只有eclipse软件,最好多一点注释,好让我看得懂,后天就要交了,急呀,谢谢了!...
还有,说说怎么运行的,要用到什么软件,我只有eclipse软件,最好多一点注释,好让我看得懂,后天就要交了,急呀,谢谢了!
展开
展开全部
//服务器端:
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
/**
* @param args
*/
boolean start = false;
ServerSocket ss = null;
Socket s = null;
List<Client> clients = new ArrayList<Client>();
public static void main(String[] args) {
new ChatServer().start();
}
public void start() {
try {
ss = new ServerSocket(8888);
start = true;
int num = 0;
while (start) {
num ++;
s = ss.accept();
System.out.println("a client connet! " + num);
Client c = new Client(s,num);
new Thread(c).start();
clients.add(c);
}
ss.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private class Client implements Runnable {
private Socket s = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private int num = 0;
public Client(Socket s,int num) {
this.s = s;
this.num = num;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) {
try {
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this);
}
}
@Override
public void run() {
try {
String str = null;
while (!((str = dis.readUTF()).equals("exit"))) {
System.out.println("Form Client " + num + ": " + str);
for(int i=0; i<clients.size(); i++) {
Client c = clients.get(i);
c.send("Form Client " + num + ": " +str);
}
}
System.out.println("a client stoped!——" + num);
dis.close();
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//客服端:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.CharBuffer;
public class ChatClient extends Frame {
TextField text = new TextField();
TextArea ta = new TextArea();
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
String str = null;
boolean isRun = false;
Server ser = new Server();
Thread t = new Thread(ser);
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setSize(300, 300);
setLocation(300, 300);
add(text, BorderLayout.SOUTH);
add(ta, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
closeConnet();
System.exit(0);
}
});
text.addActionListener(new ChatMontions());
setVisible(true);
connet();
isRun = true;
t.start();
}
public void connet() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("connet!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void closeConnet() {
try {
isRun = false;
dos.writeUTF("exit");
t.join();
dos.close();
dis.close();
s.close();
}catch(InterruptedException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void display(String str) {
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private class ChatMontions implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = text.getText().trim();
text.setText("");
display(str);
}
}
private class Server implements Runnable {
@Override
public void run() {
try {
while (isRun) {
String str = dis.readUTF();
ta.setText(ta.getText() + "\n" + str);
}
} catch(EOFException e) {
System.out.println("结束!!");
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
这是最简单的聊天程序,功能也很少,代码一共才200多一点。先启动服务器,再启动多个客户端,这个原理我想不用解释了吧,哦,注释我就不帮你写了,都这么晚了,我该看球了。呵呵
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
/**
* @param args
*/
boolean start = false;
ServerSocket ss = null;
Socket s = null;
List<Client> clients = new ArrayList<Client>();
public static void main(String[] args) {
new ChatServer().start();
}
public void start() {
try {
ss = new ServerSocket(8888);
start = true;
int num = 0;
while (start) {
num ++;
s = ss.accept();
System.out.println("a client connet! " + num);
Client c = new Client(s,num);
new Thread(c).start();
clients.add(c);
}
ss.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private class Client implements Runnable {
private Socket s = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private int num = 0;
public Client(Socket s,int num) {
this.s = s;
this.num = num;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) {
try {
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this);
}
}
@Override
public void run() {
try {
String str = null;
while (!((str = dis.readUTF()).equals("exit"))) {
System.out.println("Form Client " + num + ": " + str);
for(int i=0; i<clients.size(); i++) {
Client c = clients.get(i);
c.send("Form Client " + num + ": " +str);
}
}
System.out.println("a client stoped!——" + num);
dis.close();
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//客服端:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.CharBuffer;
public class ChatClient extends Frame {
TextField text = new TextField();
TextArea ta = new TextArea();
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
String str = null;
boolean isRun = false;
Server ser = new Server();
Thread t = new Thread(ser);
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setSize(300, 300);
setLocation(300, 300);
add(text, BorderLayout.SOUTH);
add(ta, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
closeConnet();
System.exit(0);
}
});
text.addActionListener(new ChatMontions());
setVisible(true);
connet();
isRun = true;
t.start();
}
public void connet() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("connet!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void closeConnet() {
try {
isRun = false;
dos.writeUTF("exit");
t.join();
dos.close();
dis.close();
s.close();
}catch(InterruptedException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void display(String str) {
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private class ChatMontions implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = text.getText().trim();
text.setText("");
display(str);
}
}
private class Server implements Runnable {
@Override
public void run() {
try {
while (isRun) {
String str = dis.readUTF();
ta.setText(ta.getText() + "\n" + str);
}
} catch(EOFException e) {
System.out.println("结束!!");
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
这是最简单的聊天程序,功能也很少,代码一共才200多一点。先启动服务器,再启动多个客户端,这个原理我想不用解释了吧,哦,注释我就不帮你写了,都这么晚了,我该看球了。呵呵
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询