利用Socket做的客户端与服务端聊天系统(多多支持)

qq627819393 2009-11-12 01:10:09
/**
* 类名: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);
}
}

}
}
...全文
104 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
学习啦 谢谢分享

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