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
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