【文件属性】:
文件名称:JAVA实现SOCKET聊天
文件大小:3KB
文件格式:RAR
更新时间:2016-01-28 12:38:44
JAVA聊天
用JAVA语言实现网络聊天import java.io.*;
import java.net.*;
import java.util.*;
public class ChatHandler implements Runnable {
protected Socket socket;
protected ObjectInputStream dataIn;
protected ObjectOutputStream dataOut;
protected Thread listener;
protected static Vector handlers = new Vector();
private boolean keepListening = true;
public ChatHandler(Socket socket) {
this.socket = socket;
}
public synchronized void start() {
if (listener == null) {
try {
dataIn = new ObjectInputStream(socket.getInputStream());
dataOut = new ObjectOutputStream(socket.getOutputStream());
listener = new Thread(this);
listener.start();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
public synchronized void stop() {
if (listener != null) {
try {
if (listener != Thread.currentThread())
listener.interrupt();
listener = null;
dataOut.close();
socket.close();
} catch (IOException ignored) {
}
}
}
public void run() {
String message = "";
try {
handlers.addElement(this);
while (keepListening) {
message = (String) dataIn.readObject();
if (message.equals("DISCONNECT")) {
dataOut.writeObject(message);
dataOut.flush();
stopListening();
} else
broadcast(message);
}
} catch (ClassNotFoundException classNotFoundException) {
} catch (EOFException ignored) {
} catch (IOException ex) {
if (listener == Thread.currentThread())
ex.printStackTrace();
} finally {
handlers.removeElement(this);
}
try {
dataIn.close();
}
catch (IOException ioException) {
ioException.printStackTrace();
}
stop();
}
protected void broadcast(String message) {
synchronized (handlers) {
Enumeration enumer = handlers.elements();
while (enumer.hasMoreElements()) {
ChatHandler handler = (ChatHandler) enumer.nextElement();
try {
handler.dataOut.writeObject(message);
handler.dataOut.flush();
} catch (IOException ex) {
handler.stop();
}
}
}
}
public void stopListening() {
keepListening = false;
}
}
【文件预览】:
ChatHandler.java
MessengerServer.java
MessengerClient.java
网友评论
- 东西确实不错,就是不知道怎么打包