Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Windows Mobile Developer Controls

eVB, WinSock - binary data getting you down?

Written by Pete Vickers  [author's bio]  [read 49369 times]
Edited by Derek

Download the code

Page 1  Page 2  Page 3 

Put the code to connect to your server behind the cmdConnect button.

Private Sub cmdConnect_Click()
If winsockTerm.State = sckClosed Then
winsockTerm.RemoteHost = txtIp.Text
winsockTerm.RemotePort = CInt(txtport)
Me.Enabled = False
WaitCursor True
winsockTerm.Connect
End If
End Sub

When the winsockterm has made a connection to your server, the connect event will fire. At this point, disable the connect button, and enable the disconnect button, and you are ready to go:

Private Sub winsockTerm_Connect()
cmdConnect.Enabled = False
cmdDisconnect.Enabled = True
WaitCursor False
Me.Enabled = True
txtTerminal.SetFocus
End Sub

Click on the 'Return' key on your keyboard, and you should get a response from the server. The text box txtTerminal acts as the 'terminal window', and will only send data to the server on receipt of a carriage return character. The string idata buffers the characters until a carriage return character is received, and then sends the string with winsockter.senddata idata

Private Sub txtTerminal_KeyPress _
(ByVal KeyAscii As Integer)

Last_Key = Chr(KeyAscii)
If KeyAscii <> 13 Then
idata = idata & Chr(KeyAscii)
Else
idata = idata & vbCrLf
winsockTerm.SendData idata
End If
End Sub

This is now the start of all our problems. The server is going to send us some data back, and we are going to have to deal with it somehow. When the data arrives, the dataarrival event fires, and tells us how many bytes of data we are getting back.

We get the data by winsockterm.getdata bdata, vbbyte+vbarray. This will bring us the data, and put it in our declared byte array bdata.

We now need to get our data from the byte array into a string. This is done by a function called Translate_Buffer, which simply goes byte by byte through the array translating it using the chr function.

We do a bit of other validation, replacing vbcrlf, and ignoring some characters that would screw us up, and then we display the data using the ShowData sub.

The code for the data arrival and translation is:

Private Sub winsockterm_DataArrival _
(ByVal bytesTotal As Long)

winsockTerm.GetData bData, vbByte + vbArray
Rdata = Translate_Buffer(bytesTotal)
If InStr(Rdata, Chr(255)) <> 0 And _
InStr(Rdata, Chr(1)) <> 0 Then
Exit Sub
End If


If Rdata = vbCrLf Then
Rdata = Replace(Rdata, vbLf, "")
End If

If Rdata <> Last_Key Then
If Rdata <> idata Then
ShowData txtTerminal, Rdata
If Me.Enabled = False Then
Screen.MousePointer = vbDefault
Me.Enabled = True
End If
End If
End If
End Sub

Private Function Translate_Buffer _
(Nchars As Integer) As String

Translate_Buffer = ""

For ict = 0 To Nchars - 1
Translate_Buffer = Translate_Buffer _
& Chr(bData(ict))
Next

End Function

Simple isn't it. The ShowData subroutine is the most complex code in the project. It deals with ensuring our text box doesn't exceed the 32k limit, handles backspace characters and carriage return/line feed combinations, and displays our data.

Previous Page  Next Page