• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[C#] Object Reference Error

Experienced Elementalist
Joined
Apr 12, 2009
Messages
241
Reaction score
32
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
 
(oO (||||) (||||) Oo)
Loyal Member
Joined
Aug 6, 2009
Messages
2,132
Reaction score
429
ok I see where I made error. A bit more practice and I might be able to produce something.

I need advice. Would you recommend using class for all server stuff (i.e. serverinit, start, on receive, etc)?
 
Experienced Elementalist
Joined
Apr 12, 2009
Messages
241
Reaction score
32
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
 
Newbie Spellweaver
Joined
Jul 21, 2010
Messages
39
Reaction score
0
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.

 
Last edited:
(oO (||||) (||||) Oo)
Loyal Member
Joined
Aug 6, 2009
Messages
2,132
Reaction score
429
Yes, I am using microsoft Visual C# 2008 Express.
Good tool, at the time of posting this thread I forgot to post error itself.
But then I got it sorted out.
 
(oO (||||) (||||) Oo)
Loyal Member
Joined
Aug 6, 2009
Messages
2,132
Reaction score
429
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?

and question. how do i loop so server will accept all connections from different clients?
 
Newbie Spellweaver
Joined
Jul 21, 2010
Messages
39
Reaction score
0
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();
            }
        }
    }
}
 
Experienced Elementalist
Joined
Apr 12, 2009
Messages
241
Reaction score
32
can you add me on msn? I added you but you appear offline all time.
i@danspd.com


GRRRRR
...It never told me you added me... and this has happened before with other people. They add me and I never find out

...Occasionally it does work fine and the message pops up (it's set to)

Anyone know WHY this happens?!
 
Newbie Spellweaver
Joined
Jul 21, 2010
Messages
39
Reaction score
0
Here's an excerpt from a demo project I were putting together:

---------- 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:
(oO (||||) (||||) Oo)
Loyal Member
Joined
Aug 6, 2009
Messages
2,132
Reaction score
429
ok so here is where I am at.
Server:
Client:

Server accepts multiple connections and receives message like it's a loop.
Now how can I send a message from client only when I enter it in console?
I have tried to pu Console.ReadLine() in message but client connection just closes when i send custom message

Advice
 
Newbie Spellweaver
Joined
Jul 21, 2010
Messages
39
Reaction score
0
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
Top