"KeepAlive" for TCPClient connection to keep connection open? - vb.net

Is there a way to keep a TcpClient connection open constantly? I have an application which lets our users scan a carton, some database updates are performed, and a shipping label is sent to and printed from a wireless hip printer (model of printer is Zebra QLn420) which the user is using.
The application attempts to keep a connection to the wireless printer via the TcpClient connection, and multiple checks are made throughout processing to make sure the connection is good, before sending a generated ZPL to the printer for printing.
We have been having an issue with an occasional label missing, and it seems to be whenever the user stops scanning for a few minutes, then resumes. However, it is a semi-rare occurrence when a label is skipped, and as such is rather hard to reproduce (I haven't been able to replicate it myself, but I have seen it happen out in the warehouse).
I would like to know either if there is a way to make sure that the connection is always open (by "pinging" the device every so often), or if there is a way to get feedback that the data has been received and printed.
This is the code I'm calling to ensure a connection:
Public Function Connect(strIP As String, intPort As Integer) As Boolean
Try
'connect to printer via TcpClient, need ip address and port number
'connects without thread, hangs program for 10-20 seconds if printer is not turned on, replaced with code below to thread the connection and set timeout
For i As Integer = 1 To 2
If Not (client IsNot Nothing AndAlso client.Connected) Then
'uses ClientSocketParameters structure to pass to recursive function ConnectionReturned()
clntSockParams = New ClientSocketParameters
clntSockParams.addrs = strIP
clntSockParams.prt = intPort
'create client and call BeginConnect (attempts to connect on separate thread until TimeoutTime has elapsed)
client = New System.Net.Sockets.TcpClient
client.SendTimeout = 5000
client.ReceiveTimeout = 5000
'setup timer with timeout length and start, if timer goes past intTimeoutLength, the Timeout() function is called which closes everything and leaves client = Nothing
AddHandler TimeoutTime.Elapsed, AddressOf Timeout
TimeoutTime.Interval = intTimeoutLength
TimeoutTime.Start()
client.BeginConnect(strIP, intPort, New AsyncCallback(AddressOf ConnectionReturned), clntSockParams)
'keeps the program from doing anything else until BeginConnect either succeeds or fails (due to connect on separate thread)
Do While TimeoutTime.Enabled
System.Threading.Thread.Sleep(500)
Loop
End If
'if TimeoutTime is elapsed and client is Nothing, connection didn't happen, throw an error
If client Is Nothing Then
blnConnected = False
Else
blnConnected = True
Exit For
End If
Next
Catch ex As Exception
blnConnected = False
End Try
Return blnConnected
End Function
Private Sub ConnectionReturned(ByVal ar As System.IAsyncResult)
'this method is called from the client.BeginConnect line in Connect(), make sure timer is running
If TimeoutTime.Enabled Then
'ensure client is initialized
If client Is Nothing Then client = New System.Net.Sockets.TcpClient
'keep calling ConnectionReturned until client.Connected is true
If client.Connected Then
TimeoutTime.Stop()
Else
Dim actualParameters As ClientSocketParameters = DirectCast(ar.AsyncState, ClientSocketParameters)
client.BeginConnect(actualParameters.addrs, actualParameters.prt, New AsyncCallback(AddressOf ConnectionReturned), clntSockParams)
End If
End If
End Sub
Private Sub Timeout(ByVal sender As Object, ByVal e As EventArgs)
'this method is only called if TimeoutTime elapsed, which means no connection was made. close the client object if needed, set to Nothing, and stop TimeoutTime
If TimeoutTime.Enabled Then
Try
client.Close()
Catch ex As Exception
End Try
client = Nothing
TimeoutTime.Stop()
End If
End Sub
According to this question:
tcp client in vb.net not receiving the entire data response data from server
TcpClient is not always guaranteed to deliver all data to the other end of a connection, so if a more reliable connection method is available, that would be worth a try as well.
Please let me know if more information is needed. Thank you!
Originally I got the code for connecting via this link. I've modified it since because it would hang the application for 10-20 seconds if it took longer to connect. Code here is in C# and I translated to VB:
Send ZPL Commands via TCP/IP in C#
This is the link to the docs for the class:
TcpClient Class

Zebra printers have a timeout setting on TCP that I think has a 3 or 5 minute default. The first thing to do is to turn that timeout off. There will still be other reasons that the printer would disconnect so you will need to handle that as well.
embed this into your program:
! U1 setvar "wlan.ip.timeout.enable" "off"
Make sure you send a CR/LF before and after that line.
if you send a query after your format you can know that the whole format made it to the printer. Something like the following would work:
! U1 getvar "device.uptime"

That's not a sane solution. A sane solution is this: If a label isn't completely sent to the printer because the connection failed, make a new connection and send the label.

Related

Asynchronous tcp client dilemma

I am trying to write an asynchronous TCP client with VB.NET to communicate with a thermal printer (Intermec Px4e), but I am facing an issue regarding the way I receive the data from it.
Previously I was using a synchronous client, but I had the problem regarding the response not coming instantly from the printer, so after sending data and then checking for available bytes I was able to read only the first bytes of the response.
This is the synchronous client I am actually using:
Private Function sendBytesEthernet(ByVal data As Byte(), ByVal withResponse As Boolean) As PrinterPacket(Of Byte())
Dim socketStatus As Net.Sockets.SocketError
Dim response As New PrinterPacket(Of Byte()) With {.StatusCode = CommandStatusCode.ERR, _
.Response = String.Empty, _
.Data = data}
Try
If Me.oggettoSocket.Client.Poll(2000, Sockets.SelectMode.SelectWrite) Then
Me.oggettoSocket.Client.Send(data, 0, data.Length, 0, socketStatus)
Else
Throw New Exception(socketStatus.ToString())
End If
If withResponse Then
Dim bytesAvailable As UInteger = Me.oggettoSocket.Available
If bytesAvailable > 0 Then
Dim dataBuffer(bytesAvailable - 1) As Byte
Dim bytesReceived As UInteger = Me.oggettoSocket.Client.Receive(dataBuffer, 0, dataBuffer.Length, Sockets.SocketFlags.None, socketStatus)
If socketStatus = Sockets.SocketError.Success Then
If bytesReceived > 0 Then
response.Response = System.Text.Encoding.UTF8.GetString(dataBuffer)
response.StatusCode = CommandStatusCode.OK
Return response
End If
Else
Throw New Exception(socketStatus.ToString())
End If
End If
End If
If socketStatus = Sockets.SocketError.Success Then
response.StatusCode = CommandStatusCode.OK
Else
Throw New Exception(socketStatus.ToString())
End If
Catch ex As Exception
Select Case ex.Message
Case "ConnectionReset"
Me.oggettoSocket.Client.Disconnect(True)
Me.openSocket()
End Select
response.Response = ex.Message
End Try
Return response
End Function
What happens is that If send a command to the printer that involves a big response from it, I receive the full response splitted in several packets, so with this function I am only able to read the first packet.
For example:
I send to the printer the command to list all the fonts available (FONTS[CRLF])
The printer responds, but it sends the first 54 bytes and then truncates the list.
The printer sends the second packet, this time 1560 bytes long, but it still truncates the list.
Lastly the printer completes the list with last 25 bytes.
Basically with my function I am only able to read the first 54 bytes, unless I don't use a loop to check the availability on the receive buffer in conjunction with a Thread.Sleep() call, since the client is synchronous and I have to wait for the printer to send every packet, but I find this method really inefficient.
At this point I concluded that I had to move to an asynchronous approach, due to the nature of the TCP protocol. I copied an example of async client on the MSDN and it works, but it doesn't fit my use case. Why?
Because I actually have a thread that constantly communicate with the printer through the socket, and I open the connection when the thread starts and close it when the thread ends. If use, for example, the BeginReceive callback used in the MSDN example, that call will never end until I close the connection client side. This cannot take place, since I keep the connection open to the printer and I can't establish a new TCP connection for every thread cycle (that would be a waste in terms of network traffic).
I hope I have explained the problem well.

VB Socket Exception Issue - Ports

Creating a program that can pass built strings between two computers.
After digging and changing the syntax, I was finally able to see this socket exception:
System.Net.Sockets.SocketException (0x80004005): Only one usage of each socket address (protocol/network address/port) is normally permitted
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at System.Net.Sockets.TcpListener.Start()
at ChatProgFinal.chatForm.serverMode()
The port that I'm using is 11337. According to netstat, I'm not using that port, so I'm not certain how else I could be throwing this exception.
Here is my serverMode block:
Public Sub serverMode()
Try
Dim server As TcpListener = Nothing
server = New TcpListener(LocalIPEndPointAnyIP)
server.Start()
Dim incomingClient As TcpClient = server.AcceptTcpClient()
Do While isAlive = True And incomingClient.Connected = True
stream = incomingClient.GetStream()
If stream.CanRead Then
RaiseEvent DataReceived(stream)
End If
If incomingClient.Connected = False Then
RaiseEvent statusCheck("Disconnected")
isAlive = False
server.Stop()
End If
Loop
Catch e As Exception
RaiseEvent statusCheck(e.ToString)
Using outfile As New StreamWriter(docPath + "\log.txt")
outfile.Write(e.ToString)
End Using
Finally
stream.Close()
End Try
End Sub
Thank you for any advice you can provide on this.
Side question: I am not sure how to test/debug software that communicates between two computers through Visual Studio 2010. What I'm doing right now is publish it and deploy to a test VM. Anyone have any other methods I could use?
Along the lines of Hans's comment, you are probably calling serverMode more than once through the life of your program. The one thing I noticed is that in your finally block you are not calling .Stop on your server, which means its possible to exit serverMode without shutting it down. Once that happens, repeated calls to start the server will likely fail.

How do I check if an ftp server is online and get the error that it generates if it is not connected?

I am new to programming in vb.net. I have come a long ways in my development and understanding of vb, but there is one hurtle I can not seem to fix. I am hosting an ftp server on my pc and I am making an app for it to connect to my server and download files. The problem with all the sample code is that everyone ASSUMES the server WILL be ONLINE. My pc may not be running 24/7 and I also may not have the ftp service running.In the first case it shouldnt even register that it is connected. In the second case, it WILL say that is connected b/c the pc is on, but it will return that the machine ou are trying to connect to is actively refusing the connection. Is there a way to TRULY check if the program is indeed connected to the server WITHOUT generating a bunch of Exceptions in the debugger? All I want is a call like:
Dim ftponline As Boolean = False 'Set default to false
ftponline = checkftp()
If ftponline Then
'continue program
Else
'try a different server
End If
So it would be a function called checkftp that returns a boolean value of true or false.
Here is my info:
Using Visual Studio 2010 Pro
Using .Net framework 4
Can anyone help?
Thanks!
I have tried the rebex ftp pack as well as the Ultimate FTP Pack.
Here is the updated code:
Public Function CheckConnection(address As String) As Boolean
Dim logonServer As New System.Net.Sockets.TcpClient()
Try
logonServer.Connect(address, 21)
Catch generatedExceptionName As Exception
MessageBox.Show("Failed to connect to: " & address)
End Try
If logonServer.Connected Then
MessageBox.Show("Connected to: " & address)
Return True
logonServer.Close()
Else
Return False
End If
End Function
Public Sub ConnectFtp()
types.Clear()
models.Clear()
ListBox1.Items.Clear()
ListBox2.Items.Clear()
TextBox2.Clear()
Dim request As New Rebex.Net.Ftp
If CheckConnection(*) Then
Dim tempString As String()
request.Connect(*)
request.Login(*, *)
request.ChangeDirectory("/atc3/HD_Models")
Dim list As Array
list = request.GetNameList()
Dim item As String = ""
For Each item In list
tempString = item.Split(New Char() {" "c})
If types.Contains(tempString(0)) = False Then
types.Add(tempString(0))
End If
If models.Contains(item) = False Then
models.Add(item)
End If
Next
request.Disconnect()
request.Dispose()
ElseIf CheckConnection(*) Then
request.Connect(*)
request.Login(*, *)
request.ChangeDirectory(*)
Dim list2 As Array
list2 = request.GetNameList()
Dim item2 As String = ""
Dim tempString2 As String()
For Each item2 In list2
MessageBox.Show(item2)
tempString2 = item2.Split(New Char() {" "c})
If types.Contains(tempString2(0)) = False Then
types.Add(tempString2(0))
End If
If models.Contains(item2) = False Then
models.Add(item2)
End If
Next
request.Disconnect()
request.Dispose()
End If
End Sub
No matter what I do, the second server will not connect. I even put a messagebox to show what items were being returned in the second server, but there are no messageboxes apearing when I run the program with my server offline. Is there anyone who can help?
If your code is designed with proper exception catching, it shouldn't be generating a "bunch" of exceptions. The first exception you catch should be your indication that the connection failed and your code should cease attempting to communicate at that point. If for some reason you really need to check the connectivity before attempting the FTP connection, you should be able to simply attempt to synchronously open a TCP socket to the FTP server's port. If that works, it's up and running.
You could simply open a socket to the server's IP address on Port 21 (assuming default FTP port).
I'm not much of a VB.Net programmer, but here's a link to sample code:
http://vb.net-informations.com/communications/vb.net_Client_Socket.htm
If you can establish the socket connection, you know that something is listening on that port (though you have not yet proven it's an FTP server, or that it will accept your login credentials...).
If you wish to simply avoid exceptions in the debugger, you could place the connection code in a method and apply the DebuggerHidden attribute to that method.

VB 2010 Kill thread by name

In my multi-threading server app, a thread per client is generated in this way:
While (True)
counter += 1
clientSocket = serverSocket.AcceptTcpClient()
log.debug("Client No: " + Convert.ToString(counter) + " started!")
Dim client As New handleClinet
clients.Add(clientSocket)
client.startClient(clientSocket, Convert.ToString(counter))
End While
and in handleClinet:
Public Sub startClient(ByVal inClientSocket As TcpClient, ByVal clineNo As String)
Me.clientSocket = inClientSocket
Me.clNo = clineNo
Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
ctThread.Name = "client" + clineNo
ctThread.Start()
End Sub
As you can see, the thread is made with the name client1, client2, etc. When a user signs out, a command is send to the server along with the clientID. But how can I kill the specific client process?
Something like:
Public Sub killClient(ByVal clientID As Int32)
'Code to kill "client"+clientID
End Sub
Threads are just execution vehicles. Any time you think you need to push a thread around, think instead about what the thread is doing. When you don't need your doctor anymore, you don't kill him. You leave his office.
If you want to shut down the client cleanly, set a 'shutdown' flag in the client structure. Have the code that handles the client check this flag periodically. Forget about what thread is doing it, it's the client you want to shut down.
If that's too passive, shutdown the TCP connection to the client. That will cause any code that touches the client's connection to get an error.
Forget about the fact that it happens to be a dedicated thread that's handling the client. You wrote the code. Code it so that it stops handling the client when that's no longer appropriate by checking if it's appropriate to handle a client before handling it.

Don't get any data from socket when data should be there, and no exceptions raised, connection is open. Using DataAvailable to wait for data

I have a problem reading data from an RFID-reader. I connect to the reader by tcp and wait for DataAvailable to be true, then reading the data until I got a end of data character. Then I just go back and waiting for a new DataAvailable. This is done in a own thread for the function.
There seems to be some kind of timeout, If I don't got any data in a couple of minutes, then it just sits there in the do/loop waiting for DataAvailable. I hold the card to the RFID reader, it beeps, but there is no data available. I don't get any exceptions, and the information says that the clientsocket is still connected. Are there anything more I can check?
If I put the card to the reader in a minute-interval it seems as this will never occur. So 2-3 minutes idle:ing seems to do this.
Here are my code to read data from the socket, I have taken away some irrelevant code:
Sub test(ByVal ip As String, ByVal port As Integer)
' this sub is meant to run forever
Try
Dim clientSocket As New System.Net.Sockets.TcpClient()
clientSocket.Connect(ip, port)
Using serverStream As NetworkStream = clientSocket.GetStream()
Do 'loop forever or until error occur
'Every new dataentry starts here
Dim inStream(0) As Byte
Dim returndata As String = ""
Do 'loop forever
Do Until serverStream.DataAvailable 'loop until data exists to read
If clientSocket.Connected = False Then
'this will never happen.
'but if there are more than 5 minutes between data then
'it never got data again as if no data was sent.
Exit Sub
End If
Application.DoEvents()
Loop
'there is data to read, read first byte and
serverStream.Read(inStream, 0, 1)
If inStream(0) = 13 Then
'got end of data
'exit loop if reading chr 13.
returndata &= System.Text.Encoding.ASCII.GetString(inStream)
Exit Do
End If
Loop
GotData(returndata)
Loop
End Using
Catch ex As Exception
' handle error
Finally
'close connection if open
End Try
End Sub
I found out that socket.Connected only reports the status for when the last command was runned on the connection.
So in the Do Until serverStream.DataAvailable loop I used this trick to check if the connection was closed instead:
Dim test As Boolean = clientSocket.Client.Poll(10, System.Net.Sockets.SelectMode.SelectRead)
If test = True And serverStream.DataAvailable = False Then
'restart connection
End If
So now finally got control over what is happening and know that its because of the client connection is closed that I dont got any data.
So, then I figured, now that I know that the connection is closed, how do I prevent it? That was easier, just send data to the tcpip-server every 10 second and it will hold it open.
The result that works for me (this is not the production code, just an example of the solution):
Dim s As Date = Now
Do Until serverStream.DataAvailable
Dim r As Boolean = clientSocket.Client.Poll(10, System.Net.Sockets.SelectMode.SelectRead)
If DateDiff(DateInterval.Second, s, Now) > 10 Then
Dim dta(0) As Byte
dta(0) = 0
clientSocket.Client.Send(dta)
s = Now
End If
If r = True And serverStream.DataAvailable = False Then
'restart sub
Exit Sub
End If
loop
So now it doesnt even close and need to restart every x minutes.
My problems are solved and Im a happy coder. ;)