首页 新闻 搜索 专区 学院

smslib库报【Connection reset by peer:socket write error】错误,如何处理

0
悬赏园豆:40 [已关闭问题] 关闭于 2015-07-15 09:43
using System.Windows.Forms;

namespace demo
{
    using System;
    using System.Threading;

    using org.smslib;
    using org.smslib.modem;

    public class CallNotification : ICallNotification
    {
        public void process(AGateway gateway, String callerId)
        {
            Console.WriteLine(">>> New call detected from Gateway: " + gateway.getGatewayId() + " : " + callerId);
        }
    }

    public class InboundNotification : IInboundMessageNotification
    {
        public void process(AGateway gateway, Message.MessageTypes msgType, InboundMessage msg)
        {
            if (msgType == Message.MessageTypes.INBOUND) Console.WriteLine(">>> New Inbound message detected from Gateway: " + gateway.getGatewayId());
            else if (msgType == Message.MessageTypes.STATUSREPORT) Console.WriteLine(">>> New Inbound Status Report message detected from Gateway: " + gateway.getGatewayId());
            Console.WriteLine(msg);
            try
            {
                // Uncomment following line if you wish to delete the message upon arrival.
                gateway.deleteMessage(msg);
            }
            catch (Exception e)
            {
                Console.WriteLine("Oops!!! Something gone bad...");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
    }

    public class GatewayStatusNotification : IGatewayStatusNotification
    {
        public void process(AGateway gateway, org.smslib.AGateway.GatewayStatuses oldStatus, org.smslib.AGateway.GatewayStatuses newStatus)
        {
            Console.WriteLine(">>> Gateway Status change for " + gateway.getGatewayId() + ", OLD: " + oldStatus + " -> NEW: " + newStatus);
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Create new Service object - the parent of all and the main interface to you.
            Service srv;
            srv = Service.getInstance();

            // *** The tricky part ***
            // *** Comm2IP Driver ***
            // Create (and start!) as many Comm2IP threads as the modems you are using.
            // Be careful about the mappings - use the same mapping in the Gateway definition.
            Comm2IP.Comm2IP com1 = new Comm2IP.Comm2IP(new byte[] { 127, 0, 0, 1 }, 12530, "COM3", 9600);

            try
            {
                Console.WriteLine("Example: Read messages from a serial gsm modem.");
                Console.WriteLine(Library.getLibraryDescription());
                Console.WriteLine("Version: " + Library.getLibraryVersion());

                // Start the COM listening thread.
                new Thread(new ThreadStart(com1.Run)).Start();

                // Lets set some callbacks.
                srv.setInboundMessageNotification(new InboundNotification());
                srv.setCallNotification(new CallNotification());
                srv.setGatewayStatusNotification(new GatewayStatusNotification());

                // Create the Gateway representing the serial GSM modem.
                // Due to the Comm2IP bridge, in SMSLib for .NET all modems are considered IP modems.
                IPModemGateway gateway = new IPModemGateway("modem.com4", "127.0.0.1", 12530, "wavecom", "16919");
                gateway.setIpProtocol(ModemGateway.IPProtocols.BINARY);

                // Set the modem protocol to PDU (alternative is TEXT). PDU is the default, anyway...
                gateway.setProtocol(AGateway.Protocols.PDU);

                // Do we want the Gateway to be used for Inbound messages?
                gateway.setInbound(true);

                // Do we want the Gateway to be used for Outbound messages?
                gateway.setOutbound(true);

                // Let SMSLib know which is the SIM PIN.
                gateway.setSimPin("0000");

                // Explicit SMSC address set is required for some modems.
                // Below is for VODAFONE GREECE - be sure to set your own!
                gateway.setSmscNumber("+8613800595500");

                // Add the Gateway to the Service object.
                srv.addGateway(gateway);

                // Similarly, you may define as many Gateway objects, representing
                // various GSM modems, add them in the Service object and control all of them.

                // Start! (i.e. connect to all defined Gateways)
                srv.startService();

                // Printout some general information about the modem.
                Console.WriteLine();
                Console.WriteLine("Modem Information:");
                Console.WriteLine("  Manufacturer: " + gateway.getManufacturer());
                Console.WriteLine("  Model: " + gateway.getModel());
                Console.WriteLine("  Serial No: " + gateway.getSerialNo());
                Console.WriteLine("  SIM IMSI: " + gateway.getImsi());
                Console.WriteLine("  Signal Level: " + gateway.getSignalLevel() + "dBm");
                Console.WriteLine("  Battery Level: " + gateway.getBatteryLevel() + "%");
                Console.WriteLine();

                // Send one message.
                // Remember to change the recipient!
                OutboundMessage msg = new OutboundMessage("15906027462", "Hello from SMSLib for .NET");
                srv.sendMessage(msg);
                Console.WriteLine(msg);

                // Send more than one message at once.
                //OutboundMessage[] msgArray = new OutboundMessage[2];
                //msgArray[0] = new OutboundMessage("+306948494037", "Hello from SMSLib for .NET (#1)");
                //msgArray[1] = new OutboundMessage("+306948494037", "Hello from SMSLib for .NET (#2)");
                //srv.sendMessages(msgArray);
                //Console.WriteLine(msgArray[0]);
                //Console.WriteLine(msgArray[1]);

                MessageBox.Show("Press <ENTER> to terminate...");
            }
            catch (Exception ex)
            {
                MessageBox.Show("ex.Message=" + ex.Message);
                MessageBox.Show("ex.StackTrace="+ex.StackTrace);
            }
            finally
            {
                com1.Stop();
                srv.stopService();
            }
        }
    }
}

我用的是wavecom短信猫,用smslib-3.5.4类库,第一次启动短信猫的时候(button1_Click通过这个事件),正常,可以发送短信;第二次再button1_Click的时候,报【Connection reset by peer:socket write error】这个错,一直解决不了

美国国父爱泼斯坦的主页 美国国父爱泼斯坦 | 初学一级 | 园豆:152
提问于:2014-03-05 11:15
< >
分享
所有回答(2)
0

连接被重置了,把你的 Service srv 和 Comm2IP.Comm2IP com1 作为 Form1 的成员变量,把 com1.Stop 和 srv.stopService 放到 Form1 Close 中去。

Launcher | 园豆:45045 (高人七级) | 2014-03-05 11:46

不行的,试了,我上传的代码和你讲的其实是一样的,谢了

支持(0) 反对(0) 美国国父爱泼斯坦 | 园豆:152 (初学一级) | 2014-03-05 12:05

@ddxkj: 你上传的代码确实跟我说的不一样,你要理解变量的作用范围:

using System.Windows.Forms;

namespace demo
{
    using System;
    using System.Threading;

