短信猫不能关闭服务,端口占用,javax.comm.PortInUseException: Port currently owned by org.smslib等问题

前些天在弄短信猫时出现一种情况,即只能发送一条信息,再次发送时就报端口占用错误。我用的是官方提供的代码 如下:
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."  );
      }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值