package com.dasenlin.chart;
import java.io.BufferedReader;
import java.io.IOException;
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 ChartServer {
private List<ServerThread> clients = null;
public static void main(String[] args) {
System.out.println("服务器已经启动!");
new ChartServer().startUp();
}
/**
*监听客户端的请求
*/
private void startUp(){
ServerSocket ss=null;
try {
ss = new ServerSocket(7788);
clients=new ArrayList<ServerThread>();
while(true){
Socket s = ss.accept();
Thread t = new Thread(new ServerThread(s));
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(null!=ss)ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//内部类
class ServerThread implements Runnable{
private Socket socket;
private BufferedReader in=null;
private PrintWriter out=null;
private String name=null;
public ServerThread(Socket socket) throws IOException {
this.socket=socket;
this.in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.out = new PrintWriter(socket.getOutputStream());
this.name="["+socket.getInetAddress()+":"+socket.getPort()+"]";
clients.add(this);
sendMessage(name+"上线了");
}
@Override
public void run() {
try {
this.receiveClient();
} catch (IOException e) {
e.printStackTrace();
}
}
//接受消息
public void receiveClient() throws IOException{
String msg=null;
while((msg=in.readLine())!=null){
if("quit".equals(msg)){
this.out.print("disconnect");
}
sendMessage(msg);
}
}
//发送消息
public void sendMessage(String msg){
for(ServerThread st:clients){
st.out.println(msg);
}
}
//下线
public void stop(){
this.sendMessage(name+"下线");
clients.remove(this);
}
}
}
package com.dasenlin.chart;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ChartClient {
private Socket s=null;
private BufferedReader in=null;
private PrintWriter out=null;
private BufferedReader sbr=null;
private boolean flag=true;
public static void main(String[] args) {
new ChartClient().startUp();
}
//主线程用于向服务端发送数据
private void startUp(){
try {
s = new Socket("127.0.0.1",7788);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream());
sbr = new BufferedReader(new InputStreamReader(System.in));
new Thread(new ClientThread()).start();
sendMessage();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(null!=s)s.close();
if(null!=in)in.close();
if(null!=out)out.close();
if(null!=sbr)sbr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void sendMessage(){
String str=null;
try {
while(flag){
str=sbr.readLine();
if(null==str){
this.out.print("quit");
break;
};
this.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void receiveMessage(){
String str=null;
try {
str=this.in.readLine();
if(null==str)return;
if("disconnect".equalsIgnoreCase(str)){
flag=false;
}
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
class ClientThread implements Runnable{
@Override
public void run() {
while(flag){
receiveMessage();
}
}
}
}