frmClient.vb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. Public Class frmClient
  2. Public IsClosing As Boolean = False
  3. Public _Client As New tcpCommClient(AddressOf UpdateUI)
  4. Private Function BytesToString(ByVal data() As Byte) As String
  5. Dim enc As New System.Text.UTF8Encoding()
  6. BytesToString = enc.GetString(data)
  7. End Function
  8. Private Function StrToByteArray(ByVal text As String) As Byte()
  9. Dim encoding As New System.Text.UTF8Encoding()
  10. StrToByteArray = encoding.GetBytes(text)
  11. End Function
  12. Public Sub UpdateUI(ByVal bytes() As Byte, ByVal dataChannel As Integer)
  13. If Me.InvokeRequired() Then
  14. ' InvokeRequired: We're running on the background thread. Invoke the delegate.
  15. Me.Invoke(_Client.ClientCallbackObject, bytes, dataChannel)
  16. Else
  17. ' We're on the main UI thread now.
  18. Dim dontReport As Boolean = False
  19. If dataChannel < 251 Then
  20. Me.ListBox1.Items.Add(BytesToString(bytes))
  21. ElseIf dataChannel = 255 Then
  22. Dim msg As String = BytesToString(bytes)
  23. Dim tmp As String = ""
  24. ' Display info about the incoming file:
  25. If msg.Length > 15 Then tmp = msg.Substring(0, 15)
  26. If tmp = "Receiving file:" Then
  27. gbGetFilePregress.Text = "Receiving: " & _Client.GetIncomingFileName
  28. dontReport = True
  29. End If
  30. ' Display info about the outgoing file:
  31. If msg.Length > 13 Then tmp = msg.Substring(0, 13)
  32. If tmp = "Sending file:" Then
  33. gbSendFileProgress.Text = "Sending: " & _Client.GetOutgoingFileName
  34. dontReport = True
  35. End If
  36. ' The file being sent to the client is complete.
  37. If msg = "->Done" Then
  38. gbGetFilePregress.Text = "File->Client: Transfer complete."
  39. btGetFile.Text = "Get File"
  40. dontReport = True
  41. End If
  42. ' The file being sent to the server is complete.
  43. If msg = "<-Done" Then
  44. gbSendFileProgress.Text = "File->Server: Transfer complete."
  45. btSendFile.Text = "Send File"
  46. dontReport = True
  47. End If
  48. ' The file transfer to the client has been aborted.
  49. If msg = "->Aborted." Then
  50. gbGetFilePregress.Text = "File->Client: Transfer aborted."
  51. dontReport = True
  52. End If
  53. ' The file transfer to the server has been aborted.
  54. If msg = "<-Aborted." Then
  55. gbSendFileProgress.Text = "File->Server: Transfer aborted."
  56. dontReport = True
  57. End If
  58. ' _Client as finished sending the bytes you put into sendBytes()
  59. If msg.Length > 4 Then tmp = msg.Substring(0, 4)
  60. If tmp = "UBS:" Then ' User Bytes Sent on channel:???.
  61. btSendText.Enabled = True
  62. dontReport = True
  63. End If
  64. ' We have an error message. Could be local, or from the server.
  65. If msg.Length > 4 Then tmp = msg.Substring(0, 5)
  66. If tmp = "ERR: " Then
  67. Dim msgParts() As String
  68. msgParts = Split(msg, ": ")
  69. MsgBox("" & msgParts(1), MsgBoxStyle.Critical, "Test Tcp Communications App")
  70. dontReport = True
  71. End If
  72. ' Display all other messages in the status strip.
  73. If Not dontReport Then Me.ToolStripStatusLabel1.Text = BytesToString(bytes)
  74. End If
  75. End If
  76. End Sub
  77. Private Sub frmClient_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  78. IsClosing = True
  79. _Client.StopRunning()
  80. While _Client.isClientRunning
  81. Application.DoEvents()
  82. End While
  83. End Sub
  84. Private Sub frmClient_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  85. IsClosing = True
  86. _Client.StopRunning()
  87. While _Client.isClientRunning
  88. Application.DoEvents()
  89. End While
  90. End Sub
  91. Private Sub frmClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  92. TextBox2.Text = _Client.GetLocalIpAddress.ToString
  93. _Client.SetReceivedFilesFolder(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\ClientReceivedFiles")
  94. tmrPoll.Start()
  95. End Sub
  96. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  97. If Me.Button2.Text = "Connect" Then
  98. Dim s As String
  99. s = Me.TextBox3.Text.Trim
  100. _Client.Connect(Me.TextBox2.Text.Trim, Convert.ToInt32(s))
  101. Me.Button2.Text = "Disconnect"
  102. Else
  103. _Client.StopRunning()
  104. Me.Button2.Text = "Connect"
  105. End If
  106. End Sub
  107. Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btGetFileBrowse.Click
  108. Dim ofd As New OpenFileDialog
  109. Dim _path As String
  110. ofd.ShowDialog()
  111. _path = ofd.FileName
  112. tbGetFileReq.Text = _path
  113. End Sub
  114. Private Sub btGetFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btGetFile.Click
  115. If btGetFile.Text = "Get File" Then
  116. If tbGetFileReq.Text.Trim <> "" Then
  117. If _Client.isClientRunning Then _Client.GetFile(tbGetFileReq.Text.Trim)
  118. btGetFile.Text = "Cancel"
  119. End If
  120. Else
  121. _Client.CancelIncomingFileTransfer()
  122. btGetFile.Text = "Get File"
  123. End If
  124. End Sub
  125. Private Sub tmrPoll_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrPoll.Tick
  126. ' Update progress bars
  127. pbIncomingFile.Value = _Client.GetPercentOfFileReceived
  128. pbOutgoingFile.Value = _Client.GetPercentOfFileSent
  129. ' Update Mbps
  130. Me.Text = "Test Client (" & _Client.GetMbps & ")"
  131. End Sub
  132. Private Sub btSendFileBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSendFileBrowse.Click
  133. Dim ofd As New OpenFileDialog
  134. ofd.ShowDialog()
  135. tbSendFile.Text = ofd.FileName
  136. End Sub
  137. Private Sub btSendFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSendFile.Click
  138. If btSendFile.Text = "Send File" Then
  139. If tbSendFile.Text.Trim <> "" Then
  140. If _Client.isClientRunning Then _Client.SendFile(tbSendFile.Text.Trim)
  141. btSendFile.Text = "Cancel"
  142. End If
  143. Else
  144. _Client.CancelOutgoingFileTransfer()
  145. btSendFile.Text = "Send File"
  146. End If
  147. End Sub
  148. Private Sub btSendText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSendText.Click
  149. If Me.tbSendText.Text.Trim.Length > 0 Then
  150. _Client.SendBytes(StrToByteArray(Me.tbSendText.Text.Trim))
  151. btSendText.Enabled = False
  152. End If
  153. End Sub
  154. Private Sub gbTextIn_Enter(sender As Object, e As EventArgs) Handles gbTextIn.Enter
  155. End Sub
  156. Private Sub gbGetFile_Enter(sender As Object, e As EventArgs) Handles gbGetFile.Enter
  157. End Sub
  158. End Class