473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Specified cast is not valid TcpClient TcpListener

This code gives me the runtime error "Specified cast is not valid":
MyTcpClient ClientTcp=(MyTcpClient)ListenerTcp.AcceptTcpClient ();

MyTcpClient is a class that inherits from TcpClient. Why can't I do
this? Is it possible to get the remote endpoint without have to go to
the underlying socket? I don't want to mess with sockets and I just
want the network stream to read/write data.

Nov 17 '05 #1
1 1903
Your problem is one of basic inheritance... a dog is a mammal, but not all
mammals are dogs.

You are trying to typecast a base class into a class that inherits the base
class... this is not possible. It’s like typecasting a mammal to be a dog...
in some cases it might work, if the object you are receiving is actually of
that type (which it is not in this case).

As for accessing the end point of the socket... the quickest way to it is
through the socket you receive... namely through it’s RemoteEndPoint
property... to do so you would simply say:

EndPoint ep = ListenerTcp.AcceptTcpClient().RemoteEndPoint;

As for creating a network stream... again you would need the socket in some
way... the optimal way to do it would be to capture the returned class from
AcceptTcpClient() and pass it into the constructor of NetworkStream.

Brendan
"Nuno Magalhaes" wrote:
This code gives me the runtime error "Specified cast is not valid":
MyTcpClient ClientTcp=(MyTcpClient)ListenerTcp.AcceptTcpClient ();

MyTcpClient is a class that inherits from TcpClient. Why can't I do
this? Is it possible to get the remote endpoint without have to go to
the underlying socket? I don't want to mess with sockets and I just
want the network stream to read/write data.

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: zZ | last post by:
Hi All, I'm building a TCPListener as the server to accept TCPClient connections, using TCPListener.AcceptTcpClient. I'd like to retrieve client's IP after connected, but found no way to do...
3
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
10
by: Abubakar | last post by:
hi, I work on a computer that is part of a network and uses proxy to connect to net. I cant connect to servers outside my proxy with simple ConnectTo code. I need to know how to make my requests go...
1
by: MuZZy | last post by:
HI, How do i get a remote TcpClient address here? // ======================================= TcpListener l = new TcpListener(IPAddress.Parse("127.0.0.1"), 8080); TcpListener.Start(); While...
2
by: James | last post by:
I'm a newbie to vb.net, and classes were something I never played with in "standard" vb, but... Basically,part of my program comprises of some tcp/ip stuff, and I used the following code to...
0
by: den 2005 | last post by:
Hi everybody, I am still learning how sockets, TcpClient and TcpListener work, in order that both sides to send and receive data, both sides must have a socket or TcpListener and a TcpListener...
2
by: Martin Arvidsson | last post by:
Hi! I am just beginning to learn about the TcpListener and TcpClient and Socket. I have figured out the TcpListener, it is used for booth in and outgoing requests, right? But when to use...
11
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:...
1
by: secutos | last post by:
When a TcpListener accepts a connection, it returns a TcpClient, which allows you to recieve data. But what about TcpListener.Server.Receive? Does that also allow to receive data in the same way? Or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.