package cn.net.test;

import com.google.gson.Gson;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.BindException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import cn.net.cpzslibs.prot.SocketCreate;

public class Prot10037 {

//	{
//		"userid": “jymf”, 
//		"gender": 1,
//		"age": 1,
//		"profession": “iphoneAPP开发”, 
//		"mobile": “13012345678,
//		}

	public static void main(String[] args) throws BindException, ConnectException, SocketTimeoutException, UnknownHostException, IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("consArea", "1010200");
		Gson gson = new Gson();
		String sendJson = gson.toJson(map);
		SocketCreate create = new SocketCreate("192.168.0.46", 8898, "2000000000001");
		create.createJustCreateSocket();
		byte[] sendJsons = sendJson.getBytes("UTF-8");
		
		System.out.println(Arrays.toString(sendJsons));
		
		
		int jsonlen = sendJsons.length;
		DataOutputStream dos = create.getDos();
		dos.write("@JYMF".getBytes("UTF-8"));
		dos.writeInt(0);
		dos.writeShort(0);
		dos.writeShort(10037);
		dos.writeShort(jsonlen+2);
		dos.write(sendJsons);
		dos.writeShort(CRC16Util.CRC16(sendJsons));
		
		
		DataInputStream dis = create.getDis();
//		dis.read(b)
		recHeader(dis);
		
	}
	
	protected static void recHeader(DataInputStream dis) throws IOException {
		byte[] sTag = new byte[5];
		int sTagLength = sTag.length;
		for (int i = 0; i < sTagLength; i++) {
			sTag[i] = dis.readByte();
		}
		int iRecTaskIndex = dis.readInt();
		short iRecTaskSubIndex = dis.readShort();
		short iRecTaskCode = dis.readShort();
		short iRecBodySize = dis.readShort();
		int jsonLen = iRecBodySize-2;
		byte[] retJons = new byte[jsonLen];
		dis.read(retJons);
		short checkCode = dis.readShort();
		System.out.println("checkCode--->"+checkCode+" ;"+new String(retJons,"UTF-8"));
		
	}
}
