AuthorMessage
Corayzon
Regular
Posts: 67

Code:
Public Class AsyncSocketClient
    Public Class clsClientObject
        Private bConnected As Boolean = True
        Public Socket As System.Net.Sockets.Socket = Nothing
        Public Buffer(3071) As Byte
        Public Tag As Object
        Public Property Connected() As Boolean
            Get
                Return bConnected
            End Get
            Set(ByVal Value As Boolean)
                bConnected = Value
            End Set
        End Property
        Public Sub Send(ByVal sData As String)
            Try
                Me.Socket.Send(System.Text.Encoding.ASCII.GetBytes(sData))
            Catch ex As Exception
                Me.Disconnect()
            End Try
        End Sub
        Public Sub Send(ByVal bData() As Byte)
            Try
                Me.Socket.Send(bData)
            Catch ex As Exception
                Me.Disconnect()
            End Try
        End Sub
        Public Sub Disconnect()
            Try
                Socket.Shutdown(Net.Sockets.SocketShutdown.Both)
                Socket.Close()
            Catch ex As Exception
            End Try
            Socket = Nothing
            bConnected = False
        End Sub
    End Class
    Private iPort As String
    Private sHost As String
    Private Socket As System.Net.sockets.Socket
    Public Property HostName() As String
        Get
            Return sHost
        End Get
        Set(ByVal Value As String)
            sHost = Value
        End Set
    End Property
    Public Property HostPort() As Integer
        Get
            Return iPort
        End Get
        Set(ByVal Value As Integer)
            iPort = Value
        End Set
    End Property
    Public Sub Connect()
        Connect(sHost, iPort)
    End Sub
    Public Sub Connect(ByVal sHostName As String, ByVal piPort As Integer)
        sHost = sHostName
        iPort = piPort
        Socket = New System.Net.sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, _
            System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp)
        Dim ipHostInfo As System.Net.IPHostEntry = System.Net.Dns.Resolve(sHost)
        Dim ipAddress As System.Net.IPAddress = ipHostInfo.AddressList(0)
        Dim localEndPoint As New System.Net.IPEndPoint(ipAddress, iPort)
        Try
            Try
                Socket.Connect(localEndPoint)
                If Socket.Connected = True Then
                    Dim Client As New clsClientObject
                    Client.Socket = Socket
                    RaiseEvent SocketConnected()
                    If Client.Connected = False Then
                        Socket = Nothing
                        Client = Nothing
                        Exit Sub
                    End If
                    Socket.BeginReceive(Client.Buffer, 0, 1024, Net.Sockets.SocketFlags.None, New AsyncCallback(AddressOf ReadCallback), Client)
                End If
            Catch ex As Exception
                RaiseEvent SocketError("Connect()", ex)
                Exit Sub
            End Try
        Catch ex As Exception
            RaiseEvent SocketError("Connect()", ex)
        End Try
    End Sub
    Public Sub Close()
        Try
            Socket.Shutdown(Net.Sockets.SocketShutdown.Both)
            Socket.Close()
        Catch ex As Exception
            RaiseEvent SocketError("Close()", ex)
        End Try
    End Sub
    Public Sub Send(ByVal sData As String)
        Socket.Send(System.Text.Encoding.ASCII.GetBytes(sData))
    End Sub
    Public Sub Send(ByVal bData() As Byte)
        Socket.Send(bData)
    End Sub
    Private Sub ReadCallback(ByVal AsyncResult As IAsyncResult)
        Dim Client As clsClientObject = AsyncResult.AsyncState
        Dim bytesRead As Integer = 0
        Try
            bytesRead = Client.Socket.EndReceive(AsyncResult)
            ' Check if socket was disconnected
            If bytesRead = 0 Then
                RaiseEvent SocketClosed()
                Client.Disconnect()
                Client = Nothing
                Exit Sub
            End If
        Catch ex As System.Exception
            RaiseEvent SocketClosed()
            Client.Disconnect()
            Client = Nothing
            Exit Sub
        End Try
        ' call data arrival
        RaiseEvent DataArrival(Client, Client.Buffer, bytesRead)
        ' check for disconnection
        If Client.Connected = False Then
            RaiseEvent SocketClosed()
            Client = Nothing
            Exit Sub
        End If
        ' clear buffer and wait for data
        Dim Bytes(3071) As Byte
        Client.Buffer = Bytes
        Try
            Client.Socket.BeginReceive(Client.Buffer, 0, 1024, 0, New AsyncCallback(AddressOf ReadCallback), Client)
        Catch ex As Exception
            RaiseEvent SocketClosed()
            Client.Disconnect()
            Client = Nothing
            Exit Sub
        End Try
    End Sub
    Public Event SocketConnected()
    Public Event SocketClosed()
    Public Event DataArrival(ByRef Client As Om_Tribe_Components.AsyncSocketClient.clsClientObject, ByVal bData() As Byte, ByVal bytesRead As Integer)
    Public Event SocketError(ByVal sSource As String, ByVal Ex As System.Exception)
End Class

Copy this class into a VB.Net source file. To use declare a new variable as a AsyncSocketClient.
Public Socket as New AsyncSocketClient
If you need help using this class, just pop a reply to this post =]
Corayzon
Regular
Posts: 67

Class updated...
* Connect() is now public
* Send() added to class
* Close() works now
once again, if u have any better methods this class could use please post the changes =]
Corayzon
Regular
Posts: 67

Class updated.
*** Fully tested
MethodZ
n00b
Posts: 12

This is way over my head .... but im taking notes
Corayzon
Regular
Posts: 67

maybe ill show u how to use this class if u ask nicely ^^