1 5 6 import java.net.*; 7 import java.io.*; 8 9 public class debugserver { 10 static boolean closed = false; 11 12 public static void main(String [] args) throws IOException { 13 14 ServerSocket serverSocket = null; 15 int port= 5559; 16 if (args.length > 0) { 17 try { 18 port = Integer.parseInt(args[0]); 19 } catch (NumberFormatException e) { 20 System.err.println("could not parse port number "+args[0]); 21 } 22 } 23 24 while (true) { 25 26 try { 27 serverSocket = new ServerSocket(port); 28 } catch (IOException e) { 29 System.err.println("Could not listen on port "+port); 30 System.exit(1); 31 } 32 33 34 35 closed = false; 36 try { 37 System.out.println("Listening on port "+port); 38 final Socket clientSocket = serverSocket.accept(); 39 40 System.out.println("Accepting connection on port "+port); 41 42 PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); 43 final BufferedReader in = new BufferedReader( 44 new InputStreamReader( 45 clientSocket.getInputStream())); 46 String inputLine, outputLine; 47 48 int seqnum = 1; 49 50 new Thread () { 52 public void run() { 53 try { 54 FileWriter of = new FileWriter("lzdebug.log"); 55 PrintWriter outf = new PrintWriter(of); 56 while (true) { 57 try { 58 int ch = in.read(); 59 if (ch == -1) { 60 debugserver.closed = true; 61 return; 62 } 63 if (ch == 0) { 64 System.out.println(""); 65 outf.println(""); 66 outf.flush(); 67 } else { 68 System.out.write(ch); 69 outf.write(ch); 70 } 71 if (clientSocket.isClosed()) { 72 debugserver.closed = true; 73 return; 74 } 75 } catch (IOException e) { 76 System.out.println(e); 77 debugserver.closed = true; 78 return; 79 } 80 } 81 } catch (IOException e) { } 82 } 83 }.start(); 84 85 if (args.length > 1) { 87 for (int n = 1; n < args.length; n++) { 88 String fname = args[n]; 89 try { 90 FileInputStream fis = new FileInputStream(fname); 91 byte b[] = new byte[fis.available()]; 92 fis.read(b); 93 String cmd = new String (b); 94 System.out.println("Sent: "+cmd); 95 out.write("<exec seq='"+(seqnum++)+"'>"+escapeXml(cmd)+"</exec>\000"); 96 out.flush(); 97 } catch (IOException e) { 98 System.out.println(e); 99 } 100 } 101 } 102 103 BufferedReader tty = new BufferedReader(new InputStreamReader(System.in)); 104 String expr; 105 106 while (!closed && (expr = tty.readLine()) != null) { 107 if (expr.trim().equals("")) continue; 108 out.write("<eval seq='"+(seqnum++)+"'>"+ escapeXml(expr)+"</eval>\000"); 109 out.flush(); 110 } 111 112 113 out.close(); 114 in.close(); 115 clientSocket.close(); 116 serverSocket.close(); 117 System.out.println("Connection closed"); 118 } catch (RuntimeException e) { 119 System.out.println("Exception "+e); 120 } 121 } 122 } 123 124 131 public static String escapeXml(String s) { 132 if (s == null) return null; 133 StringBuffer sb = new StringBuffer (); 134 for(int i=0; i<s.length(); i++) { 135 char c = s.charAt(i); 136 if (c == '<') { 137 sb.append("<"); 138 } else if (c == '>') { 139 sb.append(">"); 140 } else if (c == '\'') { 141 sb.append("'"); 142 } else if (c == '&') { 143 sb.append("&"); 144 } else if (c == '"') { 145 sb.append("""); 146 } else { 147 sb.append(c); 148 } 149 } 150 return sb.toString(); 151 } 152 153 154 } 155 | Popular Tags |