-1

When server started by the button, below code gives an SystemNullReferenceException error in function named Runner. One of my friend says that the code is working in his computer, but I can not make it work in my computer. When I started the code by executable file, it falls into not responding state, and I need to close it from task manager by killing the program. From textboxes IP is read as 127.0.0.1 and the port is 13000. I appreciate your guidance.

namespace FakeDataTCP_Server_NameSpace
{
    public partial class FakeDataTCP_ServerForm : Form
    {
        public FakeDataTCP_ServerForm()
        {
            InitializeComponent();
        }

        private void buttonServerRUN_Click(object sender, EventArgs e)
        {
            textBoxSTATUS.Clear();
            IPAddress sunucuIP = IPAddress.Parse(comboBoxServerIP.Text);
            int sunucuPORT = Int32.Parse(textBoxServerPORT.Text);
            textBoxSTATUS.AppendText("sunucu IP: " + sunucuIP.ToString() + "\r\n");
            textBoxSTATUS.AppendText("PORT: " + sunucuPORT.ToString() + "\r\n");

            Socket sunucuSoketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint sunucuEndIP = new IPEndPoint(sunucuIP, sunucuPORT);

            sunucuSoketListener.Bind(sunucuEndIP);
            sunucuSoketListener.Listen(100);

            textBoxSTATUS.AppendText("Server Started (Listening) \r\n");
            Socket istemciSoketi = default(Socket);
            FakeDataTCP_ServerForm p = new FakeDataTCP_ServerForm();
            Thread sunucuThread = new Thread(new ThreadStart(() => p.Runner(istemciSoketi)));
            sunucuThread.Start();

            int istemciSAYISI = 0;
            while (true)
            {
                istemciSAYISI++;
                istemciSoketi = sunucuSoketListener.Accept();
                textBoxSTATUS.AppendText(istemciSAYISI + " clients connected \r\n");
            }
        }
        public void Runner(Socket istci)
        {
            try
            {
                while (true)
                {
                    byte[] msg = new byte[1024];
                    int ebat = istci.Receive(msg);
                    istci.Send(msg, 0, ebat, SocketFlags.None);
                }
            }
            catch
            {
                MessageBox.Show("failed");
            }
        }

    }
}
Minik
  • 1
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Fildor May 10 '22 at 21:07

1 Answers1

1

gives an SystemNullReferenceException error in function named Runner.

Looking at this function, I don't see anything inside that could itself be null, except for the istci Socket passed as an argument.

Working backward from there I see the function is called like this:

 p.Runner(istemciSoketi)

And working still further backwards I see the istemciSoketi variable is defined like this:

Socket istemciSoketi = default(Socket);

Aha! default(Socket) is null, so istemciSoketi doesn't have a value yet! The Socket you are trying to use was never actually created. You do assign a value later on:

istemciSoketi = sunucuSoketListener.Accept();

... but this doesn't happen until after p.Runner() was called.

To fix the issue, move the code calling p.Runner() to just after the SocketListener accepts a connection.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Thanks for your comment. Null error is resolved. Now with netstat command, I can see that server starts listening. May I ask your comment about program's not responding state? Why everytime I need to kill the program? – Minik May 10 '22 at 21:31
  • There's nothing to ever break out of the `while (true)` loop. – Joel Coehoorn May 11 '22 at 13:56