Tag
Hash :
0c191b75
Author :
Date :
2014-09-19T18:06:26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/*
*
* Copyright (C) Andrew Smith 2012-2013
*
* Usage: java API command ip port
*
* If any are missing or blank they use the defaults:
*
* command = 'summary'
* ip = '127.0.0.1'
* port = '4028'
*
*/
import java.net.*;
import java.io.*;
class API
{
static private final int MAXRECEIVESIZE = 65535;
static private Socket socket = null;
private void closeAll() throws Exception
{
if (socket != null)
{
socket.close();
socket = null;
}
}
public void display(String result) throws Exception
{
String value;
String name;
String[] sections = result.split("\\|", 0);
for (int i = 0; i < sections.length; i++)
{
if (sections[i].trim().length() > 0)
{
String[] data = sections[i].split(",", 0);
for (int j = 0; j < data.length; j++)
{
String[] nameval = data[j].split("=", 2);
if (j == 0)
{
if (nameval.length > 1
&& Character.isDigit(nameval[1].charAt(0)))
name = nameval[0] + nameval[1];
else
name = nameval[0];
System.out.println("[" + name + "] =>");
System.out.println("(");
}
if (nameval.length > 1)
{
name = nameval[0];
value = nameval[1];
}
else
{
name = "" + j;
value = nameval[0];
}
System.out.println(" ["+name+"] => "+value);
}
System.out.println(")");
}
}
}
public void process(String cmd, InetAddress ip, int port) throws Exception
{
StringBuffer sb = new StringBuffer();
char buf[] = new char[MAXRECEIVESIZE];
int len = 0;
System.out.println("Attempting to send '"+cmd+"' to "+ip.getHostAddress()+":"+port);
try
{
socket = new Socket(ip, port);
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.print(cmd.toCharArray());
ps.flush();
InputStreamReader isr = new InputStreamReader(socket.getInputStream());
while (0x80085 > 0)
{
len = isr.read(buf, 0, MAXRECEIVESIZE);
if (len < 1)
break;
sb.append(buf, 0, len);
if (buf[len-1] == '\0')
break;
}
closeAll();
}
catch (IOException ioe)
{
System.err.println(ioe.toString());
closeAll();
return;
}
String result = sb.toString();
System.out.println("Answer='"+result+"'");
display(result);
}
public API(String command, String _ip, String _port) throws Exception
{
InetAddress ip;
int port;
try
{
ip = InetAddress.getByName(_ip);
}
catch (UnknownHostException uhe)
{
System.err.println("Unknown host " + _ip + ": " + uhe);
return;
}
try
{
port = Integer.parseInt(_port);
}
catch (NumberFormatException nfe)
{
System.err.println("Invalid port " + _port + ": " + nfe);
return;
}
process(command, ip, port);
}
public static void main(String[] params) throws Exception
{
String command = "summary";
String ip = "127.0.0.1";
String port = "4028";
if (params.length > 0 && params[0].trim().length() > 0)
command = params[0].trim();
if (params.length > 1 && params[1].trim().length() > 0)
ip = params[1].trim();
if (params.length > 2 && params[2].trim().length() > 0)
port = params[2].trim();
new API(command, ip, port);
}
}