Exemple #1
0
        public override string Open()
        {
            /*string addr = Common.Conf.IP1 + "." +
             *      Common.Conf.IP2 + "." +
             *      Common.Conf.IP3 + "." +
             *      Common.Conf.IP4;*/
            int port = Common.Conf.Port;

            if (Socket != null)
            {
                return(null);
            }
            Socket s = null;

            Socket = null;
            try
            {
                //IPHostEntry ent = Dns.GetHostEntry(addr);
                byte[]    bt          = { Common.Conf.IP1, Common.Conf.IP2, Common.Conf.IP3, Common.Conf.IP4 };
                IPAddress ipAddress   = new IPAddress(bt);// ent.AddressList[0];
                EndPoint  destination = new IPEndPoint(ipAddress, port);
                s = new Socket(destination.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
                s.Bind(new IPEndPoint(IPAddress.Any, port));
                s.Connect(destination);
                s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 500000);
                s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 500000);
                Destination = destination;//*/

                IpPacket p = IpPacket.GetCommand_PowerOnReset();
                Send(p, 1000, s);

                SpectroWizard.gui.MainForm.SetupConnectionIndicator(true);
            }
            catch (Exception e)
            {
                if (s != null)
                {
                    try
                    {
                        SpectroWizard.gui.MainForm.SetupConnectionIndicator(false);
                        s.Shutdown(SocketShutdown.Both);
                    }
                    finally
                    {
                        try
                        {
                            s.Close();
                        }
                        finally
                        {
                        }
                    }
                }
                Common.LogNoMsg(e);
                return(e.Message);
            }
            Socket = s;
            return(null);
        }