    using org.smslib;
    using org.smslib.modem;

    public class CallNotification : ICallNotification
    {
        public void process(AGateway gateway, String callerId)
        {
            Console.WriteLine(">>> New call detected from Gateway: " + gateway.getGatewayId() + " : " + callerId);
        }
    }

    public class InboundNotification : IInboundMessageNotification
    {
        public void process(AGateway gateway, Message.MessageTypes msgType, InboundMessage msg)
        {
            if (msgType == Message.MessageTypes.INBOUND) Console.WriteLine(">>> New Inbound message detected from Gateway: " + gateway.getGatewayId());
            else if (msgType == Message.MessageTypes.STATUSREPORT) Console.WriteLine(">>> New Inbound Status Report message detected from Gateway: " + gateway.getGatewayId());
            Console.WriteLine(msg);
            try
            {
                // Uncomment following line if you wish to delete the message upon arrival.
                gateway.deleteMessage(msg);
            }
            catch (Exception e)
            {
                Console.WriteLine("Oops!!! Something gone bad...");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
    }

    public class GatewayStatusNotification : IGatewayStatusNotification
    {
        public void process(AGateway gateway, org.smslib.AGateway.GatewayStatuses oldStatus, org.smslib.AGateway.GatewayStatuses newStatus)
        {
            Console.WriteLine(">>> Gateway Status change for " + gateway.getGatewayId() + ", OLD: " + oldStatus + " -> NEW: " + newStatus);
        }
    }

    public partial class Form1 : Form
    {
        Service srv;
        Comm2IP.Comm2IP com1;        

        public Form1()
        {
            InitializeComponent();

            srv = Service.getInstance();
            com1 = new Comm2IP.Comm2IP(new byte[] { 127, 0, 0, 1 }, 12530, "COM3", 9600);
            new Thread(new ThreadStart(com1.Run)).Start();

            srv.setInboundMessageNotification(new InboundNotification());
            srv.setCallNotification(new CallNotification());
            srv.setGatewayStatusNotification(new GatewayStatusNotification());

            // Create the Gateway representing the serial GSM modem.
            // Due to the Comm2IP bridge, in SMSLib for .NET all modems are considered IP modems.
            IPModemGateway gateway = new IPModemGateway("modem.com4", "127.0.0.1", 12530, "wavecom", "16919");
            gateway.setIpProtocol(ModemGateway.IPProtocols.BINARY);

            // Set the modem protocol to PDU (alternative is TEXT). PDU is the default, anyway...
            gateway.setProtocol(AGateway.Protocols.PDU);

            // Do we want the Gateway to be used for Inbound messages?
            gateway.setInbound(true);

            // Do we want the Gateway to be used for Outbound messages?
            gateway.setOutbound(true);

            // Let SMSLib know which is the SIM PIN.
            gateway.setSimPin("0000");

            // Explicit SMSC address set is required for some modems.
            // Below is for VODAFONE GREECE - be sure to set your own!
            gateway.setSmscNumber("+8613800595500");

            // Add the Gateway to the Service object.
            srv.addGateway(gateway);

            srv.startService();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Create new Service object - the parent of all and the main interface to you.
            
            

            // *** The tricky part ***
            // *** Comm2IP Driver ***
            // Create (and start!) as many Comm2IP threads as the modems you are using.
            // Be careful about the mappings - use the same mapping in the Gateway definition.          

            try
            {
                Console.WriteLine("Example: Read messages from a serial gsm modem.");
                Console.WriteLine(Library.getLibraryDescription());
                Console.WriteLine("Version: " + Library.getLibraryVersion());

                // Start the COM listening thread.
                

                // Lets set some callbacks.
                
                // Similarly, you may define as many Gateway objects, representing
                // various GSM modems, add them in the Service object and control all of them.

                // Start! (i.e. connect to all defined Gateways)
                

                // Printout some general information about the modem.
                Console.WriteLine();
                Console.WriteLine("Modem Information:");
                Console.WriteLine("  Manufacturer: " + gateway.getManufacturer());
                Console.WriteLine("  Model: " + gateway.getModel());
                Console.WriteLine("  Serial No: " + gateway.getSerialNo());
                Console.WriteLine("  SIM IMSI: " + gateway.getImsi());
                Console.WriteLine("  Signal Level: " + gateway.getSignalLevel() + "dBm");
                Console.WriteLine("  Battery Level: " + gateway.getBatteryLevel() + "%");
                Console.WriteLine();

                // Send one message.
                // Remember to change the recipient!
                OutboundMessage msg = new OutboundMessage("15906027462", "Hello from SMSLib for .NET");
                srv.sendMessage(msg);
                Console.WriteLine(msg);

                // Send more than one message at once.
                //OutboundMessage[] msgArray = new OutboundMessage[2];
                //msgArray[0] = new OutboundMessage("+306948494037", "Hello from SMSLib for .NET (#1)");
                //msgArray[1] = new OutboundMessage("+306948494037", "Hello from SMSLib for .NET (#2)");
                //srv.sendMessages(msgArray);
                //Console.WriteLine(msgArray[0]);
                //Console.WriteLine(msgArray[1]);

                MessageBox.Show("Press <ENTER> to terminate...");
            }
            catch (Exception ex)
            {
                MessageBox.Show("ex.Message=" + ex.Message);
                MessageBox.Show("ex.StackTrace=" + ex.StackTrace);
            }
        }
    }
}

而且,我希望这里的 Form1 是你 Application.Run 中使用的。

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2014-03-05 13:11
0

LZ。你的问题解决了吗?

_weibo | 园豆:100 (初学一级) | 2014-09-29 11:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册