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);
        }
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值