public void InitSendClientPassive()
        {
            bool SendClosed = false;

            while (true)
            {
                if (ConnectionClosed != null && SendClosed)
                {
                    SendClosed = false;
                    context.Post(delegate { ConnectionClosed(1); }, null);
                }



                if (Send_Client == null || Send_Client.Connected == false)
                {
                    if (Send_Client != null)
                    {
                        Send_Client.Close();
                    }

                    TcpListener server = new TcpListener(local_ip, send_connection_port);
                    server.Start();

                    Send_Client = null;
                    if (onlyOneConnection)
                    {
                        Recieve_Client = null;
                    }
                    Send_Client = server.AcceptTcpClient();

                    SocketExtensions.SetKeepAlive(Send_Client.Client, 100, 20);

                    SendClosed = true;

                    if (ConnectionEstablished != null)
                    {
                        context.Post(delegate { ConnectionEstablished(1); }, null);
                    }

                    //Thread for Recieving Data
                    if (ReceiveDataSendClientThread != null)
                    {
                        ReceiveDataSendClientThread.Abort();
                        Threads.Remove(ReceiveDataSendClientThread);
                    }
                    ReceiveDataSendClientThread      = new Thread(ReceiveDataSendClient);
                    ReceiveDataSendClientThread.Name = "PReceiveDataSendClientThread";
                    ReceiveDataSendClientThread.Start();
                    Threads.Add(ReceiveDataSendClientThread);
                    //End Thread

                    if (onlyOneConnection)
                    {
                        Recieve_Client = Send_Client;
                    }
                }
            }
        }
Example #2
0
        public void InitSendClientPassive()
        {
            TcpListener server       = null;
            TcpClient   oldTcpClient = null;
            bool        SendClosed   = false;

            while (true)
            {
                if (ConnectionClosed != null && SendClosed && (_tcpClient == null || _tcpClient.Connected == false))
                {
                    oldTcpClient = _tcpClient;
                    SendClosed   = false;
                    context.Post(delegate { ConnectionClosed(oldTcpClient); }, null);
                }

                if (!SendClosed && (_tcpClient == null || _tcpClient.Connected == false))
                {
                    if (_tcpClient != null)
                    {
                        _tcpClient.Close();
                    }

                    if (server == null)
                    {
                        server = new TcpListener(/*local_ip,*/ _port);
                        server.Start();
                    }

                    _tcpClient = null;
                    _tcpClient = server.AcceptTcpClient();
                    SocketExtensions.SetKeepAlive(_tcpClient.Client, 100, 20);
                    SendClosed = true;

                    if (ConnectionEstablished != null)
                    {
                        context.Post(delegate { ConnectionEstablished(_tcpClient); }, null);
                    }

                    //Thread for Recieving Data
                    if (_receiveDataThread != null)
                    {
                        _receiveDataThread.Abort();
                        Threads.Remove(_receiveDataThread);
                    }
                    _receiveDataThread      = new Thread(ReceiveDataSendClient);
                    _receiveDataThread.Name = "PReceiveDataSendClientThread";
                    _receiveDataThread.Start();
                    Threads.Add(_receiveDataThread);
                    //End Thread
                }
                else
                {
                    Thread.Sleep(100);
                }
            }
        }
        public void InitRecieveClientActive()
        {
            bool RecieveClosed = false;

            while (true)
            {
                try
                {
                    if (Recieve_Client == null || Recieve_Client.Connected == false)
                    {
                        if (RecieveClosed)
                        {
                            RecieveClosed = false;
                            context.Post(delegate { ConnectionClosed(2); }, null);
                        }

                        Recieve_Client = null;
                        Recieve_Client = new TcpClient(plc_ip.ToString(), recieve_connection_port);
                        SocketExtensions.SetKeepAlive(Recieve_Client.Client, 100, 20);

                        RecieveClosed = true;

                        if (ConnectionEstablished != null)
                        {
                            context.Post(delegate { ConnectionEstablished(2); }, null);
                        }

                        //Thread for Recieving Data
                        if (ReceiveDataRecieveClientThread != null)
                        {
                            ReceiveDataRecieveClientThread.Abort();
                            Threads.Remove(ReceiveDataRecieveClientThread);
                        }
                        ReceiveDataRecieveClientThread      = new Thread(ReceiveDataRecieveClient);
                        ReceiveDataRecieveClientThread.Name = "AReceiveDataRecieveClient";
                        ReceiveDataRecieveClientThread.Start();
                        Threads.Add(ReceiveDataRecieveClientThread);
                        //End Thread
                    }
                    Thread.Sleep(1000);
                }
                catch (ThreadAbortException ex)
                {
                    return;
                }
                catch (Exception ex)
                {
                    Thread.Sleep(5000);
                }
            }
        }