Client
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ClientBio {
private static String USER_CONTENT_SPILIT = "#@#";
public static final int port = 8081;
public String hostname = "127.0.0.1";
Socket socket = null;
String userName = "";
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public static void main(String[] args) {
ClientBio client = new ClientBio();
new Thread(client.formatStart()).start();
}
public Start formatStart(){
Start start = new Start();
return start;
}
class Cthread extends Thread {
public void run() {
try {
BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
String msg2 = "客户1号";
pw.println(msg2);
while (true) {
msg2 = re.readLine();
pw.println(msg2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Start implements Runnable{
public void run() {
try {
socket = new Socket(hostname, port);
new Cthread().start();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg;
while ((msg = br.readLine()) != null) {
System.out.println(msg);
}
} catch (Exception e) {
}
}
}
}
Server
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class ServerBio {
private static String USER_CONTENT_SPILIT = "#@#";
public static int port = 8081;
List<ClientBio> clients;
ServerSocket server;
public static void main(String[] args) {
ServerBio server = new ServerBio();
server.start();
}
public void start(){
new AcceptThread().start();
}
class Mythread extends Thread {
Socket ssocket;
private BufferedReader br;
private PrintWriter pw;
public String msg;
public Mythread(Socket s) {
ssocket = s;
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(ssocket.getInputStream()));
while ((msg = br.readLine()) != null) {
msg = "【" + ssocket.getInetAddress() +":" + ssocket.getLocalPort() + "】say:" + msg;
sendMsg();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void sendMsg() {
try {
System.out.println(msg);
for (int i = clients.size() - 1; i >= 0; i--) {
pw = new PrintWriter(clients.get(i).getSocket().getOutputStream(), true);
pw.println(msg);
pw.flush();
}
} catch (Exception ex) {
}
}
}
class AcceptThread extends Thread {
public void run() {
try {
clients = new ArrayList<ClientBio>();
server = new ServerSocket(port);
while (true) {
Socket socket = server.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String first = br.readLine();
System.out.println(first );
if (first.length() > 0){
String[] arrayContent = first.split(USER_CONTENT_SPILIT);
if (arrayContent != null && arrayContent.length >= 1) {
String name = arrayContent[0];
ClientBio client = new ClientBio();
client.setUserName(name);
client.setSocket(socket);
clients.add(client);
String message = "welcome " + name + " to chat room! in "+ socket.getLocalAddress() + ":"+ socket.getLocalPort() +"! Online numbers:" + clients.size();
System.out.println(message);
}
} else {
continue;
}
Mythread mythread = new Mythread(socket);
mythread.start();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}