C# Class that contains a socket

T

tcomer

Hello - I'm working on a simple instant messenger application, and I
have it working but in order to achieve the results I want I need to
make a few changes. The problem is that I have a class that contains a
Socket. If I try to assign a socket reference to the socket in the
Client class a NullReferenceException is thrown. I know that
m_mainSocket is valid because it works fine if I declare an array of
sockets instead of using the Client class. Am I overlooking something
possibly? Any help is appreciated.

private Client[] m_Client = new Client[10];
private Socket m_mainSocket = new Socket(<params here>);
....
....
public void ClientConnect(IAsyncResult result)
{
try
{
m_Client[0].m_clientSocket = m_mainSocket.EndAccept(result); //
line 292
}
catch(NullReferenceException nre){ MessageBox.Show(nre.StackTrack);}
// finds null reference at line 292
....
....
public class Client{

public System.Net.Sockets.Socket m_clientSocket;
public string m_clientName;
}

I've also tried initializing m_clientSocket using a constructor in the
Client class but that doesn't seem to solve the problem.
 
T

Truong Hong Thi

If you put a breakpoint there, you'll know what is null.
From the snippet you posted, it is m_Client[0] which is null.
When you write:
private Client[] m_Client = new Client[10];
That mean you instantiate an array to store 10 clients - and 10 are all
initialized to null references.
To solve this, you have to instantiate it before using. Example:
m_Client[0] = new Client();
m_Client[0].m_clientSocket = m_mainSocket.EndAccept(result);

Or you can instantiate all of them in your class constructor in so you
do not have to instantiate them again in ClientConnect method.

for (int i = 0, n = m_Client.Length; i < n; i++)
{
m_Client = new Client();
}

Hope that helps,
Thi
http://thith.blogspot.com
Hello - I'm working on a simple instant messenger application, and I
have it working but in order to achieve the results I want I need to
make a few changes. The problem is that I have a class that contains a
Socket. If I try to assign a socket reference to the socket in the
Client class a NullReferenceException is thrown. I know that
m_mainSocket is valid because it works fine if I declare an array of
sockets instead of using the Client class. Am I overlooking something
possibly? Any help is appreciated.

private Client[] m_Client = new Client[10];
private Socket m_mainSocket = new Socket(<params here>);
...
...
public void ClientConnect(IAsyncResult result)
{
try
{
m_Client[0].m_clientSocket = m_mainSocket.EndAccept(result); //
line 292
}
catch(NullReferenceException nre){ MessageBox.Show(nre.StackTrack);}
// finds null reference at line 292
...
...
public class Client{

public System.Net.Sockets.Socket m_clientSocket;
public string m_clientName;
}

I've also tried initializing m_clientSocket using a constructor in the
Client class but that doesn't seem to solve the problem.
 
T

tcomer

Thanks for the quick reply. I tried your suggestions and unfortunately
it still throws the exception. At this point I'm wondering if it is
even possible to access a Socket through an array of objects. I've
tried initializing each element of the array in the constructor:

for(int i = 0; i < m_Client.Length; i++)
m_Client = new Client();

I have also just tried the one element before assigning it:

m_Client[0] = new Client();
m_Client[0].m_clientSocket = m_mainSocket.EndAccept(asyn);

I've tried:

public class Client{
public System.Net.Sockets.Socket m_clientSocket;

public Client(){
m_clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
}
}

Still no luck. Thanks for the feedback though.
 
P

Peter Duniho

tcomer said:
Thanks for the quick reply. I tried your suggestions and unfortunately
it still throws the exception.

Well, then unfortunately you have a whole new bug. The reply from Thi
seemed very much to the point, IMHO. He certainly did point out an actual
bug in your code.
At this point I'm wondering if it is
even possible to access a Socket through an array of objects.

Of course it is possible. Why wouldn't it be?

The first two code snippets you posted to initialize the array elements
should resolve the null reference of trying to access an array element that
hasn't been initialized yet. That was certainly at least one bug in the
code you posted first. (The last snippet, initializing the socket in the
Client constructor, has no apparent relevance to the problem you originally
described).

However, it appears you may have more than one bug, or that you introduced a
new bug in trying to fix the earlier one. There's no way for anyone to help
you find and fix that one unless you post at least as much detail as you did
in your first post. If you *really* want for people to be able to help you,
you ought to run your code in the debugger, and find out exactly *what* in
the line of code is null.

Of course, if you did that, you ought to be able to figure out by yourself
how to ensure that element of the line of the code isn't null. But at a
minimum, expecting others to just figure out what's wrong with your code
without any specific guidance from you seems somewhat unreasonable to me.

So, if you are still having problems, use the debugger and get *all* of the
information available about the null reference exception that is available,
and post that along with the relevant lines of code.

Pete
 
T

tcomer

I apoligize for my lack of expertise in the art of requesting help
and/or suggestions. I'll be sure to pick up a book on the subject
before doing so in the future. Thanks.
 
P

Peter Duniho

William Stacey said:
forget the book. Your questions are good.

Perhaps you could answer them for him then. Those of us who have tried
apparently don't have the intelligence to properly interpret his "good
questions". Since his questions are good, you should have no trouble giving
him the precise answer he needs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top