VB.Net Networking Heartbeat

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 1
Reputation: khairilz is an unknown quantity at this point 
Solved Threads: 0
khairilz khairilz is offline Offline
Newbie Poster

VB.Net Networking Heartbeat

 
0
  #1
Oct 23rd, 2009
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.
  1. Public Sub StartBeat(ByVal pstIpAddress As String, ByVal pstPortNo As String, ByVal pstMaxTries As Integer)
  2.  
  3. Dim tcpClient As New System.Net.Sockets.TcpClient()
  4.  
  5. Dim Bytes2Write As [Byte]() = Encoding.ASCII.GetBytes("+++")
  6.  
  7. If (pstMaxTries = 0) Then
  8. pstMaxTries = 1
  9. End If
  10.  
  11. Dim tries As Integer = 0
  12.  
  13. For tries = 0 To pstMaxTries
  14. Try
  15. tcpClient.Connect(pstIpAddress, pstPortNo)
  16. Dim nstream As NetworkStream = tcpClient.GetStream()
  17. fstErrmsg = Write2Port(nstream, Bytes2Write, tcpClient)
  18. If (String.IsNullOrEmpty(fstErrmsg)) Then
  19. Exit For
  20. End If
  21.  
  22.  
  23. nstream.Close()
  24. Catch ex As Exception
  25. fstErrmsg = ex.Message
  26. End Try
  27. If (tries = pstMaxTries) Then
  28. 'sendemail
  29. End If
  30. Next
  31.  
  32. If (tcpClient.Connected = True) Then
  33. tcpClient.Close()
  34. End If
  35.  
  36.  
  37. End Sub
  38.  
  39. Private Function Write2Port(ByVal ns As NetworkStream, ByVal lBbyte As Byte(), ByVal tcpClient As TcpClient)
  40. Dim lstErrmsg As String = String.Empty
  41. Try
  42. If (ns.CanWrite) Then
  43. ns.Write(lBbyte, 0, lBbyte.Length)
  44.  
  45. 'Dim ReceiveByte As Byte = tcpClient.ReceiveBufferSize
  46.  
  47. Else
  48. lstErrmsg = "Error: Cannot write data to stream"
  49. End If
  50. Catch ex As Exception
  51. lstErrmsg = ex.Message
  52. End Try
  53.  
  54. Return lstErrmsg
  55. End Function
Reply With Quote Quick reply to this message  
Reply

Tags
networking, port, socket, tcp, vb.net

Message:



Similar Threads
Other Threads in the VB.NET Forum


Views: 613 | Replies: 0
Thread Tools Search this Thread



Tag cloud for networking, port, socket, tcp, vb.net
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC