-1

I am trying to connect TcpListener to localhost for ip address 127.0.0.1 and port number 8081 but I'm getting an error

NullReferenceException was unhandled
Object reference not set to an instance of an object..

And here is my code ...

public class Listener
{
    private TcpListener tcpListener;
    private Thread listenThread;

    Int32 port = 8081;
    IPAddress localAddr = IPAddress.Parse("127.0.0.1");
    Byte[] bytes = new Byte[256];

    public void ListenForClients()
    {
        //getting error at this line..
        this.tcpListener.Start();

        while (true)
        {
            //blocks until a client has connected to the server
            TcpClient client = this.tcpListener.AcceptTcpClient();

            //create a thread to handle communication 
            //with connected client
            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
            clientThread.Start(client);
        }
    }

    public void HandleClientComm(object client)
    {
        TcpClient tcpClient = (TcpClient)client;
        NetworkStream clientStream = tcpClient.GetStream();

        byte[] message = new byte[4096];
        int bytesRead;

        while (true)
        {
            bytesRead = 0;

            try
            {
                //blocks until a client sends a message
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch
            {
                //a socket error has occured
                // System.Windows.MessageBox.Show("socket");
                break;
            }

            if (bytesRead == 0)
            {
                //the client has disconnected from the server
                // System.Windows.MessageBox.Show("disc");
                break;
            }

            //message has successfully been received
            ASCIIEncoding encoder = new ASCIIEncoding();

            String textdata = encoder.GetString(message, 0, bytesRead);
            System.IO.File.AppendAllText(@"D:\ipdata.txt", textdata);



            //mainwind.setText(encoder.GetString(message, 0, bytesRead));
            //System.Windows.MessageBox.Show(encoder.GetString(message, 0, bytesRead));
            // System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
        }

        tcpClient.Close();
    }
}

And I am getting error at the following line of the code...

this.tcpListener.Start();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Adi
  • 1,395
  • 11
  • 37
  • 61
  • 1
    You dont create a new instance of `tcpListener` anywhere.. try adding something like `tcpListener = new TcpListener (..)` – Jens Kloster Nov 25 '13 at 14:22

2 Answers2

2

Your TcpListener is null. You need to call new on it and create an actual instance of it.

private TcpListener tcpListener;

Should probably be

private TcpListener tcpListener = new TcpListener();
nvoigt
  • 75,013
  • 26
  • 93
  • 142
2

You have only declared tcpListener

private TcpListener tcpListener;

It is not having any value. It is null.

You have to define it first before using it.

try

private TcpListener tcpListener = new TcpListener();
Microsoft DN
  • 9,706
  • 10
  • 51
  • 71