请问这个怎么编译使用。谢谢
/*长城宽带认证登陆程序Filename: LoginApp.javaAuthor: starfish */import java.io.*;import java.net.*;import java.util.StringTokenizer;public class LoginApp { private Socket sock; private InetAddress server; BufferedReader in; BufferedWriter out; static final int MAX_TRY = 10; static final int PORT = 6500; static final String SERVER_IP = "10.156.252.5"; static final int TIME_OUT = 40000; public String loginToServer(String user, String password) { String msg = "login " + user + " " + password + " 0"; return sendMessage(msg); } public String logoutToServer(String userID) { if (userID == null) { return "Failed. userID is null."; } else { String msg = "logout " + userID; return sendMessage(msg); } } public String sendMessage(String msg) { String result = "sendMessage failed! "; // for debug // System.out.println("sendMessage: " + msg); if (sock == null) { int i; for (i = 0; i < MAX_TRY; i++) {System.out.println("Initializing socket ...");if (initSock()) break; } if (i == MAX_TRY) {return "Initialize socket failed. Please try again."; } } try { out.write(encode(msg)); out.flush(); char ac[] = new char; try {int i = in.read(ac, 0, 128);if(i > 0 && i < 128) { result = new String(ac, 0, i); result = decode(result);} } catch (SocketException e) {e.printStackTrace();result ="sendMessage failed!"; } catch (IOException e) {e.printStackTrace();result = "sendMessage failed!"; } } catch (IOException e) { e.printStackTrace(); result = "sendMessage failed!"; } finally { closeSock(); } return result; } public boolean initSock() {try { server = InetAddress.getByName(SERVER_IP); sock = new Socket(server, PORT); sock.setTcpNoDelay(true); sock.setSoTimeout(TIME_OUT); } catch (UnknownHostException e) { return false; } catch (SocketException e) { if(sock != null) {try { sock.close(); sock = null;} catch (IOException e2) {} } return false; } catch(IOException e) { return false; } try { in = new BufferedReader(new InputStreamReader(sock.getInputStream())); out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())); } catch (IOException e) { try {sock.close();sock = null; } catch (IOException e2) { } return false; } return true; } public void closeSock() { if (sock == null) return; try { in.close(); out.close(); sock.close(); } catch (IOException e) {} sock = null; } public String encode(String str) { byte data[]; String result; int i, j, k, l; try { data = str.getBytes("GBK"); } catch (IOException e) { try {data = str.getBytes("ISO-8859-1"); } catch (IOException e2) { return "The operation system is not compatible."; } } for (i = 0; i < data.length; i++) { j = (data & 3) << 2; k = (data & 0xc) >> 2; l = data & 0xf0; data = (byte)(j | k | l); data = (byte)(data + 1); } try { result = new String(data, "GBK"); } catch (IOException e) { try {result = new String(data, "ISO-8859-1"); } catch (IOException e2) {return "The operation system is not compatible."; } } return result; } public String decode(String str) { byte data[]; String result;int i, j, k, l; try { data = str.getBytes("GBK"); } catch (IOException e) { try {data = str.getBytes("ISO-8859-1"); } catch (IOException e2) {return "The operation system is not compatible."; } } for(i = 0; i < data.length; i++) { data = (byte)(data - 1); j = (data & 3) << 2; k = (data & 0xc) >> 2; l = data & 0xf0; data = (byte)(j | k | l); } try { result = new String(data, "GBK"); } catch (IOException e) { try {result = new String(data, "ISO-8859-1"); } catch (IOException e2) {return "The operation system is not compatible."; } } if (result.startsWith("failed! 8 ")) { result = str.substring(10); } return result; } public static void printUsage() { System.err.println("Usage: "); System.err.println(" java LoginApp login uname password"); System.err.println(" java LoginApp logout userID"); System.exit (1); } public static void main (String args[]) { String answer, user, password, uid; LoginApp app = new LoginApp(); if (args.length == 2) { if (args.compareToIgnoreCase("logout") == 0) {uid = args;answer = app.logoutToServer(uid);if (answer.startsWith("OK!")) { System.out.println("Logout successfully.");} else { System.out.println("Logout failed."); System.out.println("The answer ofthe server is: " + answer);} } else {printUsage(); } } else if (args.length == 3) { if (args.compareToIgnoreCase("login") == 0) {user = args;password = args; answer = app.loginToServer(user, password);if (answer.startsWith("OK!")) { StringTokenizer stk = new StringTokenizer(answer); stk.nextToken(); uid = new String(stk.nextToken()); System.out.println("Login successfully. Your user ID is: " + uid);} else { System.out.println("Login failed."); System.out.println("The answer of the server is: " + answer);} } else {printUsage(); } } else { printUsage(); } }} javac LoginApp.java拜托下次用code格式化一下代码。
页:
[1]