⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testclientsocket.java

📁 中国联通炫铃业务接口开发
💻 JAVA
字号:
package com.wireless.crbt.gwif.test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.StringTokenizer;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.wireless.crbt.gwif.global.CRBT;
import com.wireless.crbt.gwif.global.Util;

//import com.wireless.sms.gwif.test.MTClientSocket;
//import com.wireless.sms.pub.entity.MT;
//import com.wireless.sms.pub.util.CryptoTool;

public class TestClientSocket {
	private static final String RESPONSEMESSAGE = "RESPONSEMESSAGE";
	  private Vector mthost = new Vector();
	  private Vector mtport = new Vector();
	  private static TestClientSocket mtClientSocket = null;
	  private Logger log = null;
	  private String [] address = new String[0];
	  private String [] ports = new String[0];

	  /**
	   * 设置监控地址
	   * @param address String[]  IP 地址
	   * @param ports String[]  端口号
	   */
	  public void setMonitor(String [] address, String [] ports){
	    this.address = address;
	    this.ports = ports;
	  }

	  public static TestClientSocket getInstance() {
	    if (mtClientSocket == null) {
	      mtClientSocket = new TestClientSocket();
	    }
	    return mtClientSocket;
	  }

	  public TestClientSocket(String[] host, String[] port) {

	    for (int i = 0; i < host.length; i++) {
	      mthost.add(host[i]);
	    }

	    for (int i = 0; i < port.length; i++) {
	      mtport.add(port[i]);
	    }
	  }

	  public TestClientSocket(String host, String port) {
	    mthost.add(host);
	    mtport.add(port);
	  }

	  private TestClientSocket() {

	    String hosts = "211.94.156.167";
	    String ports = "8700,8701,8702,8703,8704,8705,8706,8707,8708,8709,8710,8711,8712,8713,8714,8715,8716,8717,8718,8719,8720";

	    StringTokenizer hostips = new StringTokenizer(hosts, ",");
	    StringTokenizer hostports = new StringTokenizer(ports, ",");
	    String tmpStr = null;

	    while (hostips.hasMoreTokens()) {
	      tmpStr = hostips.nextToken().toString();
	      mthost.add(tmpStr);
	    }

	    while (hostports.hasMoreTokens()) {
	      tmpStr = hostports.nextToken().toString();
	      mtport.add(tmpStr);
	    }
	  }

	  public void setLog(Logger log) {
	    this.log = log;
	  }
	  /** 发送字节数据 */
	  public boolean sendMT(byte[] mt) throws Exception{

//	    mt.setMsgContent( CryptoTool.base64Encode( mt.getMsgContent() ) );

	    boolean thisflag = false;
	    String host = null;
	    int port = 0;

	    for (int i = 0; i < mthost.size() && thisflag == false; i++) {
	      for (int j = 0; j < mtport.size() && thisflag == false; j++) {
	        try {

	          host = (String)this.mthost.elementAt(i);
	          port = Integer.parseInt( (String)this.mtport.elementAt(j));

	          SocketChannel sc = SocketChannel.open();
	          InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(host), port);
	          sc.connect(address);

	          ByteBuffer buffer = ByteBuffer.allocate(2048);

	          if (sc.isConnected()) {

	            try {
	              buffer.clear();
	              buffer.put(mt);
	              buffer.flip();
	              sc.write(buffer);
	              sc.close();
	              thisflag = true;
	            }
	            catch (IOException ex) {
	              sc.close();
	              thisflag = false;
	              throw ex;
	            }
	          }
	        }
	        catch (IOException ex) {
	          thisflag = false;
	          throw ex;
	        }
	      }
	    }

