NoobStudios
NoobStudios

Reputation: 3

Socket.Accept Throws Null Reference Exception

First of all, i'm new to all this Stack Overflow thingy.

I'm mostly a Unity programmer, but i need to run some code on my Pi and Unity can't build for it. Therefore, i'm using C# so i can later build with Mono.

I have a basic socket server code that runs off of unity, and it works fine. I tried porting it over to VS but i always get a Null Ref Exception when calling Socket.Accept.

    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;


class RasPi
{
    IPAddress ipAddress;
    IPEndPoint localEndPoint;

    Socket listener;
    Socket handler;

    int i = 0;

    string data = string.Empty;

    public string IP = "192.168.10.5";
    public int Port = 5001;

    static void Main(string[] args)
    {
        new RasPi().Initialize();
        new RasPi().StartListening();
    }


    void Initialize()
    {
        i = 0;

        ipAddress = IPAddress.Parse(IP);
        localEndPoint = new IPEndPoint(ipAddress, Port);


        listener = new Socket(AddressFamily.InterNetwork,
                             SocketType.Stream,
                             ProtocolType.Tcp);

        listener.Bind(localEndPoint);
        listener.Listen(10);
    }

    public void StartListening()
    {

        try
        {
            while (true)
            {
                if (i == 0)
                {
                    Console.WriteLine(listener.Accept());
                    handler = listener.Accept();
                }

                data = string.Empty;


                while (true)
                {
                    byte[] bytes = new byte[1024];
                    int bytesRec = handler.Receive(bytes);

                    data = Encoding.ASCII.GetString(bytes, 0, bytesRec);

                    ProcessData(data);

                    handler.Send(bytes);

                    break;
                }


                //Restart();
                Console.ReadKey();
                break;
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }


   /* void Restart()
    {    
        i++;
        StartListening();
    }*/

    public void ProcessData(string data)
    {
        switch (data)
        {

        }
    }
}

Errors occur whenever i call listener.Accept();

In Unity, this worked fine. Anyone willing to help me here?

PS: I'm new to Networking, so i'm probably doing something stupid and i don't realise it.

Upvotes: 0

Views: 483

Answers (1)

Hatted Rooster
Hatted Rooster

Reputation: 36483

You create 2 RasPi objects and call Initialize() and StartListening() on them individually. The second object won't have the same state as the first object that called Initialize() so StartListening() fails miserably. Instead, create one object and call both methods on it:

var rasPi = new RasPi();
rasPi.Initialize();
rasPi.StartListening();

Upvotes: 2

Related Questions