DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Client Object

10.04.2006
| 507 views |
  • submit to reddit
        // Simply tcp/ip client

Imports System.Net.Sockets
Imports System.Text
Imports System.Windows.Forms
Public Class ClientObj
    Private tcpClient As New System.Net.Sockets.TcpClient()
    Private readPulseTimer As Timer
    Private isReadingStream As Boolean = False
    Public Sub New(ByVal ipAddress As String, ByVal port As String)
        Try
            tcpClient.Connect(ipAddress, port)
            Me.readPulseTimer = New Timer
            Me.readPulseTimer.Interval = 250
            AddHandler readPulseTimer.Tick, AddressOf readPulseTick
            Me.readPulseTimer.Start()
        Catch ex As Exception
            Throw New Exception("Cannot connect to server! Error: " & ex.Message)
        End Try
        Dim networkStream As NetworkStream = tcpClient.GetStream()
        If networkStream.CanWrite And networkStream.CanRead Then
        Else
            If Not networkStream.CanRead Then
                tcpClient.Close()
                Throw New Exception("Cannot read from the Server!")
            Else
                If Not networkStream.CanWrite Then
                    tcpClient.Close()
                    Throw New Exception("Cannot send to the Server!")
                End If
            End If
        End If
    End Sub
    Public Sub SendToServer(ByVal msg As String)
        Try
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(msg & "|")
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            networkStream.Write(sendBytes, 0, sendBytes.Length)
        Catch ex As Exception
            tcpClient.Close()
            Throw New Exception("Error sending data to server! Error: " & ex.Message)
        End Try
    End Sub
    Public Event ServerMessage(ByVal msg As String)
    Public Sub close()
        While isReadingStream
        End While
        Me.readPulseTimer.Enabled = False
        Me.tcpClient.Close()
    End Sub
    Private Sub readPulseTick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            isReadingStream = True
            Me.readPulseTimer.Stop()
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            If networkStream.DataAvailable Then
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                Dim clientdata As String = ""
                For i As Integer = 0 To bytes.Length - 1
                    If bytes(i) <> 0 Then
                        clientdata &= Encoding.ASCII.GetString(bytes, i, 1)
                    End If
                Next
                If clientdata <> "" Then
                    Dim queued As String() = clientdata.Split("|")
                    For Each str As String In queued
                        If str <> "" Then
                            RaiseEvent ServerMessage(str)
                        End If
                    Next
                End If
            End If
            isReadingStream = False
            Me.readPulseTimer.Start()
        Catch ex As Exception
            tcpClient.Close()
            Throw New Exception("Error reading data from server! Error: " & ex.Message)
        End Try
    End Sub
End Class