利用Socket做的客户端与服务端聊天系统(多多支持)
/**
* 类名:ChatClient
* 时间:2009-11-11
* 功能:这是一个使用套接字来实现聊天的示例 客户端
*
* @author 曾智宇
*
*/
public class ChatClient {
private static final int PORT = 9090;
private static ExecutorService exec = Executors.newCachedThreadPool();
private static String userName;
public static void main(String[] args) {
new ChatClient();
}
/**
* 构造方法
*/
public ChatClient() {
try {
System.out.print("请输入姓名:");
Scanner input = new Scanner(System.in);
userName = input.next();
Socket socket = new Socket("localhost", PORT);
exec.execute(new Sender(socket));
System.out.println("[" + userName + "]你好!欢迎进入聊天室!");
BufferedReader br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
String msg; // 要发送的消息
while ((msg = br.readLine()) != null) {
System.out.println(msg);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 发送消息类
*
* @author Administrator
*
*/
static class Sender implements Runnable {
private Socket socket;
/**
* 构造方法
*
* @param socket
*/
public Sender(Socket socket) {
this.socket = socket;
}
/**
* 主体方法
*/
public void run() {
try {
String msg;
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
while (true) {
msg = br.readLine();
msg = "[" + userName + "]说:" + msg;
pw.println(msg);
if (msg.trim().equals("bye")) {
pw.close();
br.close();
exec.shutdown();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* 类名:ChatClient
* 时间:2009-11-11
* 功能:这是一个使用套接字来实现聊天的示例 服务端
*
* @author 曾智宇
*
*/
public class ChatServer {
private static final int PORT = 8765; // 端口号
private static List<Socket> clientList = new ArrayList<Socket>(); // 泛型集合保存连接对象
private ExecutorService exec; // 返回新创建的线程池
private ServerSocket server; // 套接字
public static void main(String[] args) {
new ChatServer();
}
public ChatServer() {
try {
server = new ServerSocket(PORT);
exec = Executors.newCachedThreadPool();
System.out.println("服务器已启动");
Socket client = null;
while (true) {
client = server.accept(); // 接受客户端连接
clientList.add(client);
exec.execute(new CharTask(client));
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class CharTask implements Runnable {
private Socket socket;
private BufferedReader br;
private PrintWriter pw;
String msg;
/**
* 构造方法
*
* @param socket
* @throws IOException
*/
public CharTask(Socket socket) throws IOException {
this.socket = socket;
br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
msg = "[" + this.socket.getInetAddress() + "]进入了聊天室,当前聊天室有["
+ clientList.size() + "]人";
sendMessage();
}
/**
* 主体方法
*/
public void run() {
try {
while ((msg = br.readLine()) != null) {
if (msg.trim().equals("bye")) {
clientList.remove(socket);
br.close();
pw.close();
msg = "[" + this.socket.getInetAddress()
+ "]离开了聊天室,当前聊天室有[" + clientList.size() + "]人";
sendMessage();
break;
} else {
sendMessage();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 发送消息的方法
*
* @throws IOException
*/
public void sendMessage() throws IOException {
System.out.println(msg);
for (Socket client : clientList) {
pw = new PrintWriter(client.getOutputStream(), true);// 通过现有的
// OutputStream
// 创建新的
// PrintWriter。
pw.println(msg);
}
}
}
}