Socket = TCPClient.Client

Posted on 2005-05-09
Last Modified: 2012-06-27
Is there a way to get the System.Net.Socket from a TCPClient ? in the help it says that the TCPClient class has a protected property "client" that gets "or sets?" the underlying socket. The TCPClient that I have delcared does not give me the following option....
(Assuming TCPClient is already in action)

Dim Sock as Socket
sock = Tcpclient.client

How would I accomplish this?

Question by:addicktz
    LVL 1

    Accepted Solution

    The Client property is not accessible in this context because it is protected.

    To use this property you will have to create a class which Derives from System.Net.Sockets.TcpClient.

    Here's an example:

    LVL 1

    Author Comment

    with that example, i am still not sure how to return the socket, how would i return a value using that class?
    LVL 1

    Expert Comment

    Inside that class you could create a public variable of type Socket. You can then assign this variable with Me.client. Then write a SUB to return the variable.

    If u r having trouble using the class let me know.
    LVL 1

    Author Comment

    ya, im lost
    LVL 1

    Author Comment

    this is what I was able to come up with, but It is not letting me use the active property, and I dont know if I am doing this right at all, its not working thats for sure =]

        Public Class gets
            Inherits TcpClient
            Public c As Object
            Public Sub New(ByVal clint As TcpClient)
                c = clint
            End Sub 'New

            Public Function gethandle() As String

                'Uses the protected 'Active' property  belonging to the TcpClient base class
                'to determine if a connection is established.
                If c.Active = True Then
                    ' Calls the protected 'Client' property belonging to the TcpClient base class.
                    Dim s As Socket = c.Client
                    'Uses the Socket returned by Client to set an option that is not available using TcpClient.
                    gethandle = s.Handle.ToString
                End If
                'To free all resources, calls protected virtual method Dispose belonging to the TcpClient base class.
            End Function 'UsingProtectedMethods
        End Class 'MyTcpClientDerivedClass
    LVL 1

    Author Comment

    when it says underlying socket....does that mean the active socket in that class? what if there is two?

    should I be putting that code in the class with the tcpclient?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now