www.pudn.com > cmppNeu.rar > Tester.java


package cmpp.v2_0; 
 
import java.net.*; 
import java.io.*; 
import java.util.*; 
import java.text.*; 
 
import cmpp.*; 
//import com.neusoft.utils.*; 
 
public class Tester { 
	 
		public static void main(String[] args) 
	      	throws FileNotFoundException,IOException,Exception,ClassNotFoundException 
	      	{ 
			 
				String result=Tester.send("hello","12233333333","",""); 
	      	} 
 
	      	public static String send(String messageContent, String PhoneNumber,String desiredDeliveryTime, String deliveryExpirationTime) 
	      	throws FileNotFoundException,IOException,Exception,ClassNotFoundException 
	      	{ 
	      		return send(messageContent.getBytes(), PhoneNumber,"",""); 
	      	}      	      	 
	      	 
	      	public static String send(byte[] data, String PhoneNumber,String desiredDeliveryTime, String deliveryExpirationTime) 
	      	throws FileNotFoundException,IOException,Exception,ClassNotFoundException 
	      	{ 
	      		//System.out.println("next 2 continue ****	*****"); 
	      		 
	      		Socket so=null; 
	         	OutputStream out=null; 
	         	InputStream input=null; 
	         	Connect connect=null; 
	         	ConnectResp connectResp=null; 
 
	         	CMPP cmpp= new CMPP(); 
	         	String result="over"; 
			try{ 
	           		connect=new Connect(); 
	           		connect.setSourceAddr("99999"); 
	           		connect.setSharedSecret(""); 
	           		 
           		int err; 
 
				System.out.println("begin socket connect---------------"); 
	           	so=new Socket("127.0.0.1", 7890); 
	           	System.out.println("ok socket connect -----------------"); 
        	   	out=new DataOutputStream(so.getOutputStream()); 
           		input = new DataInputStream(so.getInputStream()); 
 
           		err=connect.write(out);//发送bind 
           		 
           		System.out.println(connect.toString()); 
           		System.out.println("send connect OK"); ///----------------------2 
 
           		if(err!=0)   		{ 
	           			result="BIND命令执行失败"; 
	               			return result; 
               			 
           		} 
	          	 
	          		System.out.println("recv bind_resp  111"); 
	          		CMPP tmp=cmpp.read(input);//接收sgip消息 
	          		 
	          		System.out.println("recv bind_resp   222"); 
	          		if(tmp.getCommandID()==CMPP.ID_CMPP_CONNECT_RESP)          		 
	          		{ 
	          			connectResp=(ConnectResp)tmp;//强制转换 
	               		System.out.println(connectResp.toString()); 
	           		} 
 
	          		System.out.println("ok bind_resp"); 
	          		 
	          		Submit submit = new Submit(); 
	          		submit.setServiceId("WAP"); 
	          		submit.setSrcId("9990011"); 
	          		submit.setMsgSrc("99999"); 
	          		submit.setFeeType("01"); 
	          		submit.setMsgText("你好", 15); 
	          		submit.addDestTerminalId(PhoneNumber); 
	          		submit.addDestTerminalId("1111123123"); 
	          	          		 
	          		submit.write(out);//发送submit 
	          		System.out.println("******Send the SMS:");//---------------------6 
 
					System.out.println(" recv submit resp"); 
					 
					tmp=cmpp.read(input); 
					 
		            System.out.println("submit resp id:  "+ tmp.getCommandID());	 
	          		if(tmp.getCommandID()==CMPP.ID_CMPP_SUBMIT_RESP) { 
	              			SubmitResp submitresp=(SubmitResp)tmp;//强制转换 
	              			System.out.println(submitresp.toString()); 
		           	} 
 
		              	//System.out.println("ok submit resp"); 
	          		 
	          		ActiveTest activeTest = new ActiveTest(); 
	          		activeTest.write(out); 
	          		System.out.println(activeTest.toString()); 
	          		tmp = cmpp.read(input); 
	          		if(tmp.getCommandID()==CMPP.ID_CMPP_ACTIVE_TEST_RESP) { 
	          			ActiveTestResp resp = (ActiveTestResp)tmp; 
	          			System.out.println(resp.toString()); 
	          		} 
	          		 
	          		Deliver deliver = new Deliver(); 
	          		deliver.setDestId("123123"); 
	          		deliver.setMsgText("你好",15); 
	          		deliver.setRegisteredDelivery(1); 
	          		deliver.setSrcTerminalId("13330113123"); 
 
	          		deliver.write(out); 
	          		System.out.println(deliver.toString()); 
	          		tmp = cmpp.read(input); 
	          		if(tmp.getCommandID()==CMPP.ID_CMPP_DELIVER_RESP) { 
	          			DeliverResp resp = (DeliverResp)tmp; 
	          			System.out.println(resp.toString()); 
	          		} 
	          		 
 
	          		Terminate unbind=new Terminate(); 
	        	      	System.out.println("send unbind"); 
	              		unbind.write(out);//发送unbind 
	              		System.out.println("receive unbind resp"); 
	              		tmp=cmpp.read(input); 
	              		 
	              	if(tmp.getCommandID()==CMPP.ID_CMPP_TERMINATE_RESP) 
	              		{ 
	              		TerminateResp Unresp=(TerminateResp)tmp; 
 
	              		} 
	              		 
	             		out.close(); 
	             		so.close(); 
	            		 
	         	} catch (CMPP_Exception e){ 
	              		System.out.println(e.toString()); 
	           	} 
	         	catch (Exception e) { 
	            		System.out.println(e.toString()); 
	         	}  
			 
			System.out.println("result="+result); 
			         	 
	         	return result; 
	       } 
 
}