📄 clientsocket.java
字号:
package com.wireless.sms.sgip.net;
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;
public class ClientSocket {
private Vector vHost = new Vector();
private Vector vPort = new Vector();
private static ClientSocket clientSocket = 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 ClientSocket getInstance() {
if (clientSocket == null) {
clientSocket = new ClientSocket();
}
return clientSocket;
}
public ClientSocket(String[] host, String[] port) {
for (int i = 0; i < host.length; i++) {
vHost.add(host[i]);
}
for (int i = 0; i < port.length; i++) {
vPort.add(port[i]);
}
}
public ClientSocket(String host, String port) {
vHost.add(host);
vPort.add(port);
}
private ClientSocket() {
String hosts = "211.94.156.177";
String ports = "8700,8701,8702,8703,8704,8705,8706";
StringTokenizer hostips = new StringTokenizer(hosts, ",");
StringTokenizer hostports = new StringTokenizer(ports, ",");
String tmpStr = null;
while (hostips.hasMoreTokens()) {
tmpStr = hostips.nextToken().toString();
vHost.add(tmpStr);
}
while (hostports.hasMoreTokens()) {
tmpStr = hostports.nextToken().toString();
vPort.add(tmpStr);
}
}
public void setLog(Logger log) {
this.log = log;
}
public boolean sendSimulateMO(String sendStr) throws Exception{
boolean thisflag = false;
String host = null;
int port = 0;
for (int i = 0; i < vHost.size() && thisflag == false; i++) {
for (int j = 0; j < vPort.size() && thisflag == false; j++) {
try {
host = (String)this.vHost.elementAt(i);
port = Integer.parseInt( (String)this.vPort.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(sendStr.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 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 + -