Extended class of TcpClient doesn't accept with EndAcceptTcpClient(ar) function

85 views Asked by At

I've created a new class which inherits the TcpClient class :

Public Class MyTcpClient
    Inherits Net.Sockets.TcpClient

    Private _ClintName As String

    Sub New(ByVal host As String, ByVal port As Integer, ByVal ClientName As String)
        MyBase.New(host,port)
        _ClintName = ClientName
    End Sub

    Public Property ClientName()
        Get
            Return _ClintName
        End Get
        Set(ByVal value)
            _ClintName = value
        End Set
    End Property
End Class

I tried to connect to an instance of TcpListener:

Try
    client = New MyTcpClient(ip, port , "ClientName")
Catch ex As Exception
    xUpdate("Can't connect to the server!")
End Try 

However on the server side, there is a problem with the EndAcceptTcpClient function. The server side code is something like this:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    Dim NewClient As MyTcpClient
    Listning = New TcpListener(GetMyIP, 3818)
    Listning.Start()
    UpdateList("Server Starting", False)
    Listning.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listning)
End Sub

Sub AcceptClient(ByVal ar As IAsyncResult)
    NewClient = Listning.EndAcceptTcpClient(ar)
    UpdateList("New Client Joined!", True)
    Listning.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listning)
End Sub

The error is occuring on the first line of the AcceptClient sub :

NewClient = Listning.EndAcceptTcpClient(ar)

The error message is:

Unable to cast object of type 'System.Net.Sockets.TcpClient' to type 'ServerChat.MyTcpClient'.

If I change MyTcpClient to TcpClient everything will be ok.

Should I do any changes on the TcpListener object or is my attempt wrong?

0

There are 0 answers