Guest User

Untitled

a guest
Jul 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. Imports System.Net
  2. Imports System.Net.Sockets
  3. Imports System.IO
  4.  
  5. Public Class mainForm
  6.  
  7. Private Sub mainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  8.  
  9. Dim blah As backgroundServer
  10.  
  11. initServer()
  12.  
  13. blah = New backgroundServer
  14. blah.bgServerInstance.RunWorkerAsync()
  15.  
  16. ' updateServerLabel("called from main") <--works
  17.  
  18. End Sub
  19.  
  20.  
  21. Public Sub updateServerLabel(ByVal strMessage As String)
  22.  
  23. lblServerInfo.Text = strMessage
  24.  
  25. End Sub
  26.  
  27. End Class
  28.  
  29. Imports System.Net
  30. Imports System.Net.Sockets
  31. Imports System.IO
  32.  
  33. Module bgServer
  34.  
  35. Public strLocalIP As String
  36. Public strHostName As String
  37. Public intPortNumber As Int32
  38.  
  39. Public Sub initServer()
  40. strHostName = System.Net.Dns.GetHostName()
  41. strLocalIP = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString
  42. intPortNumber = 1052
  43.  
  44. ' mainForm.updateServerLabel("called from init") <--works
  45. End Sub
  46.  
  47.  
  48. Public Class backgroundServer
  49.  
  50. Public WithEvents bgServerInstance As New System.ComponentModel.BackgroundWorker
  51.  
  52. Private Sub bgServerInstance_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgServerInstance.DoWork
  53.  
  54. Dim server As TcpListener = Nothing
  55. Dim client As TcpClient = Nothing
  56. Dim stream As NetworkStream = Nothing
  57. Dim port As Int32 = 8888
  58. Dim localAddr As IPAddress = IPAddress.Parse(strLocalIP)
  59.  
  60. Dim data As String = Nothing
  61. Dim i As Int32
  62. Dim bytes(1024) As Byte
  63. Dim retMsg As Byte() = System.Text.Encoding.ASCII.GetBytes("Default Reply")
  64.  
  65. mainForm.updateServerLabel("called from bg") '<-- does not work
  66.  
  67. server = New TcpListener(localAddr, port)
  68. server.Start()
  69.  
  70. While (True)
  71.  
  72. Try
  73. client = server.AcceptTcpClient()
  74.  
  75. MsgBox("client accepted!") '<--works
  76.  
  77. client.Close()
  78. Catch ex As Exception
  79. MsgBox(ex.Message)
  80. End Try
  81.  
  82. End While
  83.  
  84. End Sub
  85.  
  86. End Class
  87.  
  88. End Module
Add Comment
Please, Sign In to add comment