Guest User

Untitled

a guest
Feb 20th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.81 KB | None | 0 0
  1. Imports System.Net
  2. Imports System.IO
  3.  
  4. Module Module1
  5.  
  6.     Dim Sock As New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
  7.     Dim Port As String = 81
  8.     Dim Ip As String = "83.135.207.179"
  9.     Dim Buffer(&H7FFF + 6 - 1) As Byte
  10.  
  11.  
  12.     Sub Main()
  13.         Sock.BeginConnect(IPAddress.Parse(Ip), Port, New AsyncCallback(AddressOf ConnectEnd), Nothing)
  14.         Console.ReadKey()
  15.     End Sub
  16.  
  17.     Sub ConnectEnd(ByVal ar As IAsyncResult)
  18.         Try
  19.             sock.EndConnect(ar)
  20.             sock.BeginReceive(buffer, 0, buffer.Length, Sockets.SocketFlags.None, New AsyncCallback(AddressOf EndReceive), 0)
  21.         Catch ex As Exception
  22.             Console.WriteLine(ex.ToString)
  23.         End Try
  24.     End Sub
  25.     Sub EndReceive(ByVal ar As IAsyncResult)
  26.  
  27.         Dim ReceivedBytes As Int32
  28.         Dim Offset As Int32
  29.  
  30.         Offset = ar.AsyncState
  31.  
  32.         Try
  33.             ReceivedBytes = Sock.EndReceive(ar)
  34.         Catch ex As Exception
  35.             Console.WriteLine(ex.ToString)
  36.         End Try
  37.  
  38.         '#### check if connection is lost ####
  39.         If ReceivedBytes = 0 Then
  40.             Console.WriteLine("connection lost")
  41.             Exit Sub
  42.         End If
  43.  
  44.         '#### split data into silkroad packets               ####
  45.         '#### write remaing bytes at beginning of the buffer ####
  46.         '#### set new offset                                 ####
  47.         CreatPackets(Buffer, ReceivedBytes, Offset)
  48.         'Console.WriteLine(BitConverter.ToString(Buffer, 0, ReceivedBytes))
  49.         '#### begin next receive ####
  50.         Sock.BeginReceive(Buffer, 0 + Offset, Buffer.Length - Offset, Sockets.SocketFlags.None, New AsyncCallback(AddressOf EndReceive), Offset)
  51.  
  52.     End Sub
  53.  
  54.  
  55.     Sub CreatPackets(ByRef buffer() As Byte, ByRef ReceivedBytes As Int32, ByRef Offset As Int32)
  56.  
  57.         Dim br As New BinaryReader(New MemoryStream(buffer))
  58.         Dim BytesInBuffer As Int32 = ReceivedBytes + Offset
  59.  
  60.         Dim packetlen As Int16
  61.         Dim packetOPC As Int16
  62.         Dim packetSecBytes As Int16
  63.         Dim packetdata() As Byte
  64.  
  65.         Do
  66.             '#### check if header is complett ####
  67.             If br.BaseStream.Position + 6 <= BytesInBuffer Then
  68.                 packetlen = br.ReadInt16()
  69.                 packetOPC = br.ReadInt16()
  70.                 packetSecBytes = br.ReadInt16()
  71.             Else 'header not complett
  72.                 Exit Do
  73.             End If
  74.  
  75.             '#### check if we have all data bytes ####
  76.             If br.BaseStream.Position + packetlen <= BytesInBuffer Then
  77.                 packetdata = br.ReadBytes(packetlen)
  78.             Else
  79.                 br.BaseStream.Position -= 6 ' header belong to not complett packet --> 6 byte back in stream
  80.                 Exit Do
  81.             End If
  82.  
  83.             '#### packet complett let's print it ####
  84.             Console.WriteLine("len:{0} opc:{1} sec:{2} data:{3}", _
  85.                         packetlen.ToString("X2"), _
  86.                         packetOPC.ToString("X2"), _
  87.                         packetSecBytes.ToString("X2"), _
  88.                         BitConverter.ToString(packetdata))
  89.  
  90.         Loop
  91.  
  92.         '#### check if we read all bytes from the buffer ####
  93.         If br.BaseStream.Position = ReceivedBytes + Offset Then
  94.             Offset = 0 'set new offset
  95.         Else 'didn't read all bytes --> calc the offset and write the remaining bytes to the beginning of the buffer
  96.             Dim bw As New BinaryWriter(New MemoryStream(buffer))
  97.  
  98.             Offset = BytesInBuffer - br.BaseStream.Position 'set new offset
  99.             bw.Write(br.ReadBytes(Offset)) ' write remaining bytes back to beginning of the buffer
  100.  
  101.             bw.Flush()
  102.             bw.Close()
  103.         End If
  104.  
  105.         br.Close()
  106.  
  107.     End Sub
  108.  
  109.  
  110. End Module
Add Comment
Please, Sign In to add comment