Exemplo n.º 1
0
        public static Socket GetSocket(ComunicationType LeiX, string A1, string A2)
        {
            Socket result = null;

            if (LeiX == ComunicationType.TCPMyisServer)
            {
                checked
                {
                    if (Program.tcplistener != null)
                    {
                        for (int i = 0; i < Program.tcplistener.clients.Count; i++)
                        {
                            if (A1 == Program.tcplistener.clients[i].A1 & A2 == Program.tcplistener.clients[i].A2 & Program.tcplistener.clients[i].Connected)
                            {
                                result = Program.tcplistener.clients[i].Sock;
                            }
                        }
                    }
                }
            }
            else
            {
                if (LeiX == ComunicationType.TCPMyisClient)
                {
                    bool flag = false;
                    try
                    {
                        if (Program.Connecter_Socket.Connected)
                        {
                            flag = true;
                        }
                    }
                    catch
                    {
                    }
                    if (!flag)
                    {
                        Program.Connecter_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        byte[] optionInValue = new byte[]
                        {
                            1,
                            0,
                            0,
                            0,
                            48,
                            117,
                            0,
                            0,
                            208,
                            7,
                            0,
                            0
                        };
                        Program.Connecter_Socket.IOControl(IOControlCode.KeepAliveValues, optionInValue, null);
                    }
                    result = Program.Connecter_Socket;
                }
            }
            return(result);
        }
Exemplo n.º 2
0
 private void 断开连接ToolStripMenuItemClick(object sender, EventArgs e)
 {
     try
     {
         int              index   = this.GridV.CurrentRow.Index;
         string           leiX    = Convert.ToString(this.GridV.Rows[index].Cells[4].Value);
         string           a       = Convert.ToString(this.GridV.Rows[index].Cells[2].Value);
         string           a2      = Convert.ToString(this.GridV.Rows[index].Cells[3].Value);
         ComunicationType leiXing = Program.GetLeiXing(leiX);
         if (leiXing != ComunicationType.Serial)
         {
             Socket socket = Program.GetSocket(leiXing, a, a2);
             socket.Close();
         }
     }
     catch
     {
     }
 }