Socket subclassing oddity

L

Lyndon Hughey

I would like to have the ability to add a custom property a subclass of the
Sockets class, but i'm having odd issue with casting.

I have a TcpListener object than creates a socket using the AcceptSocket()
method like this:
TcpListener tcpListener = new TcpListener(1000);
Socket sock= tcpListener.AcceptSocket();

In order to keep track of the name of the client that signs on to the
socket, i have created a custom class that implements the socket class.
This class is simply named SocketWrapper and does nothing more than
implements the Socket class as a base class, as shown below:
class SocketWrapper:Socket

{

private string connectedCompany;

public SocketWrapper(SocketInformation
socketInformation):base(socketInformation){}


public void AcceptConnection(Socket s){}

//

public string Processor{

get { return processor; }

set { processor = value; }

}

}



I would like to have the ability to cast the tcpListener.AcceptSocket()
method to my SocketWrapper class like this:

SocketWrapper sock = (SocketWrapper)tcpListener.AcceptSocket();

that way, i could easily keep track of the company connected to the socket
with the SocketWrapper's connectedCompany property. The code above
compiles, but I get this error when i try to run it:

Unable to cast object of type 'System.Net.Sockets.Socket' to type
'SocketWrapper'. System.Exception {System.InvalidCastException}

Does anyone have a clue on why this is displaying this runtime error? I
find it odd since the base class of the SocketWrapper class is the socket
class. Casting should be no problem. Alternately, do you have a better
idea on how i could add a custom property to the Socket class or a subclass?

thanks,

Lyndon
 
J

Jon Skeet [C# MVP]

Unable to cast object of type 'System.Net.Sockets.Socket' to type
'SocketWrapper'. System.Exception {System.InvalidCastException}

Does anyone have a clue on why this is displaying this runtime error?

Absolutely - tcpListener.AcceptSocket() isn't returning a
SocketWrapper, it's returning whatever Socket implementation it would
normally do.
I find it odd since the base class of the SocketWrapper class is the socket
class. Casting should be no problem.

Casting will always be a problem when the runtime type isn't the
appropriate one. What you've done is the equivalent of:

object o = new object();
string s = (string) o;

That will cause the same error.
Alternately, do you have a better
idea on how i could add a custom property to the Socket class or a
subclass?

Create a genuine *wrapper* - one that *has* a Socket, rather than
*deriving* from Socket.
 
U

UL-Tomten

I find it odd since the base class of the SocketWrapper class is the socket
class. Casting should be no problem.

Well, as Jon already pointed out, you're attempting to "down cast"
here. If you step through this in a debugger, you can see which type
AcceptSocket() actually returns.

Since you're using C#, if you can use Orcas, an extension method might
suit your needs better than a wrapper class. Using this, you can
effectively add methods to the Socket class without wrapping or
subclassing.
 
L

Lyndon Hughey

thanks for your reply, Jon. I appreciate it.

Jon Skeet said:
Absolutely - tcpListener.AcceptSocket() isn't returning a
SocketWrapper, it's returning whatever Socket implementation it would
normally do.


Casting will always be a problem when the runtime type isn't the
appropriate one. What you've done is the equivalent of:

object o = new object();
string s = (string) o;

That will cause the same error.


Create a genuine *wrapper* - one that *has* a Socket, rather than
*deriving* from Socket.
 

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