smart360使用API - java

发布时间:2016-12-6 10:48:59 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"smart360使用API - java",主要涉及到smart360使用API - java方面的内容,对于smart360使用API - java感兴趣的同学可以参考一下。

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import java.util.Random; import java.io.DataInputStream;  import java.io.DataOutputStream;  public class client1 {     public static char CMD_JOIN     = 0x01;     public static char CMD_DATA     = 0x02;     public static char CMD_REDIRECT = 0x03;     public static char CMD_ERROR    = 0xFE;     public static char CMD_OK       = 0xFC;         public static char DEV_SEND     = 0x01;     public static char DEV_RECV     = 0x02;         // Get Slaver Host Information     public static String GetHost(String ip, int port, char dev, char[] appid)     {         char[] host = new char[255];         host[0]     = '\0';         try         {    Socket socket        = new Socket (ip, port );             DataOutputStream dos = new DataOutputStream(socket.getOutputStream());    DataInputStream br   = new DataInputStream ( socket.getInputStream());             byte[] buf           = new byte[255];                         // apply to join appid pack             buf[0] = (byte)CMD_JOIN;           /*CMD*/             buf[1] = (byte)dev;                /*the type of device*/             for (int i = 0; i < 16; i++)             {                 buf[i + 2] = (byte)appid[i];             }                dos.write ( buf, 0, 18 );    dos.flush();                         int size = br.read(buf);             buf[size] = '\0';             if (buf[0] == (byte)CMD_REDIRECT)    /*return redirect service ip and port*/             {                  for(int i = 0; i < size; i++)                 {                     host[i] = (char)buf[i+1];                 }                }    br.close();    dos.close();    socket.close();         }         catch (IOException e)         {         }                 return new String(host);     }         // Send Data to Appid     public static void SendData(String ip, int port, char[] appid)     {         char[] host = new char[255];         host[0]     = '\0';         Random random=new Random();         try         {    Socket socket        = new Socket (ip, port );             DataOutputStream dos = new DataOutputStream(socket.getOutputStream());    DataInputStream br   = new DataInputStream ( socket.getInputStream());             byte[] buf           = new byte[255];                         // apply to join appid pack             buf[0] = (byte)CMD_JOIN;          /*CMD*/             buf[1] = (byte)DEV_SEND;          /*as send device*/             for (int i = 0; i < 16; i++)             {                 buf[i + 2] = (byte)appid[i];             }                dos.write ( buf, 0, 18 );    dos.flush();                         int size = br.read(buf);             buf[size] = '\0';             int buf_end = 0;                         // after joining, will return CMD_OK pack             if (buf[0] == (byte)CMD_OK)             {                 while (true)                 {                     // constructing random number as an example pack                     char[] data = ("" + random.nextFloat()).toCharArray();                     int data_len = String.valueOf(data).length();                     buf[0] = (byte)CMD_DATA;                              /*CMD of sending data*/                     for (buf_end = 1; buf_end < data_len; buf_end++)      /*data in sending pack*/                     {                         buf[buf_end] = (byte)data[buf_end - 1];                     }                     buf[buf_end] = '\n';                     buf[buf_end + 1] = '\0';                     dos.write ( buf, 0, data_len + 1 );                     dos.flush();                                         size = br.read(buf);                     if (buf[0] != (byte)CMD_OK)                          /*received CMD_OK if success*/                     {                         break;                     }                     try                     {                         Thread.sleep(1000);                     }                     catch(InterruptedException e)                     {}                 }             }    br.close();    dos.close();    socket.close();         }         catch (IOException e)         {             System.out.println(e);         }     }      public static void main ( String[] args )  {         try         {             String master_ip  = args[0];    // service ip             int master_port   = 5615;       // service port             String appid      = args[1];    // apid                         String slaver_host = GetHost(master_ip, master_port, DEV_SEND, appid.toCharArray());             System.out.println("redirect to: " + slaver_host.trim());             String slaver_ip   = slaver_host.split("_")[0];             int slaver_port    = Integer.valueOf(new String(slaver_host.split("_")[1].trim())).intValue();             SendData(slaver_ip, slaver_port, appid.toCharArray());         }         catch(NumberFormatException e)         {             System.out.println(e);         }  } }     import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import java.util.Random; import java.io.DataInputStream;  import java.io.DataOutputStream;  public class client2 {     public static char CMD_JOIN     = 0x01;     public static char CMD_DATA     = 0x02;     public static char CMD_REDIRECT = 0x03;     public static char CMD_ERROR    = 0xFE;     public static char CMD_OK       = 0xFC;         public static char DEV_SEND     = 0x01;     public static char DEV_RECV     = 0x02;         // Get Slaver Host Information     public static String GetHost(String ip, int port, char dev, char[] appid)     {         char[] host = new char[255];         host[0]     = '\0';         try         {    Socket socket        = new Socket (ip, port );             DataOutputStream dos = new DataOutputStream(socket.getOutputStream());    DataInputStream br   = new DataInputStream ( socket.getInputStream());             byte[] buf           = new byte[255];                         // apply to join appid pack             buf[0] = (byte)CMD_JOIN;           /*CMD*/             buf[1] = (byte)dev;                /*the type of device*/             for (int i = 0; i < 16; i++)             {                 buf[i + 2] = (byte)appid[i];             }                dos.write ( buf, 0, 18 );    dos.flush();                         int size = br.read(buf);             buf[size] = '\0';             if (buf[0] == (byte)CMD_REDIRECT)    /*return redirect service ip and port*/             {                  for(int i = 0; i < size; i++)                 {                     host[i] = (char)buf[i+1];                 }                }    br.close();    dos.close();    socket.close();         }         catch (IOException e)         {         }                 return new String(host);     }         // Send Data to Appid     public static void SendData(String ip, int port, char[] appid)     {         char[] host = new char[255];         host[0]     = '\0';         Random random=new Random();         try         {    Socket socket        = new Socket (ip, port );             DataOutputStream dos = new DataOutputStream(socket.getOutputStream());    DataInputStream br   = new DataInputStream ( socket.getInputStream());             byte[] buf           = new byte[255];                         // apply to join appid pack              buf[0] = (byte)CMD_JOIN;       /*CMD*/             buf[1] = (byte)DEV_RECV;       /*as receive device*/             for (int i = 0; i < 16; i++)             {                 buf[i + 2] = (byte)appid[i];             }                dos.write ( buf, 0, 18 );    dos.flush();                         int size = br.read(buf);             buf[size] = '\0';             int buf_end = 0;                         // after joining, will return CMD_OK pack             if (buf[0] == (byte)CMD_OK)             {                 while (true)                 {                     // loop reading data from service                     size = br.read(buf);                     if (size == -1)                     {                         break;                     }                     for(int i = 0; i<size; i++)                     {                         System.out.print((char)buf[i]);                     }                 }             }    br.close();    dos.close();    socket.close();         }         catch (IOException e)         {             System.out.println(e);         }     }      public static void main ( String[] args )  {         try         {             String master_ip  = args[0];    // service ip             int master_port   = 5615;       // service port             String appid      = args[1];    // apid                         String slaver_host = GetHost(master_ip, master_port, DEV_RECV, appid.toCharArray());             System.out.println("redirect to: " + slaver_host.trim());             String slaver_ip   = slaver_host.split("_")[0];             int slaver_port    = Integer.valueOf(new String(slaver_host.split("_")[1].trim())).intValue();             SendData(slaver_ip, slaver_port, appid.toCharArray());         }         catch(NumberFormatException e)         {             System.out.println(e);         }  } }  

上一篇:一般断点
下一篇:poj 1523 SPF(tarjan求割点)

相关文章

相关评论