frmServer.vb 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. Public Class frmServer
  2. Private server As TcpComm.Server
  3. Private lat As TcpComm.Utilities.LargeArrayTransferHelper
  4. Public Sub UpdateUI(ByVal bytes() As Byte, ByVal sessionID As Int32, ByVal dataChannel As Byte)
  5. ' Use TcpComm.Utilities.LargeArrayTransferHelper to make it easier to send and receive
  6. ' large arrays sent via lat.SendArray()
  7. ' The LargeArrayTransferHelperb will assemble any number of incoming large arrays
  8. ' on any channel or from any sessionId, and pass them back to this callback
  9. ' when they are complete. Returns True if it has handled this incomming packet,
  10. ' so we exit the callback when it returns true.
  11. If lat.HandleIncomingBytes(bytes, dataChannel, sessionID) then Return
  12. If Me.InvokeRequired() Then
  13. ' InvokeRequired: We're running on the background thread. Invoke the delegate.
  14. Me.Invoke(server.ServerCallbackObject, bytes, sessionID, dataChannel)
  15. Else
  16. ' We're on the main UI thread now.
  17. If dataChannel < 251 Then
  18. Me.lbTextInput.Items.Add("Session " & sessionID.ToString & ": " & TcpComm.Utilities.BytesToString(bytes))
  19. ElseIf dataChannel = 255 Then
  20. Dim tmp = ""
  21. Dim msg As String = TcpComm.Utilities.BytesToString(bytes)
  22. Dim dontReport As Boolean = False
  23. ' server has finished sending the bytes you put into sendBytes()
  24. If msg.Length > 3 Then tmp = msg.Substring(0, 3)
  25. If tmp = "UBS" Then ' User Bytes Sent.
  26. Dim parts() As String = Split(msg, "UBS:")
  27. msg = "Data sent to session: " & parts(1)
  28. End If
  29. If msg = "Connected." then UpdateClientsList()
  30. If msg.Contains(" MachineID:") then UpdateClientsList()
  31. If msg.Contains("Session Stopped. (") then UpdateClientsList()
  32. If Not dontReport Then Me.ToolStripStatusLabel1.Text = msg
  33. End If
  34. End If
  35. End Sub
  36. Private Sub UpdateClientsList()
  37. Dim sessionList As List(Of TcpComm.Server.SessionCommunications) = server.GetSessionCollection()
  38. Dim lvi As ListViewItem
  39. Me.lvClients.Items.Clear
  40. For Each session As TcpComm.Server.SessionCommunications In sessionList
  41. If session.IsRunning Then
  42. lvi = New ListViewItem(" Connected", 0, lvClients.Groups.Item(0))
  43. Else
  44. lvi = New ListViewItem(" Disconnected", 1, lvClients.Groups.Item(1))
  45. End If
  46. lvi.SubItems.Add(session.sessionID.ToString())
  47. lvi.SubItems.Add(session.machineId)
  48. Me.lvClients.Items.Add(lvi)
  49. Next
  50. End Sub
  51. Private Sub frmServer_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  52. If server IsNot Nothing Then server.Close()
  53. End Sub
  54. Private Sub frmServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  55. Me.ToolStripStatusLabel1.Text = "Idle."
  56. frmClient.Show()
  57. End Sub
  58. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btStartServer.Click
  59. If btStartServer.Text = "Start Server" Then
  60. server = New TcpComm.Server(AddressOf UpdateUI,, cbUniqueIds.Checked)
  61. lat = New TcpComm.Utilities.LargeArrayTransferHelper(server)
  62. server.Start(22490)
  63. btStartServer.Text = "Stop Server"
  64. Else
  65. If server IsNot Nothing Then server.Close()
  66. lat = Nothing
  67. Me.lvClients.Items.Clear
  68. btStartServer.Text = "Start Server"
  69. End If
  70. End Sub
  71. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSendText.Click
  72. If Me.tbSendText.Text.Trim.Length > 0 Then
  73. ' Send a text message to all connected sessions on channel 1.
  74. server.SendText(Me.tbSendText.Text.Trim)
  75. End If
  76. End Sub
  77. Private Sub btStartNewClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btStartNewClient.Click
  78. Dim newClient As New frmClient
  79. Static uniqueMachineIDNumber As Int32 = 1
  80. newClient.SetMachineIDNumber(uniqueMachineIDNumber)
  81. newClient.Show()
  82. uniqueMachineIDNumber +=1
  83. End Sub
  84. Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  85. Me.lbTextInput.Items.Clear()
  86. End Sub
  87. Private Sub SendAFileToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SendAFileToolStripMenuItem.Click
  88. If lvClients.SelectedItems.Count > 0 then
  89. Dim lvi As ListViewItem = lvClients.SelectedItems.Item(0)
  90. ' Get the session using the sessionID pulled from the selected listview item
  91. Dim session As TcpComm.Server.SessionCommunications = server.GetSession(Convert.ToInt32(lvi.SubItems(1).Text))
  92. Dim message As String
  93. Dim fileName As String
  94. If session is Nothing then
  95. MsgBox("This session is disconnected.", MsgBoxStyle.Critical, "TcpDemoApp")
  96. Return
  97. End If
  98. message = "Select a file to send to " & lvi.SubItems(2).Text
  99. ofdSendFileToClient.Title = message
  100. ofdSendFileToClient.FileName = ""
  101. ofdSendFileToClient.ShowDialog
  102. fileName = ofdSendFileToClient.FileName
  103. If fileName.Trim().Equals("") then Exit Sub
  104. If Not server.SendFile(fileName, session.sessionID) Then
  105. MsgBox("This session is disconnected.", MsgBoxStyle.Critical, "TcpDemoApp")
  106. End If
  107. End If
  108. End Sub
  109. Private Sub SendTextToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SendTextToolStripMenuItem.Click
  110. If lvClients.SelectedItems.Count > 0 then
  111. Dim lvi As ListViewItem = lvClients.SelectedItems.Item(0)
  112. ' Get the session using the sessionID pulled from the selected listview item
  113. Dim session As TcpComm.Server.SessionCommunications = server.GetSession(Convert.ToInt32(lvi.SubItems(1).Text))
  114. Dim message, title, defaultValue As String
  115. Dim retValue As Object
  116. If session is Nothing then
  117. MsgBox("This session is disconnected.", MsgBoxStyle.Critical, "TcpDemoApp")
  118. Return
  119. End If
  120. message = "Type some text to send to " & lvi.SubItems(2).Text
  121. title = "TcpComm Demo App"
  122. defaultValue = "Test text"
  123. retValue = InputBox(message, title, defaultValue)
  124. If retValue Is "" Then Return
  125. If session IsNot Nothing Then server.SendText(retValue.ToString(), 1, session.sessionID)
  126. End If
  127. End Sub
  128. Private Sub DisconnectSessionToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DisconnectSessionToolStripMenuItem.Click
  129. If lvClients.SelectedItems.Count > 0 then
  130. Dim lvi As ListViewItem = lvClients.SelectedItems.Item(0)
  131. ' Get the session using the sessionID pulled from the selected listview item
  132. Dim session As TcpComm.Server.SessionCommunications = server.GetSession(Convert.ToInt32(lvi.SubItems(1).Text))
  133. If session IsNot Nothing Then session.Close()
  134. End If
  135. End Sub
  136. Private Sub TestLargeArrayTransferHelperToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TestLargeArrayTransferHelperToolStripMenuItem.Click
  137. If lvClients.SelectedItems.Count = 0 then
  138. MsgBox("You must select a connected client to send an array.", MsgBoxStyle.Critical, "TcpComm Demo App")
  139. Return
  140. End If
  141. Dim lvi As ListViewItem = lvClients.SelectedItems.Item(0)
  142. ' Get the session using the sessionID pulled from the selected listview item
  143. Dim session As TcpComm.Server.SessionCommunications = server.GetSession(Convert.ToInt32(lvi.SubItems(1).Text))
  144. If session is Nothing then
  145. MsgBox("You can't send a large array to a disconnected session!", MsgBoxStyle.Critical, "TcpComm Demo App")
  146. Return
  147. End If
  148. Dim msg = "This version if the library includes a helper function for people attempting to send arrays larger then the maximum packetsize. " & _
  149. "In those cases, the array will be broken up into multiple packets, and delivered one by one. This helper class can be used to send the large arrays and " & _
  150. "have LAT (the TcpComm.Utilities.LargeArrayTransferHelper tool) assemble them for you in the remote machine. " & vbCrLf & vbCrLf & _
  151. "This test will read about 500k of a large text file into a byte array, and send it to the client you selected (this would normally arrive in about 8 pieces). When it arrives, it will be " & _
  152. "displayed in the 'Lat Viewer', a form with a multiline textbox on it that you can use to verify that all the text has been delivered and assembled " & _
  153. "properly, and verify the message size."
  154. Dim retVal As MsgBoxResult = MsgBox(msg, MsgBoxStyle.Information Or MsgBoxStyle.OkCancel, "TcpComm Demo App")
  155. If retVal = MsgBoxResult.Ok Then
  156. If session IsNot Nothing Then
  157. Dim fileBytes() As Byte = System.IO.File.ReadAllBytes("big.txt")
  158. Dim errMsg As String = ""
  159. If Not lat.SendArray(fileBytes, 100, session.sessionID, errMsg) Then MsgBox(errMsg, MsgBoxStyle.Critical, "TcpComm Demo App")
  160. End If
  161. End If
  162. End Sub
  163. End Class