QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 494|回复: 1

请问这个怎么编译使用。谢谢

[复制链接]
发表于 2004-9-30 20:45:18 | 显示全部楼层 |阅读模式
/*  长城宽带认证登陆程序  Filename: LoginApp.java  Author: 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[128];     try {  int i = in.read(ac, 0, 12;  if(i > 0 && i < 12 {      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[0].compareToIgnoreCase("logout") == 0) {  uid = args[1];  answer = app.logoutToServer(uid);  if (answer.startsWith("OK!")) {      System.out.println("Logout successfully.");  } else {      System.out.println("Logout failed.");      System.out.println("The answer of  the server is: " + answer);  }          } else {  printUsage();     } } else if (args.length == 3) {     if (args[0].compareToIgnoreCase("login") == 0) {  user = args[1];  password = args[2];    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();    }      }}
发表于 2004-9-30 22:01:48 | 显示全部楼层
javac LoginApp.java
拜托下次用code格式化一下代码。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-16 03:56 , Processed in 0.042621 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表