khairilz 0 Newbie Poster

i need help on programming networking..

i'm creating a winservice called HeartBeat. this service is to check if the gateway(list of gateway) is alive and able to do certain process( other application) and if it's down after certain tries, it will notified related person(email/sms/voicemail).
We a doing terminal application for WAN. My supervisor told that last time he got issue as the terminal process fail. The gateway is alive but the process still fail. And later he found out that the response came from proxy, not the actual gateway behind it(gateway is down).

My supervisor told me that to write "+++" to the port of the gateway(ipaddress). He said this will not write to the port physicall but send bytes to that port.
the theory is like this..

tcpclient.Connect(ip,port)
ns as networkstream = tcpclient.getstream()
if(ns.canwrite)
ibyte = ns.write("+++")
if(ibyte>0) then
//assume reaconnection
end if
end if

I found out that networkstream only send the data to buffer and return after it finishes writing to buffer.It will not wait for the buffer finish sending to destination.
my issue is
- networkstream does not return anything. then how do i know if the data is successfully sent to the gateway
- networkstream.canwrite is not actually writing to the port also.
it only check if it can write to the stream

my view is, i can achieve this if the gateway writes back to me..
below is my basic code for this app.

Public Sub StartBeat(ByVal pstIpAddress As String, ByVal pstPortNo As String, ByVal pstMaxTries As Integer)

        Dim tcpClient As New System.Net.Sockets.TcpClient()

        Dim Bytes2Write As [Byte]() = Encoding.ASCII.GetBytes("+++")

        If (pstMaxTries = 0) Then
            pstMaxTries = 1
        End If

        Dim tries As Integer = 0

        For tries = 0 To pstMaxTries
            Try
                tcpClient.Connect(pstIpAddress, pstPortNo)
                Dim nstream As NetworkStream = tcpClient.GetStream()
                fstErrmsg = Write2Port(nstream, Bytes2Write, tcpClient)
                If (String.IsNullOrEmpty(fstErrmsg)) Then
                    Exit For
                End If
                

                nstream.Close()
            Catch ex As Exception
                fstErrmsg = ex.Message
            End Try
            If (tries = pstMaxTries) Then
                'sendemail
            End If
        Next

        If (tcpClient.Connected = True) Then
            tcpClient.Close()
        End If


    End Sub

    Private Function Write2Port(ByVal ns As NetworkStream, ByVal lBbyte As Byte(), ByVal tcpClient As TcpClient)
        Dim lstErrmsg As String = String.Empty
        Try
            If (ns.CanWrite) Then
                ns.Write(lBbyte, 0, lBbyte.Length)

                'Dim ReceiveByte As Byte = tcpClient.ReceiveBufferSize

            Else
                lstErrmsg = "Error: Cannot write data to stream"
            End If
        Catch ex As Exception
            lstErrmsg = ex.Message
        End Try

        Return lstErrmsg
    End Function