We have got to know the contents about sending message through TCP/IP. So, we may understand how to use Winsock to connect via TCP/IP. So now, we program in order to send via data, so, we have to firstly make understanding by dividing data file into 2 types in the following.
• Sequential File
• Random File
In this content, we mention how to manage file as Random File that has more advantage than Sequential File. It can be quickly opened by not arranging data as Sequential File and enter to Record.
Step of creating data as Random in Basic
1. begin to open file
Open “R”,#<File-number>,”<File description>”,<Record lenght>
File-number is the number of file we use.
File-description is the name of file we use.
Record length is the length of data that keep in 1 record to 32467
If not specify, will have 128 byte/1 record.
2. set the size in variable like the following,
Random Buffer —> FIELD #
3. get data and verify the last data –> Input
4. If don’t use the last data, take data to process.
5. transfer data to Random Buffer, if data is number, then change to string value before keeping at variable “Buffer”
6. record data to file then back to step 3
Put # <file number>[,<record number>]
7. close file when going to the last data.
Close #<file number>[,<record number>] or Close
The steps of reading data file as Random in Basic
1. Open file
Open “R”,#<file-number>,”<file description>”,<Record lenght>
2. Set the size in variables
Random Buffer —> FIELD #
3. Set data that will read and verify the last data by condition If…Then….Else
4. Read data in record
Get # <file number>[,<record number>]
5. Change data that is type, string to Numeric before processing.
6. Show value get from processing Print or Debug.Print and back to step 3.
7. Open file when getting the last data.
Close #<file number>[,<record number>] or Close
How to program for sending file in the server
Sending file in the Server side will write as Sub Function or write in Events of Control. I will present as Sub Function in order to be easy and convenient for using that will send value of Control to Function.
SendData(sFile As String, sSaveAs As String, tcpCtl As Winsock)
txtFiletranfer =Text box will get value as Text of Path File that will send.
txtNewFile = Text box as get value as Text of Path File that will keep in new place.
Winsock1 =sending by command SendData to Client.
After that opening file following pate that is set and manage to read data as binary for keeping at Buffer then send by command SendData. These are principle of this Function.
Public Sub SendData(sFile As String, sSaveAs As String, tcpCtl As Winsock)
On Error GoTo ErrHandler ‘ when error, do at sub-ErrHandler
Dim sSend As String, sBuf As String
Dim ifreefile As Integer
Dim lRead As Long, lLen As Long, lThisRead As Long, lLastRead As Long
ifreefile = FreeFile
‘ open file as binary
Open sFile For Binary Access Read As #ifreefile
lLen = LOF(ifreefile)
‘looping for making file as pack 64k
Do While lRead < lLen
lThisRead = 65536
If lThisRead + lRead > lLen Then
lThisRead = lLen - lRead
End If
If Not lThisRead = lLastRead Then
sBuf = Space$(lThisRead) ‘ keep Record in Buffer
End If
Get #ifreefile, , sBuf ‘ reading data from file to keep in Random Buffer according to record.
lRead = lRead + lThisRead
sSend = sSend & sBuf
Loop
valTotal = lLen
Close ifreefile ‘open data file
bSendingFile = True
’sending the name of data file and “File” to verify condition at Client
tcpCtl.SendData “File” & sSaveAs
DoEvents
’sending data of data file
tcpCtl.SendData sSend
DoEvents
‘finished
tcpCtl.SendData “Filecmp” sending string data named”Filecmp” in order to verify condition at Client
bSendingFile = False
Exit Sub
‘ in the case of error will run at this sub together with showing error message
ErrHandler:
txtMonitor.Text = txtMonitor.Text & Err.Description & ” - Error number: ” & Err.Number & vbCrLf
txtMonitor.SelStart = Len(txtMonitor.Text)
End Sub
How to program for sending in the side of Client
Client will get data that send from Server to create file that will be the new name or the old name according to set in Text Box named txtNewFile come from Server will do at Event Winsock1_DataArrival that has the principle of programming like this.
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim StrValue As String
Dim ifreefile
On Error GoTo ErrLabel:
Winsock1.GetData StrValue ‘ from message at the opposite side send will keep in variable named StrValue, afterward, will take to show at Text Box.
DoEvents
If Right$(StrValue, 7) = “Filecmp” Then ‘ if Server send string value named “Filecmp”
bFileArriving = False ‘ variable=0 in order to know finishing about sending data of file.
sArriving = sArriving & Left$(StrValue, Len(StrValue) - 7) ‘ keep data that send by using function to manage alphabet with variable StrValue and connecting variable value sArriving by keeping at variable value sArriving again.
ifreefile = FreeFile
If Dir(sFile) <> “” Then ‘ verify that having the original file
MsgBox “File Already Exists”
Else
‘open data file as binary in order to write data
Open sFile For Binary Access Write As #ifreefile
Put #ifreefile, 1, sArriving
‘ record data file from variable buffer to data file according to record.
Close #ifreefile ‘ close data file
‘ open file automatically
ShellExecute 0, vbNullString, App.Path & “\” & sFile, vbNullString, vbNullString, vbNormalFocus
End If
ElseIf Left$(StrValue, 4) = “File” Then ‘ if variable StrValue have value= File, keeping in variable
bFileArriving = True
sFile = Right$(StrValue, Len(StrValue) - 4)
ElseIf bFileArriving Then ‘ if variable bFileArriving =1, connecting data of variable
sArriving = sArriving & StrValue
End If
Exit Sub
ErrLabel:
‘ in the case of error will run to this sub together with showing error message
txtMonitor.Text = txtMonitor.Text & Err.Description & ” - Error number: ” & Err.Number & vbCrLf
txtMonitor.SelStart = Len(txtMonitor.Text)
End Sub
The learning outcome of this article
1. To know how to program by Visual Basic to communicate through protocol TCP/IP by Winsock.
2. To apply foundation about data management to use in sending file through TCP/IP.
3. To understand the principle of working of Event and Function of Winsock.