	    return thisflag;
	  }
	  /** 发送CRBT型数据 */
	  public boolean sendCRBT(CRBT crbt) throws Exception{

//		    mt.setMsgContent( CryptoTool.base64Encode( mt.getMsgContent() ) );

		    boolean thisflag = false;
		    String host = null;
		    int port = 0;

		    for (int i = 0; i < mthost.size() && thisflag == false; i++) {
		      for (int j = 0; j < mtport.size() && thisflag == false; j++) {
		        try {

		          host = (String)this.mthost.elementAt(i);
		          port = Integer.parseInt( (String)this.mtport.elementAt(j));

		          SocketChannel sc = SocketChannel.open();
		          InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(host), port);
		          sc.connect(address);

		          ByteBuffer buffer = ByteBuffer.allocate(2048);

		          if (sc.isConnected()) {

		            try {
		              buffer.clear();
		              String c ;
		              if(CrbtGwifSender.sendRespFlag){
		            	  crbt.setCommand_status_resp_4("1234");
		            	  crbt.setCraccount_resp_20("13426221081");
		            	  crbt.setCrid_resp_200("我们是中华人民共和国的害虫!!");
		            	  crbt.setPassword_resp_8("12345678");
		            	  crbt.setStatus_resp_2("02");
		            	  crbt.setRespTime(Util.getCurrentTime());
		            	  crbt.setTotalnum_resp_4("0006");
		            	  if(CrbtGwifSender.test==28)
		            		  CrbtGwifSender.test = 1;
		            	  crbt.setSeq_no_10(CrbtGwifSender.test+"");
		            	  CrbtGwifSender.test++;
		            	  c= RESPONSEMESSAGE+crbt.toString();
		              }else
		            	  c=crbt.toString();
		              CrbtGwifSender.txt.append("\n"+c+"\n");
		              buffer.put(c.getBytes());
		              buffer.flip();
		              sc.write(buffer);
		              sc.close();
		              thisflag = true;
		            }
		            catch (IOException ex) {
		              sc.close();
		              thisflag = false;
		              throw ex;
		            }
		          }
		        }
		        catch (IOException ex) {
		          thisflag = false;
		          throw ex;
		        }
		      }
		    }

		    return thisflag;
	  }
	  /** 发送字符串型数据 */
	  public boolean sendString(String c) throws Exception{

//		    mt.setMsgContent( CryptoTool.base64Encode( mt.getMsgContent() ) );

		    boolean thisflag = false;
		    String host = null;
		    int port = 0;

		    for (int i = 0; i < mthost.size() && thisflag == false; i++) {
		      for (int j = 0; j < mtport.size() && thisflag == false; j++) {
		        try {

		          host = (String)this.mthost.elementAt(i);
		          port = Integer.parseInt( (String)this.mtport.elementAt(j));

		          SocketChannel sc = SocketChannel.open();
		          InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(host), port);
		          sc.connect(address);

		          ByteBuffer buffer = ByteBuffer.allocate(2048);

		          if (sc.isConnected()) {

		            try {
		              buffer.clear();
		              buffer.put(c.getBytes());
		              buffer.flip();
		              sc.write(buffer);
		              sc.close();
		              thisflag = true;
		            }
		            catch (IOException ex) {
		              sc.close();
		              thisflag = false;
		              throw ex;
		            }
		          }
		        }
		        catch (IOException ex) {
		          thisflag = false;
		          throw ex;
		        }
		      }
		    }

		    return thisflag;
	  }

	  public static void main(String[] argv) throws Exception{
//		  TestClientSocket mtClientSocket = new TestClientSocket("192.168.2.75", "8700");
//	    MT mt = MT.getInstance("<?xml version=\"1.0\" encoding=\"GB2312\"?><mt><msgID></msgID><atTime></atTime><cityID></cityID><classID>http://sms.etong.net/sp/httpmo.php</classID><destTermID>13951018139</destTermID><feeCode></feeCode><feeTermID>13951018139</feeTermID><feeType></feeType><feeUserType></feeUserType><gateWayID></gateWayID><isImBalance>2</isImBalance><moID></moID><mouseID>1</mouseID><mousePackageID>1</mousePackageID><msgContent>中文字符大雨75个长度,BASE 64 编码结果</msgContent><msgContentID></msgContentID><msgFormat>15</msgFormat><msgID></msgID><msgLen>11</msgLen><mtType>2</mtType><needReply>1</needReply><provinceID></provinceID><respTime></respTime><sendTime>200410212315</sendTime><serviceCode></serviceCode><serviceID>245</serviceID><smTypeID>1138</smTypeID><srcTermID>666</srcTermID><valIdTime></valIdTime><msgLevel>1</msgLevel><mouseGroupID></mouseGroupID><status></status><linkID></linkID><spnumber>8866</spnumber><subType>0</subType></mt>");
//	    mtClientSocket.sendMT(mt);
	  }

	public synchronized String[] getAddress() {
		return address;
	}

	public synchronized void setAddress(String[] address) {
		this.address = address;
	}

	public synchronized String[] getPorts() {
		return ports;
	}

	public synchronized void setPorts(String[] ports) {
		this.ports = ports;
	}

	public synchronized Logger getLog() {
		return log;
	}
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -