Hi!
I'm developing a very simple chat application in VB.NET and I've stumbled
into a problem.
For simplicity I'm using the TcpListener and TcpClient classes. However, at
one point I discovered that I want to use the protected Active and Client
properties of the TcpClient class. To do this I made my own derived class:
Public Class MyTcpClient
Inherits TcpClient
Public ReadOnly Property IsActive() As Boolean
Get
Return Me.Active
End Get
End Property
Public ReadOnly Property IPAddress() As IPAddress
Get
Dim aSocket As Socket = Me.Client
Return CType(aSocket.R emoteEndPoint, IPEndPoint).Add ress
End Get
End Property
End Class
To wait for connections I start a TcpListener and use the AcceptTcpClient
method to assign a TcpClient. Originally this was done like this:
Dim Client As New TcpClient
Client = Listener.Accept TcpClient()
This worked really well, so when I changed to my derived class I use the
following code:
Dim Client As New MyTcpClient
Client = CType(Listener. AcceptTcpClient (), MyTcpClient)
However, this doesn't work but throws this exception:
System.InvalidC astException: Specified cast is not valid.
What am I doing wrong here? I'm quite new to programming in VB, but I know
several other languages where I could accomplish something similar without
problems. Maybe the problem is just that I don't know the correct VB syntax?
As I see it, there shouldn't be any problems assigning an instance of a
super class to an instance of a derived class.
Anyway, any help would be most appreciated.
Cheers, Anders 1 1735
> Dim Client As New MyTcpClient Client = CType(Listener. AcceptTcpClient (), MyTcpClient)
Listener.Accept TcpClient() is a Microsoft function that returns a Microsoft
TcpClient, and since Microsoft does not know about your MyTcpClient type,
you can not do the cast.
As I see it, there shouldn't be any problems assigning an instance of a super class to an instance of a derived class.
That's false. There can be casting problems if the instance of the super
class is not already an instance of the derived class. Only the opposite is
true: you can always assign a derived class object to a super class object.
That is, you can always convert a car into a vehicle, but you can not always
convert a vehicle into a car because of:
1) Maybe the vehicle is really a "disguised" truck.
2) Maybe the vehicle is neither a car nor an truck, it is just a generic
vehicle, and it wouldn´t know which is the aspect and behaviour of a car.
These concepts are independent of the programming language.
--
Carlos J. Quintero
The MZ-Tools all-in-one add-in, now for .NET: http://www.mztools.com
"Anders Berg" <an****@lbs.s e> escribió en el mensaje
news:41******** @news.wineasy.s e... Hi! I'm developing a very simple chat application in VB.NET and I've stumbled into a problem. For simplicity I'm using the TcpListener and TcpClient classes. However, at one point I discovered that I want to use the protected Active and Client properties of the TcpClient class. To do this I made my own derived class:
Public Class MyTcpClient Inherits TcpClient
Public ReadOnly Property IsActive() As Boolean Get Return Me.Active End Get End Property Public ReadOnly Property IPAddress() As IPAddress Get Dim aSocket As Socket = Me.Client Return CType(aSocket.R emoteEndPoint, IPEndPoint).Add ress End Get End Property End Class
To wait for connections I start a TcpListener and use the AcceptTcpClient method to assign a TcpClient. Originally this was done like this: Dim Client As New TcpClient Client = Listener.Accept TcpClient()
This worked really well, so when I changed to my derived class I use the following code: Dim Client As New MyTcpClient Client = CType(Listener. AcceptTcpClient (), MyTcpClient)
However, this doesn't work but throws this exception: System.InvalidC astException: Specified cast is not valid.
What am I doing wrong here? I'm quite new to programming in VB, but I know several other languages where I could accomplish something similar without problems. Maybe the problem is just that I don't know the correct VB syntax? As I see it, there shouldn't be any problems assigning an instance of a super class to an instance of a derived class.
Anyway, any help would be most appreciated.
Cheers, Anders
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jim |
last post by:
I'm working on my first .net project using c#. I've been using
activex controls embedded in a web page and wanted to do the same in
..net. My approach has been to create a windows control library and
then reference the dll with a web page as follows:
<html>
<body>
<center>
<object id="UserControl1" height="500" width="700"
classid="http://wiffle/WindowsControlLibrary3.dll#WindowsControlLibrary3.UserControl1">
|
by: alex002 |
last post by:
Dear all,
I am writing to a C# program to connect to the telnet server and getting
data; however, I know that I can use either TCPClient and Asyncronous Socket
to do the program. However, I don't know which is better and why. Can any
one tell me about this? Thank you for all of your help.
Alex
|
by: user |
last post by:
Hello
How can i read IP of incoming connection when i have TcpClient for that
connection ?
Thanx
|
by: Sebho |
last post by:
Hello,
i made an app that use sockets, based on this sample :
http://www.microsoft.com/downloads/details.aspx?FamilyId=4B3215C0-62E7-4B84-9944-7582B8CD0125&displaylang=en
i'have a problem, i can't obtain the client IP adress on the server side (i
have a pointer to a TCPclient object)
Is averyone that know how to retrieve the IPadress from a TCPclient Object ?
|
by: hamil |
last post by:
I am having trouble using the TcpListener and TcpClient classes.
At the end of this post is server code that runs, and a class whose purpose
is described below.
I need to know when the client closes the connection so I can exit my
listener code. Here is what I have tried.
First I asked MSDN and they said..
| |
by: DrZoop |
last post by:
Long story short: I am misusing the TCPClient class. I am sending
400-500 byte messages, and data is being lost.
How can the TCPClient class be misused to lose data?
Background notes:
-I know the TCP protocol doesn't lose data. Don't tell me that. I am
educated.
-I am buffering all data being received before checking for messages
and reading them, so don't tell me this either. I have quite a spiffy
|
by: =?Utf-8?B?R2Vvcmdl?= |
last post by:
Hello everyone,
I am wondering if I am using TCPClient class in C#, how to setup timeout
value? Timeout I mean, when connects to server for the 1st time, and during
<timeoutinterval, if no server response is received, the GetStream method
will return will return other than wait forever.
thanks in advance,
|
by: Bjoern Schliessmann |
last post by:
Hello,
I'm currently trying to implement a simulation program with Kamaelia
and need a reliable TCP connection to a data server.
From Twisted, I know that a method is called if the connection fails
by whatever reason. I tried to get the same results with Kamaelia's
TCPClient component. If I start up the component and try to connect
to a closed TCP port it fails and sends a message out of the signal
box, that's okay.
|
by: batmanfreeze |
last post by:
I am using C# in .Net 1.1, and need to access the 'Client' Property of TcpClient, so I created a derived class to do this based upon the Microsoft Sample, located at:
http://msdn2.microsoft.com/en-us/library/system.net.sockets.tcpclient.client(VS.71).aspx
The only problem is that in my main app, when I try to do the following, I get an Invalid Cast Exception:
int PortNum = 10000;
TcpListener objTcpListen = new...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |