DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUGĀ· RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT <http://issues.apache.org/bugzilla/show_bug.cgi?id=44108>. ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED ANDĀ· INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=44108 ------- Additional Comments From [EMAIL PROTECTED] 2007-12-19 15:33 ------- Here is a refactored and bug fixed version of the 2nd half of this class - the SocketHandler part. This corrects the bug that causes it to miss messages, cleans up some areas of the code where the print writers were not closed properly, and makes the code safe for multi-threaded operation - the previous code was not safe, because it was using two vectors that were assumed to be the same size - but in reality, they could be different sizes depending on how the threads stop and start. You may need to remove the generics, if you still need to support 1.4. // ---------------------------------------------------------- SocketHandler: /** * The SocketHandler class is used to accept connections from clients. It is * threaded so that clients can connect/disconnect asynchronously. */ protected class SocketHandler extends Thread { private Vector<TelnetClient> connections = new Vector<TelnetClient>(); private ServerSocket serverSocket; private int MAX_CONNECTIONS = 20; public void finalize() { close(); } /** * make sure we close all network connections when this handler is * destroyed. * * @since 1.2.15 */ public void close() { for (Enumeration<TelnetClient> e = connections.elements(); e.hasMoreElements();) { try { e.nextElement().close(); } catch (Exception ex) { } } try { serverSocket.close(); } catch (Exception ex) { } } /** sends a message to each of the clients in telnet-friendly output. */ public void send(String message) { Vector<TelnetClient> lostClients = new Vector<TelnetClient>(); for (Enumeration<TelnetClient> e = connections.elements(); e.hasMoreElements();) { TelnetClient tc = e.nextElement(); tc.writer.print(message); if (tc.writer.checkError()) { // The client has closed the connection, remove it from our list: lostClients.add(tc); } } for (Enumeration<TelnetClient> e = lostClients.elements(); e.hasMoreElements();) { TelnetClient tc = e.nextElement(); tc.close(); connections.remove(tc); } } /** * Continually accepts client connections. Client connections are * refused when MAX_CONNECTIONS is reached. */ public void run() { while (!serverSocket.isClosed()) { try { Socket newClient = serverSocket.accept(); TelnetClient tc = new TelnetClient(newClient); if (connections.size() < MAX_CONNECTIONS) { connections.addElement(tc); tc.writer.println("TelnetAppender v1.0 (" + connections.size() + " active connections)"); tc.writer.println(); tc.writer.flush(); } else { tc.writer.println("Too many connections."); tc.writer.flush(); tc.close(); } } catch (Exception e) { if (!serverSocket.isClosed()) { LogLog.error("Encountered error while in SocketHandler loop.", e); } break; } } try { serverSocket.close(); } catch (IOException ex) { } } public SocketHandler(int port) throws IOException { serverSocket = new ServerSocket(port); setName("TelnetAppender-" + getName() + "-" + port); } } protected class TelnetClient { private PrintWriter writer; private Socket connection; protected TelnetClient(Socket socket) throws IOException { connection = socket; writer = new PrintWriter(connection.getOutputStream()); } protected void close() { if (writer != null) { try { writer.flush(); writer.close(); } catch (Exception e) { // noop } } if (connection != null) { try { connection.close(); } catch (Exception e) { // noop } } } } -- Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are the assignee for the bug, or are watching the assignee. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
