📄 sendtest1.java
字号:
package com.hotct.mytest;
import com.hoten.cmpp.message.*;
import java.net.*;
import java.text.*;
import java.io.*;
public class Sendtest1
{
final String SMG_Url = "192.168.0.79";
final int SMG_Port = 7890;
final String SPNumber = "02939";
public static void main(String[] args)
{
Sendtest1 atestprocesse = new Sendtest1();
}
public Sendtest1()
{
Socket so = null;
DataOutputStream out = null;
DataInputStream input = null;
try
{
//1、建立和SMG的socket连接
so = new Socket(SMG_Url, SMG_Port);
out = new DataOutputStream(so.getOutputStream());
input = new DataInputStream(so.getInputStream());
System.out.println("1、建立和SMG的socket连接");
//2、向SMG发送connect命令
CMPPConnectMessage connmsg = new CMPPConnectMessage();
//定义消息头
connmsg.setSequenceID(1);
connmsg.nMsgSize=39;
connmsg.nCommandID=1;
/*
* 定义消息体
*/
//AuthenticatorSource的创建
String time=CTime.getTime(CTime.YYMMDDhhmm);
System.out.println(time);
MD5 md5 = new MD5();
byte[] user="901234".getBytes();
byte[] auth1 = "1234".getBytes();
byte[] timeStamp = time.getBytes();
byte abyte2[] = new byte[100];
System.arraycopy(user, 0, abyte2, 0, user.length);
int k = user.length+ 9;
System.arraycopy(auth1, 0, abyte2, k, auth1.length);
k += auth1.length;
System.arraycopy(timeStamp, 0, abyte2, k, 10);
k += 10;
byte auths[] = new byte[k];
System.arraycopy(abyte2, 0,auths, 0, k);
byte auth[]=md5.getMD5ofBytes(auth1,auth1.length);
connmsg.sSP_ID ="901234"; //SPN企业代码
connmsg.nVersion=1; //版本号
connmsg.nTimestamp=20070101; //Timestamp
//将消息体中的元素转成byte类型,将其添加到一个byte数组中
int length;
byte[] inMsg = new byte[100];
System.arraycopy(Tools.int2byte(connmsg.nCommandID),0,inMsg,4,4);
length=12;
System.arraycopy(connmsg.sSP_ID.getBytes(),0,inMsg,length,connmsg.sSP_ID.getBytes().length);
length+=connmsg.sSP_ID.getBytes().length;
System.arraycopy(auth,0,inMsg,length,auth.length);
length+=auth.length;
inMsg[length]=(byte)connmsg.nVersion;
length++;
System.arraycopy(Tools.int2byte(Integer.parseInt(time)),0,inMsg,length,4);
length+=4;
connmsg.nMsgSize=length;
System.arraycopy(Tools.int2byte(connmsg.nMsgSize),0,inMsg,0,4);
byte[] send = new byte[length];
System.arraycopy(inMsg,0,send,0,length);
//将转成byte类型的connect消息发送出去
out = new DataOutputStream(so.getOutputStream());
out.write(send);
out.flush();
System.out.println("2、向SMG发送bind命令");
//3、接收Conn_Resp
CMPPHead head = new CMPPHead();
head.nMsgSize=input.readInt();
head.nCommandID=input.readInt();
head.nSequenceID=input.readInt();
byte[] resp = null;
if(head.nMsgSize<=12){
System.out.println("连接出错");
}else{
resp=new byte[head.nMsgSize-12];
input.read(resp,0,head.nMsgSize-12);
}
if(head.stat == 0)
System.out.println("连接成功"+head.stat); //head.stat为0 表示正确
else
System.out.println("连接出错"+head.stat);
//4、向SMG发送Submit命令
//5、接收Submit_Response
//6、向SMG发送Unbind
//7、接收Unbind_Response
//8、关闭Socket连接
out.close();
so.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -