Vb.net application connect to multiple server via TCP/IP - vb.net

I made a Winforms application to work with 4 different machines by connecting with them via TCP/IP. Somehow, the connection sometimes seems disconnected and reconnecting after a short while. May I know is it I used too much TCP client and caused them congested??
Below is the function/method to connect those machine...with 4 of them different function names to connect each of the machine, but the code is more or less the same:
Public Async Sub connect_Machine_Ethernet(ByVal mainForm As Form1)
If Machine_COMPort.IsOpen Then
Machine_COMPort.Close()
End If
If (IsNothing(Machine_client)) Then
'do nothing, since obj is not created
Else
Try
Machine_client.GetStream.Close()
Machine_client.Close()
Catch exp As Exception
End Try
End If
Try
Machine_client = New TcpClient
Machine_client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, True)
Dim result = Machine_client.BeginConnect(Machine_ModuleIP_txt.Text, CInt(Machine_ModulePort_txt.Text), Nothing, Nothing)
result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1))
Machine_client.GetStream.BeginRead(Machine_ethernet_buffer, 0, Machine_ethernet_buffer.Length, AddressOf Machine_TCP_read, Machine_ethernet_buffer)
DisplayMsg("Machine Ethernet connection established")
manual_connection_LED.StateIndex = 3
Machine_client_isConnected = True
Machine_TCP_Reconnect_btn.Invoke(Sub() Machine_TCP_Reconnect_btn.Visible = False)
Catch ex As Exception
DisplayMsg("Error : Unable to connect to the Machine Ethernet connection")
manual_connection_LED.StateIndex = 0
Machine_client_isConnected = False
If (IsNothing(Machine_client)) Then
'do nothing, since obj is not created
Else
Try
Machine_client.GetStream.Close()
Machine_client.Close()
Catch exp As Exception
End Try
End If
End Try
End Sub
'Read Machine TCP message
Sub Machine_TCP_read(ByVal ar As IAsyncResult)
Try
Dim buffer() As Byte = ar.AsyncState
Dim bytesRead As Integer = Machine_client.GetStream.EndRead(ar)
Dim Message As String = System.Text.Encoding.ASCII.GetString(Machine_ethernet_buffer, 0, bytesRead)
If Message = "" Then
'----check connection
If Machine_client.Connected Then
Machine_client.Close()
connect_Machine_Ethernet(Me)
End If
Else
DisplayMsg("Input Received from machine : " & Message)
Process_machine_Feedback(Message) 'perform any data logic from the message
Machine_client.GetStream.BeginRead(Machine_ethernet_buffer, 0, Machine_ethernet_buffer.Length, AddressOf Machine_TCP_read, Machine_ethernet_buffer)
End If
Catch ex As Exception
DisplaySystemMsg(ex.Message)
DisplayMsg("Marking machine Ethernet disconnected from the server")
manual_connection_LED.StateIndex = 0
Machine_client_isConnected = False
Exit Sub
End Try
End Sub
'Send message to TCP
Public Sub Machine_TCP_send(ByVal str As String)
Try
sWriter = New StreamWriter(Machine_client.GetStream)
sWriter.WriteLine(Chr(2) & str & Chr(3)) 'add prefix suffix
sWriter.Flush()
DisplayMsg("Message send to the machine via TCP: " & str)
Catch ex As Exception
DisplayMsg("Error : Message failed to send to themachine!")
End Try
End Sub

Related

errors communicating with MT Scale over TCP/IP - Getting close

I am using VB.net 2015 to try to communicate with a scale. I have somethings working where i can talk to scale a bit but i get random vb.net errors. Also most of the time I send the command and i get and error back from the scale so i send it again and the scale responds correctly and then i get a second response of an error.
It feels like the message i am trying to send is not getting there in one piece so i have to send it twice. Any help is great appreciated. i have so many hours into trying to make it work i am going blind.
Still learning and this is my first project using TCPClient and NetworkStream
Imports System.Net.Sockets
Public Class Form1
'Private ClientWritter As TCPControlWritter
'Public Client As TcpClient
Private port As Int32 = 1701
Private client As TcpClient
Dim stream As Net.Sockets.NetworkStream
'opens connection
Sub OpenConnection(ByVal server As [String])
Try
client = New TcpClient(server, port)
client.ReceiveTimeout = 200
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
'closes connection
Sub CloseConnection()
Try
If stream IsNot Nothing Then
stream.Close()
stream = Nothing
End If
client.Close()
client = Nothing
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
'takes the text from textbox and sends it to scale
Sub SendMesssage(ByVal message As [String])
Try
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message & vbCrLf)
' Get a client stream for reading and writing.
If stream Is Nothing Then stream = client.GetStream()
' Send the message to the connected TcpServer.
If stream.CanWrite Then
stream.Write(data, 0, data.Length)
stream.Flush()
End If
Debug.WriteLine("Sent: " & message)
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As String = ""
If stream.CanRead Then
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Debug.WriteLine(responseData)
End If
Catch e As ArgumentNullException
Debug.WriteLine("ArgumentNullException: {0}", e)
Catch e As SocketException
Debug.WriteLine("SocketException: {0}", e)
End Try
txtMessage.Text = ""
txtMessage.Focus()
End Sub
'here is the data i get back from the scale. The "sent" lines are my input.
53 Ready for user
>
Sent: user admin
83 Command not recognized
>
83 Command not recognized
>
Sent: user admin
12 Access OK
>
83 Command not recognized
>

SSIS Script Task supress onerror

I have a script task which downloads a file using a HTTP connection object. This script task is part of a package which is called by another package. Sometimes the connection cannot be established. In these instances I want to retry the connection a number of times before finally raising an error if the connection attempts fail.
I tried to implement this. It appeared to work and the task does not fail. However an OnError event is still propagated every time an exception happens in the script task even though the script task doesn't fail. The fail occurs once control is passed from the child package back to the parent package.
Public Sub Main()
Dim tryTimes As Integer = 0
Dim maxTimes As Integer = 4
While (True)
Try
Dim nativeObject As Object = Dts.Connections("HTTP Connection Manager").AcquireConnection(Nothing)
'Create a new HTTP client connection
Dim connection As New HttpClientConnection(nativeObject)
Dim filename As String = Dts.Variables("Working_File").Value
connection.DownloadFile(filename, True)
Dts.TaskResult = ScriptResults.Success
Exit While
Catch ex As Exception
If (tryTimes < maxTimes) Then
tryTimes = tryTimes + 1
Thread.Sleep(30000)
Else
MsgBox(ex.Message)
Dts.TaskResult = ScriptResults.Failure
Throw
End If
End Try
End While
End Sub
I am hoping to get a solution where the OnError event is not called unless the connection attempts fails a certain number of times.
Try writing the same code an Fire a Warning on first 4 trial and on the 5th trial fire an error, i am not sure if it will works:
Public Sub Main()
Dim tryTimes As Integer = 0
Dim maxTimes As Integer = 4
While (True)
Try
Dim nativeObject As Object = Dts.Connections("HTTP Connection Manager").AcquireConnection(Nothing)
'Create a new HTTP client connection
Dim connection As New HttpClientConnection(nativeObject)
Dim filename As String = Dts.Variables("Working_File").Value
connection.DownloadFile(filename, True)
Dts.TaskResult = ScriptResults.Success
Exit While
Catch ex As Exception
If (tryTimes < maxTimes) Then
tryTimes = tryTimes + 1
Dts.Events.FireWarning(0, "Error ignored", _
"Retrying in 30 seconds", String.Empty, 0)
Thread.Sleep(30000)
Else
Dts.Events.FireError(-1, "", "Error message: " & ex2.ToString(), "", 0)
Dts.TaskResult = ScriptResults.Failure
End If
End Try
End While
End Sub
Reference
How to suppress OnError event for a specific error in a Script task (SSIS 2005)
You'll want to use a label, outside the try, and a GoTo within your catch
Public Sub Main()
Dim tryTimes As Integer = 0
Dim maxTimes As Integer = 4
RunCode: 'Label here
While (True)
Try
'your code here
Exit While
Catch ex As Exception
If (tryTimes < maxTimes) Then
tryTimes = tryTimes + 1
Thread.Sleep(30000)
GoTo RunCode 'after we catch the exception and eval tryTimes go back and retry
Else
'MsgBox(ex.Message)
Dts.Events.FireError(-1, "", "Error message: " & ex.ToString(), "", 0)
Dts.TaskResult = ScriptResults.Failure
'Throw
End If
End Try
End While
End Sub

Closing my application After Exception shows using try,catch method VB.NET

I have function that check internet connection before my application continue working, using ping sometimes my connection are limited and that make my program crash. i want to close my application when it happen. can you help me ?
this is my sample code
If My.Computer.Network.IsAvailable Then
Try
Dim pingreq As Ping = New Ping()
Dim pinging As PingReply = pingreq.Send("www.google.com")
Dim latency As Integer = pinging.RoundtripTime
Dim status = pinging.ToString
Catch err As PingException
write_log(Date.Now.ToString("dd:MM:yyyy - HH:mm:ss") & "||" & "Connection Error" & err.ToString() & err.Message)
If Not err Is Nothing Then
Timer1.Stop()
Me.Close()
constat = 0
End If
End Try
Else
Timer1.Stop()
Me.Close()
End If

Udpclient sometimes throws exception

I'm currently working on a UDP communication PC <-> ARM LM3S6965 (Luminary) through the Ethernet. On the PC there is a VB.net application that simulates a UDP server/client.
the message is broadcasted from Vb.net application to ARM LM3S6965 based device, and in return response is sent from ARM Device to vb.net application.
but sometimes receive UdpClient throws socket exception i.e. Only one usage of each socket address (protocol/network address/port) is normally permitted.
the line which throws exception is
udpReceivingClient = New UdpClient(mnPort)
Complete VB.net Code is as follows
Module mod_Search_UDP
Public mnPort As Int32 = 3040 'Port number to send/recieve data on
'Public Const msBroadcastAddress As String = "255.255.255.255" 'Sends data to all LOCAL listening clients, to send data over WAN you'll need to enter a public (external) IP address of the other client
'Dim endPoint As IPEndPoint = New IPEndPoint(msBroadcastAddress, mnPort)
Public udpReceivingClient As UdpClient 'Client for handling incoming data
Public udpSendingClient As UdpClient 'Client for sending data
Public receivingThread As Thread 'Create a separate thread to listen for incoming data, helps to prevent the form from freezing up
Public mbiClosing As Boolean = False 'Used to close clients if form is closing
Public mbiCloseRxClient As Boolean = False
Public Sub InitializeSender()
Dim soc As Socket
Dim lsPort As String
Dim lnPort As Int32 = 3040
Const lsBroadcastAdd As String = "255.255.255.255"
'Dim endPoint As IPEndPoint = New IPEndPoint(msBroadcastAddress, mnPort)
'udpSendingClient = New UdpClient(endPoint)
udpSendingClient = New UdpClient(lsBroadcastAdd, lnPort)
udpSendingClient.EnableBroadcast = True
soc = udpSendingClient.Client
lsPort = (CType(soc.LocalEndPoint, IPEndPoint).Port.ToString())
mnPort = Convert.ToInt32(lsPort)
End Sub
Public Sub InitializeReceiver()
'Create UdpClient class and bind it to the local port number provided
'Try
udpReceivingClient = New UdpClient(mnPort)
'udpReceivingClient.EnableBroadcast = True
mbiCloseRxClient = True
'Catch ex As Exception
' MsgBox(ex.ToString)
'End Try
Dim start As ThreadStart = New ThreadStart(AddressOf MT_Receiver)
receivingThread = New Thread(start)
receivingThread.IsBackground = True
receivingThread.Start()
End Sub
Public Sub MT_Receiver()
Dim endPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, mnPort) 'Listen for incoming data from any IP address on the specified port
Dim lbData() As Byte 'Buffer for storing incoming bytes
Dim llRet As UInt16
'udpListen.Poll(1000, Net.Sockets.SelectMode.SelectRead)
While (True) 'Setup an infinite loop
If mbiClosing = True Then 'Exit sub if form is closing
Exit Sub
End If
llRet = udpReceivingClient.Available
If llRet > 0 Then
lbData = udpReceivingClient.Receive(endPoint) 'Receive incoming bytes
'If udpListen.Available Then
' udpListen.Receive(lbData, 256, Net.Sockets.SocketFlags.None)
'End If
'If lbData Is Nothing Then
'Else
frmSearchUDP.MT_Validate_Msg(lbData)
End If
End While
End Sub
Public Sub MT_Send_UDP(ByVal lbTxBuffer() As Byte)
InitializeSender()
If mbiCloseRxClient = True Then
receivingThread.Abort()
udpReceivingClient.Client.Dispose()
udpReceivingClient.Close()
End If
Try
udpSendingClient.Send(lbTxBuffer, lbTxBuffer.Length)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
udpSendingClient.Client.Dispose()
udpSendingClient.Close()
InitializeReceiver()
'Try
' udpReceivingClient.BeginReceive(AddressOf MT_RX_Callback, Nothing)
'Catch ex As Exception
' MsgBox(ex.ToString)
'End Try
End Sub
End Module
Whats mistake is there in code ? how can i use same port for receive and transmit ?

