栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java:我没有收到其他客户端的消息吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java:我没有收到其他客户端的消息吗?

这是一个示例-
尚不完整,但应使您了解如何将输出多播到许多侦听客户端。有更好的方法来执行此操作,但是我写的类似于您执行套接字的方式。它还在许多地方都缺少错误检查,因此我将其留给读者练习。还编写了此代码,以便可以在Java
1.6或更高版本上使用。

该代码使用Server对象中维护的已连接客户端的列表。从一个客户端收到输入时,输出将多播到“客户端”列表中的每个客户端。编写是通过Client类中的write方法完成的。

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.linkedList;import java.util.Iterator;import java.util.List;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class MulticastEchoServer {    List<Client> clientList = new linkedList<Client>();    ExecutorService executor;    int port = 40480;    int max = 10;    public MulticastEchoServer() {        this.executor = Executors.newFixedThreadPool(max);    }    public void writeToAllClients(String string) throws IOException {        // Multiple threads access this so it must be in synchronized block        synchronized (this.clientList) { Iterator<Client> iter = this.clientList.iterator(); while (iter.hasNext())     iter.next().write(string);        }    }    public void addClient(Client client) {        // Multiple threads access this so it must be in synchronized block        synchronized (this.clientList) { clientList.add(client);        }    }    public void removeClient(Client client) {        // Multiple threads access this so it must be in synchronized block        synchronized (this.clientList) { clientList.remove(client);        }    }    public void listen() {        try { ServerSocket server = new ServerSocket(port); System.out.println("server started and listening for connections"); while (true) {     try {         Socket socket = server.accept();         System.out.print("connection accepted" + "n");         Client newClient = new Client(this, socket);         this.addClient(newClient);         this.executor.execute(newClient);     } catch (IOException e) {         e.printStackTrace();     } }        } catch (Exception e) { e.printStackTrace();        }    }    public static void main(String[] args) {        new MulticastEchoServer().listen();    }    private class Client implements Runnable {        Socket socket;        PrintWriter writer;        BufferedReader reader;        MulticastEchoServer server;        public Client(MulticastEchoServer server, Socket socket) throws IOException { this.server = server; this.socket = socket; this.writer = new PrintWriter(this.socket.getOutputStream()); this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));        }        synchronized public void write(String string) throws IOException { writer.write(string); writer.flush();        }        public void close() { this.writer.close(); try {     this.reader.close(); } catch (IOException e) { } try {     this.socket.close(); } catch (IOException e) { }        }        @Override        public void run() { System.out.println("Client Waiting"); String inString = null; try {     while ((inString = this.reader.readLine()) != null) {         this.server.writeToAllClients(inString + "n");         System.out.println(inString);     } } catch (IOException e1) { } server.removeClient(this); this.close(); System.out.println("Client Closed");        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/428869.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号