<strong>服务器端代码</strong>
public class Service {
private ServerSocket serverSocket;
private int port = 6646;
private static List<PrintWriter> allOutput = new ArrayList<PrintWriter>();
public Service(){
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
try{
Service server = new Service();
server.start();
}catch(Exception e){
}
}
/**
* 启动服务器
*/
public void start(){
try {
while(true){
System.out.println("等待客户端连接。。。");
Socket socket = serverSocket.accept(); //监听端口,等待客户端连接,一旦连接返回该连接的Socket
System.out.println("客户端已连接");
ClientHandler handler = new ClientHandler(socket);
Thread thread = new Thread(handler); //创建一个线程负责与该客户端的交互
thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*将输出流添加到输出队列
*/
private synchronized void addOutput(PrintWriter pw){
allOutput.add(pw);
}
/**
* 从输出队列删除输出流
*/
private synchronized void removeOutput(PrintWriter pw){
allOutput.remove(pw);
}
/**
* 向所有客户端发送消息
*/
private synchronized void sendAll(String message){
for(PrintWriter p:allOutput){
p.println(message);
p.flush();
}
}
/**
*内部类,实现Runnable接口,完成与客户端交互的工作
*/
private class ClientHandler implements Runnable{
private Socket socket;
private String host;
public ClientHandler(Socket socket){
this.socket = socket;
this.host = socket.getInetAddress().getHostAddress();
}
public void run() {
BufferedReader bfr = null;
PrintWriter pw = null;
String message;
try{
bfr = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8")); //获得socket的输入流
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8")); //获得socket的输出流
addOutput(pw);
while((message = bfr.readLine()) != null){
sendAll(host+":"+message);
}
}catch(Exception e){
e.printStackTrace();
}finally{
removeOutput(pw);
System.out.println("客户端断开连接");
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
客户端代码
public class Client {
private Socket socket;
private String host = "127.0.0.1";
private int port = 6646;
public Client(){
try{
socket = new Socket(host,port);
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
Client client = new Client();
client.start();
}
/**
* 启动客户端
*/
public void start(){
Thread thread = new Thread(new GetMessage()); //启动一个线程读取服务端发来的信息
thread.start();
System.out.println("已连接服务器");
try {
OutputStream os = socket.getOutputStream(); //获得Socket 的输出流
OutputStreamWriter osw = new OutputStreamWriter(os,"UTF-8");
PrintWriter pw = new PrintWriter(osw);
Scanner sc = new Scanner(System.in);
String message;
while(true){
message =sc.next();
if(message !=null&&message.equals("exit")){
break;
}
pw.println(message); //将输入的字符串发送到服务端
pw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
System.out.println("与服务器连接断开");
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*读取服务端发来的消息并输出
*/
private class GetMessage implements Runnable{
public void run(){
try {
String message;
InputStream is = socket.getInputStream(); //获得Socket 的输入流
InputStreamReader isr = new InputStreamReader(is,"UTF-8");
BufferedReader bf = new BufferedReader(isr);
while((message = bf.readLine()) != null){
System.out.println(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}