[c#]Variavel sempre com valor nulo

wrproject

Power Member
Ttenho o seguinte codigo do servidor que é constituido por 2 ficheiros:

[Window1.xaml.cs]

Código:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.Collections;
using System.IO;
using System.Threading;
namespace Server
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {


        //Server socket
        TcpListener server;

        //IP adress
        System.Net.IPAddress localAdd;

        //Network Stream
        NetworkStream ns;

        //thread para receber clientes
        Thread tt;

        //classe que vai servir de thread para listener
        Listener _ThListner;

        public Window1()
        {
            InitializeComponent();
            
            localAdd = System.Net.IPAddress.Parse("127.0.0.1");

            _ThListner = new Listener(server);
            server = new TcpListener(localAdd, Int32.Parse(porta.Text));
        }

        public void OnConnectClick(object sender, EventArgs args)
        {

            if (Btconnect.Content.Equals("Ligar Servidor"))
            {
                //lanca o listener numa nova thread
                tt = new Thread(new ThreadStart(_ThListner.ReceiveClient));
                tt.Start();
                Btconnect.Content = "Desligar Servidor";
            }
            else
            {
                _ThListner.StopListening();
                tt.Abort();
                Btconnect.Content = "Ligar Servidor";
            }


        }
    }
}

[Listener.cs]


Código:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net.Sockets;

namespace Server
{
    class Listener
    {
        TcpListener server;
        TcpClient _cliente;
        bool serverruning = false;
        public Listener(TcpListener s)
        {
            server = s;
            
        }

        public void ReceiveClient()
        {


            
            
            //ciclo infinito que vai receber clientes a media que vao conectando
            serverruning = true;
            while(serverruning)
            {
                _cliente = server.AcceptTcpClient();
                System.Windows.MessageBox.Show("Um cliente conectou");
            }
        }

        public void StopListening()
        {
            serverruning = false ;
        }

    }
}
e o cliente é constituito pelo seguinte código abaixo:

[Cliente.xaml.cs]

Código:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.Collections;
namespace Cliente
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    /// 
    

    public partial class Window1 : Window
    {

        TcpClient _cliente;

        System.Net.IPAddress IP;

        public Window1()
        {
            InitializeComponent();
        }


        public void OnLigar(object sender, EventArgs args)
        {
            
            _cliente = new TcpClient();

            try
            {
                if (button1.Content.Equals("Ligar"))
                {
                    IP = System.Net.IPAddress.Parse(TxtIp.Text);
                    _cliente.Connect(IP, Int32.Parse(TxtPorta.Text));
                    button1.Content = "Desligar";
                }
                else
                {
                    _cliente.Close();
                    button1.Content = "Ligar";
                }
            }
            catch(Exception)
            {
                System.Windows.MessageBox.Show("Impossivel ligar ao servidor remoto","Erro");
            }
            
        }

        public void OnSair(object sender, EventArgs args)
        {
            this.Close();
            
        }



    }
}
No evento "OnConnectClick" eu passo o argumento do socket listener para o constructor Listener para depois ser chamado numa nova thread,o problema é que depois de ser passado,as vaiaveis "server" e "_cliente" que se encontram dentro da classe listener aparecem
sempre como nulas.
Como faço para as variaveis ficarem com o valor do parametro e nao com um valor nulo?

Cumps.
 
Back
Topo