关于socket的问题,求大神解释一下以上代码,本人小白不太懂

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Timer;
/*import com.lightingcontroller.Zigbee.ZigbeeScene;*/

import android.os.Handler;
import android.util.Log;

public class ZigbeeSrpcClient {

static Thread thread;
static Socket kkSocket;
static OutputStream outStream;
static InputStream inStream;

private static String gatewayIp= "192.168.1.111";//这个是什么?改了有问题吗?
static public String gatewayPort= "11235"; //"2be3";这个端口号我没有在服务器端发现

//SRPC头位的位置
private static final int SRPC_CMD_ID_POS = 0;
private static final int SRPC_CMD_LEN_POS = 1;

//SRPC CMD的ID
//定义外向RPSC命令ID的
private static final byte SRPC_NEW_DEVICE     = (byte) 0x0001;
private static final byte SRPC_RESERVED_1         = (byte) 0x0002;
private static final byte SRPC_RESERVED_2         = (byte) 0x0003;
private static final byte SRPC_RESERVED_3         = (byte) 0x0004;
private static final byte SRPC_RESERVED_4         = (byte) 0x0005;
private static final byte SRPC_RESERVED_5         = (byte) 0x0006;
private static final byte SRPC_GET_DEV_STATE_RSP  = (byte) 0x0007;
private static final byte SRPC_GET_DEV_LEVEL_RSP  = (byte) 0x0008;
private static final byte SRPC_GET_DEV_HUE_RSP    = (byte) 0x0009;
private static final byte SRPC_GET_DEV_SAT_RSP    = (byte) 0x000a;
private static final byte SRPC_ADD_GROUP_RSP      = (byte) 0x000b;
private static final byte SRPC_GET_GROUP_RSP      = (byte) 0x000c;
private static final byte SRPC_ADD_SCENE_RSP      = (byte) 0x000d;
private static final byte SRPC_GET_SCENE_RSP      = (byte) 0x000e;


//定义接收SRPC命令ID的
private static final byte SRPC_CLOSE              = (byte) 0x80;
private static final byte SRPC_GET_DEVICES        = (byte) 0x81;
private static final byte SRPC_SET_DEV_STATE      = (byte) 0x82;    
private static final byte SRPC_SET_DEV_LEVEL      = (byte) 0x83;    
private static final byte SRPC_SET_DEV_COLOR      = (byte) 0x84;
private static final byte SRPC_GET_DEV_STATE      = (byte) 0x85;    
private static final byte SRPC_GET_DEV_LEVEL      = (byte) 0x86;    
private static final byte SRPC_GET_DEV_HUE        = (byte) 0x87;
private static final byte SRPC_GET_DEV_SAT        = (byte) 0x88;
private static final byte SRPC_BIND_DEVICES       = (byte) 0x89;
private static final byte SRPC_RESERVED_6         = (byte) 0x8a;
private static final byte SRPC_RESERVED_7         = (byte) 0x8b;
private static final byte SRPC_RESERVED_8         = (byte) 0x8c;
private static final byte SRPC_RESERVED_9         = (byte) 0x8d;
private static final byte SRPC_GET_GROUPS         = (byte) 0x8e;    
private static final byte SRPC_ADD_GROUP          = (byte) 0x8f;    
private static final byte SRPC_GET_SCENES         = (byte) 0x90;    
private static final byte SRPC_STORE_SCENE        = (byte) 0x91;    
private static final byte SRPC_RECALL_SCENE       = (byte) 0x92;    
private static final byte SRPC_IDENTIFY_DEVICE    = (byte) 0x93;    
private static final byte SRPC_CHANGE_DEVICE_NAME = (byte) 0x94;  
private static final byte SRPC_REMOVE_DEVICE      = (byte) 0x95; 
private static final byte SRPC_OPEN_NETWORK       = (byte) 0x9c; 

private static final short JOIN_AUTH_NUM          = (short) 0xebd4;

//afaddr SRPC地址编号方式
public static final byte AddrNotPresent = 0;
public static final byte AddrGroup = 1;
public static final byte Addr16Bit = 2;
public static final byte Addr64Bit = 3;
public static final byte AddrBroadcast = 1;

public static final String PREFS_NAME = "MyPrefsFile";
private static Timer ResponseTimer;
private static Handler ResponseTimerHandler;

static byte[] srpcResponse;     

private static int rpcsProcessIncoming(byte[] msg, int msgPtr)
{   
    int msgLen;     

    switch (msg[msgPtr + SRPC_CMD_ID_POS])
    {

    case SRPC_NEW_DEVICE:
    {
        int profileId=0, deviceId=0, nwkAddr=0; 
        char endPoint;
        String deviceName = "";
        byte[] ieee = new byte[8];
        byte newDevFlag;

        msgLen = msg[msgPtr + SRPC_CMD_LEN_POS] + 2;
        //index passed len and cmd ID
        msgPtr+=2;

        //得到网络地址
        for (int i=0; i < 2; i++, msgPtr++) 
        {   
            //java不支持无符号的,所以使用更大的容器来避免转换问题
            int nwkAddrTemp = (msg[msgPtr] & 0xff);              
            nwkAddr += (nwkAddrTemp << (8 * i));                
        }           

        //得到 EndPoint
        endPoint = (char) msg[msgPtr++];

        //得到一个ProfileID
        for (int i=0; i < 2; i++, msgPtr++) 
        {   
            //java不支持无符号的,所以使用更大的容器来避免转换问题
            int profileIdTemp = (msg[msgPtr] & 0xff);                
            profileId += (profileIdTemp << (8 * i));                
        }   

        //得到 DeviceId
        for (int i=0; i < 2; i++, msgPtr++) 
        {   
            //java不支持无符号的,所以使用更大的容器来避免转换问题
            int deviceIdTemp = (msg[msgPtr] & 0xff);                 
            deviceId += (deviceIdTemp << (8 * i));              
        }               

        //指数通过的版本
        msgPtr++;

        int nameSize = msg[msgPtr++];
        for(int i = 0; i < nameSize; i++)
        {
            deviceName += (char) msg[msgPtr++];
        }           

        //指数传递状态
        msgPtr++;

        //复制 IEEE Addr
        for(int i = 0; i < 8; i++)
        {
            ieee[i] = msg[msgPtr++];
        }           

        newDevFlag = msg[msgPtr++];

        ZigbeeAssistant.newDevice(profileId, deviceId, nwkAddr, endPoint, ieee, deviceName, newDevFlag);

        break;
    }

求大神解释一下以上代码,本人小白不太懂

1个回答

你这段是客户端的代码,gatewayIp这个是指定的服务器端地址,是部署了你的Server端代码的机器的IP,可以改但是必须也部署了服务器端代码。
gatewayPort这个端口是服务器端的监听端口,服务器端肯定是ServerSocket的监听端口。
定义的一系列常量都是用来通信的,服务器端根据这些类型来处理相应的请求的。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问