看看是否下面这个问题导致只能发送1条,第2条就提示端口被占用
前些天在弄短信收发设备时出现一种情况,即只能发送一条信息,再次发送时就报端口占用错误。我用的是官方提供的代码 如下:
public void doIt() throws Exception
{
Service srv;
OutboundMessage msg;
OutboundNotification outboundNotification = new OutboundNotification();
System.out.println("Example: Send message from a serial gsm modem.");
System.out.println(Library.getLibraryDescription());
System.out.println("Version: " + Library.getLibraryVersion());
srv = new Service();
SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM1", 115200, "wavecom", "17254");//115200是波特率,一般为9600。可以通过超级终端测试出来
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setOutboundNotification(outboundNotification);
srv.addGateway(gateway);
srv.startService();
System.out.println("Modem Information:");
System.out.println(" Manufacturer: " + gateway.getManufacturer());
System.out.println(" Model: " + gateway.getModel());
System.out.println(" Serial No: " + gateway.getSerialNo());
System.out.println(" SIM IMSI: " + gateway.getImsi());
System.out.println(" Signal Level: " + gateway.getSignalLevel() + "%");
System.out.println(" Battery Level: " + gateway.getBatteryLevel() + "%");
System.out.println();
// Send a message synchronously.
msg = new OutboundMessage("13152195134", "这个是用java发的中文短信!");
msg.setEncoding(MessageEncodings.ENCUCS2);//这句话是发中文短信必须的
srv.sendMessage(msg);
System.out.println(msg);
System.out.println("Now Sleeping - Hit <enter> to terminate.");
System.in.read();
srv.stopService();
}
我的程序中把这个方法放到servlet中,每次点击页面按钮时调用这个方法。但第二次点击时就报端口被占用的错误。开始认为是端口没有关闭,但后面加上removeGateway(gateway)或gateway.stopGateway()都不行。
最后一想:短信收发设备接上电脑即建立了连接,执行srv.startService()相当于保持一个长连接,不能用程序断开!最后把srv设为静态属性,每次点击按钮只调用 srv.sendMessage(msg)方法,就实现连续发送短信了。
修改后代码如下:
public static Service srv;
static {
try {
init();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void init() throws Exception{
OutboundNotification outboundNotification = new OutboundNotification();
System. out .println("Example: Send message from a serial gsm modem.");
System. out .println(System.getProperty( "java.version" ));
System. out .println(Library.getLibraryDescription());
System. out .println("Version: " + Library.getLibraryVersion());
srv = new Service();
SerialModemGateway gateway = new SerialModemGateway("modem.com4" ,
"COM3" , 9600, "wavecom" , "17254" );// 115200是波特率,一般为9600。可以通过超级终端测试出来
gateway.setInbound( true );
gateway.setOutbound( true );
gateway.setOutboundNotification(outboundNotification);
srv .addGateway(gateway);
srv .startService();
System. out .println("Modem Information:" );
System. out .println(" Manufacturer: " + gateway.getManufacturer());
System. out .println(" Model: " + gateway.getModel());
System. out .println(" Serial No: " + gateway.getSerialNo());
System. out .println(" SIM IMSI: " + gateway.getImsi());
System. out .println(" Signal Level: " + gateway.getSignalLevel() + "%" );
System. out .println(" Battery Level: " + gateway.getBatteryLevel()
+ "%" );
System. out .println();
}
public void doIt(String PhoneNum, String Mesg) throws Exception {
OutboundMessage msg;
msg = new OutboundMessage(PhoneNum, Mesg);
msg.setEncoding(MessageEncodings. ENCUCS2 );// 这句话是发中文短信必须的
srv .sendMessage(msg);
System. out .println(msg);
System. out .println("send message success!stopService." );
} |