Java实现简单聊天室的小Demo
Server端:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) {
ServerSocket server = null;
ThreadGroup myGroup = new ThreadGroup("讨论组");
int port = 9999;
boolean flag = true;
try {
server = new ServerSocket(port);
System.out.println("server start success...");
while (flag) {
Socket socket = server.accept();
System.out.println("client->" + socket + "connection successed!");
Thread t = new MyHandler(myGroup,socket);
t.start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (server != null) {
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class MyHandler extends Thread {
private Socket socket;
String encoding = "UTF-8";
BufferedReader inFromClient = null;
PrintWriter outToClient = null;
public MyHandler(ThreadGroup group,Socket socket) {
super(group,"default");
this.socket = socket;
}
public void sendMsg(String line){
outToClient.println(line);
outToClient.flush();
}
public void sendMsgToAll(String line){
ThreadGroup group = Thread.currentThread().getThreadGroup();
int activeCount = group.activeCount();
Thread[] actives = new Thread[activeCount];
group.enumerate(actives);
for(int i = 0;i<actives.length;i++){
MyHandler handler = (MyHandler)actives[i];
if(handler!=this) {
handler.sendMsg(this.getName() + ":" + line);
}
}
}
public void run() {
try {
inFromClient = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), encoding));
outToClient = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream(), encoding));
//先读一次客户端发来的用户名
String username = inFromClient.readLine();
//设置为线程的名字
this.setName(username);
while (true){
String line = inFromClient.readLine();
sendMsgToAll(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inFromClient != null) {
try {
inFromClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outToClient != null) {
try {
outToClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
Client端:
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClient {
public static void main(String[] args) {
String ip = "127.0.0.1";
int port = 9999;
String encoding = "UTF-8";
Socket socket = null;
BufferedReader inFromConsole = null;
PrintWriter outToConsole = null;
BufferedReader inFromServer = null;
PrintWriter outToServer = null;
try {
socket = new Socket(ip, port);
System.out.println("连接成功!!!");
//从控制台读
inFromConsole = new BufferedReader(new InputStreamReader(System.in, encoding));
//写入到服务器
outToServer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), encoding));
//从服务器读
inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream(), encoding));
//写入到控制台
outToConsole = new PrintWriter(new OutputStreamWriter(System.out, encoding));
//让用户输入自己的名字,发送给服务器:
outToConsole.println("请输入您的用户名:");
outToConsole.flush();
String username = inFromConsole.readLine();
//发送给服务器
outToServer.println(username);
outToServer.flush();
outToConsole.println("用户名设置完毕,您现在可以给群里其他人发送消息了:");
outToConsole.flush();
//利用多线程,一个写,一个读
ClientWriter writer = new ClientWriter(inFromConsole,outToServer);
ClientReader reader = new ClientReader(inFromServer,outToConsole);
writer.start();
reader.start();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ClientReader extends Thread {
BufferedReader inFromServer = null;
PrintWriter outToConsole = null;
Socket socket = null;
public ClientReader(BufferedReader inFromServer, PrintWriter outToConsole) {
this.inFromServer = inFromServer;
this.outToConsole = outToConsole;
}
public void run() {
String line = null;
try {
while (true) {
line = inFromServer.readLine();
outToConsole.println(line);
outToConsole.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(inFromServer!=null){
try {
inFromServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outToConsole!=null){
outToConsole.close();
}
}
}
}
class ClientWriter extends Thread {
BufferedReader inFromConsole = null;
PrintWriter outToServer = null;
public ClientWriter(BufferedReader inFromConsole, PrintWriter outToServer) {
this.inFromConsole = inFromConsole;
this.outToServer = outToServer;
}
public void run() {
while (true) {
String msg = null;
try {
while (true) {
msg = inFromConsole.readLine();
outToServer.println(msg);
outToServer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(inFromConsole!=null){
try {
inFromConsole.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outToServer!=null){
outToServer.close();
}
}
}
}
}