[C#] Object Reference Error

Dan, where's the error..? On what line does it give you an object not set error..?

...Note a couple things...

1 - you never use your ClientsList array .. which is fine if you don't need it, but why declare it?

2 - You receive only a single packet, and then the connection goes into oblivion (not closed, just can't ever be accessed again). ...In your OnReceive, make another call to BeginReceive

3 - In your EndReceive, you should look at it's return value - that's the number of bytes it read. "int numbytes = clientSocket.BeginReceive(ar)" ... because the buffer you use is a byte[1024] - but you're not going to receive 1024 bytes of data, you're going to receive however many random bytes you happened to receive in a single packet (which you can TRY to control, but can't).

4 - ASCIIEncoding.ASCII.GetString(byteData) is working on the ENTIRE byte[1024]... instead of just the number of bytes you actually received. You shold change it to work on only however many bytes you receive. There's an overload for it, I think GetString(byteData, 0, NumBytes); or something like that.

---------- Post added at 10:07 AM ---------- Previous post was at 10:05 AM ----------

5 - Just a FYI, you're only binding to 127.0.0.1, meaning you can only connect to yourself using the 127.0.0.1 IP... If you want to bind to any ip, use IPAddress.Any
 
Yeah .. I tend to do that. Put everything involving your server's functionality into a class...

class AwesomeServer
{
...
}

...Because then, say you wanted to run 2 servers in the same application (maybe on different ports or something)... you could just create two AwesomeServer classes :)

Besides, it's just cleaner when you keep things nicely object oriented
 
I have no clue which, if any, IDE you are using, but you should consider using Visual Studio for step-by-step-debugging your application.

In case you are not using any IDE, you should definitely get the one from Microsoft, it's an excellent IDE, even if you will be using the "cheapo free" one.

Microsoft Express Downloads - Visual Studio Express and SQL Server Express
 
Last edited:
please correct me.

Basically in one function you create socket, bind ip, and start listening.
Also there you add beginaccept with callback function which will be executed when new client is connected and accepted.

Is that right?

---------- Post added at 12:21 PM ---------- Previous post was at 12:16 PM ----------

Also is this going in right waY? C# | class IPtServer { - Anonymous - YrXrywPY - Pastebin.com

and question. how do i loop so server will accept all connections from different clients?
 
Since sockets do indeed block in reality, the asynchronous programing model does much of the magic behind the scenes.

However, you will need to re-wire/chain to Begin- once you are done with your management logic (get client socket, get receive/sent length) in your method invoked by the callback.

Note: You do not want to re-wire/chain when a client dropped connection (receive length == 0 indicates that the client disconnected).

---------- Post added at 07:33 PM ---------- Previous post was at 07:21 PM ----------

There is no loop since asynchronous programming is an elegant solution to avoid procedural programming (looping) altogether.

All you need to do is instantiate a fresh server instance and "loop" in the main program (Program.cs) . While debugging I use this approach

Code:
using System;
using System.Collections.Generic;
using System.Text;
#region Using directives
using System.Net;
#endregion

namespace com.ragezone.Tutorial.Server
{
    class Program
    {
        static void Main(string[] args)
        {
            // Set the title of the console window
            Console.Title = "Server";

            ServerInstance server = new ServerInstance(new IPEndPoint(IPAddress.Parse("0.0.0.0"), 13370));
            
            string command = string.Empty;
            // Read from stdin while the given command is not "!q" nor "quit"
            while (command != "!q" && command != "quit")
            {
                // Read the command
                command = Console.ReadLine();
            }
        }
    }
}
 
Here's an excerpt from a demo project I were putting together: http://pastebin.com/2A0FFtfM

---------- Post added at 07:53 PM ---------- Previous post was at 07:39 PM ----------

Hope my post lit a light for you, now I go to war in Bad Company 2

Take care and happy coding.
 
Last edited:
When I started getting my hands dirty with the .NET platform I bought a book about network programming with C# and to be completely honest with you I got increasingly dazed and confused just by reading it.

This is why I strongly suggest you should take a deeper dive into .NET and explore it in order to harness its full power before trying to (design and) write a client/server application.

I mean no harm, but think it is a tad too complex for platform newcomers. Not that I doubt your capacities, but the .NET platform has been designed very intelligently and offers very smart and elegant ways to do stuff. You should take advantage of its strengths and get back to network programming when you are familiar with the ".NET way of doing things" and the higher level concepts such as asynchronous programming :):


I went for a smoke and contemplated the lack of in-depth articles and online resources on this subject and came to the conclusion that there is too much ground to cover, which is why there is books on the subject.
 
Last edited:
Back