Timeoutexception on TCP not handled

I took this code from a website since VS 2010 doesn't support the timeout for the TCP connections:
Private Function ConnectWithTimeout() As Boolean
Dim ar As IAsyncResult = TCPClient.BeginConnect(IPAddress, TCPPort, Nothing, Nothing)
Dim wh As System.Threading.WaitHandle = ar.AsyncWaitHandle
Try
If Not ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(2), False) Then
TCPClient.Close()
TCPClient = New System.Net.Sockets.TcpClient
Throw New TimeoutException()
End If
Catch ex As Exception
ThrowError("Timeout on connecting to " & IPAddress & " at port " & TCPPort & ".")
Return False
Finally
wh.Close()
End Try
Return True
End Function
And it works fine, but everytime, it gives me this on the debug output:
"A first chance exception of type 'System.TimeoutException' occurred in"
Even if I'm catching all the exceptions. Is there a way to get rid of this exception message as it is handled?
I've tried this:
Dim connectDone As New System.Threading.AutoResetEvent(False)
TCPClient.BeginConnect(IPAddress, TCPPort, New AsyncCallback(Sub(ar As IAsyncResult)
TCPClient.EndConnect(ar)
connectDone.Set()
End Sub), TCPClient)
'client.BeginConnect("127.0.0.1", 80, new AsyncCallback(delegate( IAsyncResult ar ) { client.EndConnect( ar ); connectDone.Set(); }), client);
If Not connectDone.WaitOne(2000) Then
Debug.WriteLine("TIMEOUT")
Return False
End If
Return True
But it gives me InvalidOperationException on the beginconnect line:
BeginConnect cannot be called while another asynchronous operation is in progress on the same Socket.
Private Function ConnectWithTimeout() As Boolean
Dim ar As IAsyncResult
Dim wh As System.Threading.WaitHandle
Try
ar = TCPClient.BeginConnect(IPAddress, TCPPort, Nothing, Nothing)
wh = ar.AsyncWaitHandle
Cath ex as Exception
'Code to catch exception
End Try
Try
If Not ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(2), False) Then
TCPClient.Close()
TCPClient = New System.Net.Sockets.TcpClient
Throw New TimeoutException()
End If
Catch ex As Exception
ThrowError("Timeout on connecting to " & IPAddress & " at port " & TCPPort & ".")
Return False
Finally
wh.Close()
End Try
Return True
End Function