Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Net
- Imports System.IO
- Module Module1
- Dim Sock As New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
- Dim Port As String = 81
- Dim Ip As String = "83.135.207.179"
- Dim Buffer(&H7FFF + 6 - 1) As Byte
- Sub Main()
- Sock.BeginConnect(IPAddress.Parse(Ip), Port, New AsyncCallback(AddressOf ConnectEnd), Nothing)
- Console.ReadKey()
- End Sub
- Sub ConnectEnd(ByVal ar As IAsyncResult)
- Try
- sock.EndConnect(ar)
- sock.BeginReceive(buffer, 0, buffer.Length, Sockets.SocketFlags.None, New AsyncCallback(AddressOf EndReceive), 0)
- Catch ex As Exception
- Console.WriteLine(ex.ToString)
- End Try
- End Sub
- Sub EndReceive(ByVal ar As IAsyncResult)
- Dim ReceivedBytes As Int32
- Dim Offset As Int32
- Offset = ar.AsyncState
- Try
- ReceivedBytes = Sock.EndReceive(ar)
- Catch ex As Exception
- Console.WriteLine(ex.ToString)
- End Try
- '#### check if connection is lost ####
- If ReceivedBytes = 0 Then
- Console.WriteLine("connection lost")
- Exit Sub
- End If
- '#### split data into silkroad packets ####
- '#### write remaing bytes at beginning of the buffer ####
- '#### set new offset ####
- CreatPackets(Buffer, ReceivedBytes, Offset)
- 'Console.WriteLine(BitConverter.ToString(Buffer, 0, ReceivedBytes))
- '#### begin next receive ####
- Sock.BeginReceive(Buffer, 0 + Offset, Buffer.Length - Offset, Sockets.SocketFlags.None, New AsyncCallback(AddressOf EndReceive), Offset)
- End Sub
- Sub CreatPackets(ByRef buffer() As Byte, ByRef ReceivedBytes As Int32, ByRef Offset As Int32)
- Dim br As New BinaryReader(New MemoryStream(buffer))
- Dim BytesInBuffer As Int32 = ReceivedBytes + Offset
- Dim packetlen As Int16
- Dim packetOPC As Int16
- Dim packetSecBytes As Int16
- Dim packetdata() As Byte
- Do
- '#### check if header is complett ####
- If br.BaseStream.Position + 6 <= BytesInBuffer Then
- packetlen = br.ReadInt16()
- packetOPC = br.ReadInt16()
- packetSecBytes = br.ReadInt16()
- Else 'header not complett
- Exit Do
- End If
- '#### check if we have all data bytes ####
- If br.BaseStream.Position + packetlen <= BytesInBuffer Then
- packetdata = br.ReadBytes(packetlen)
- Else
- br.BaseStream.Position -= 6 ' header belong to not complett packet --> 6 byte back in stream
- Exit Do
- End If
- '#### packet complett let's print it ####
- Console.WriteLine("len:{0} opc:{1} sec:{2} data:{3}", _
- packetlen.ToString("X2"), _
- packetOPC.ToString("X2"), _
- packetSecBytes.ToString("X2"), _
- BitConverter.ToString(packetdata))
- Loop
- '#### check if we read all bytes from the buffer ####
- If br.BaseStream.Position = ReceivedBytes + Offset Then
- Offset = 0 'set new offset
- Else 'didn't read all bytes --> calc the offset and write the remaining bytes to the beginning of the buffer
- Dim bw As New BinaryWriter(New MemoryStream(buffer))
- Offset = BytesInBuffer - br.BaseStream.Position 'set new offset
- bw.Write(br.ReadBytes(Offset)) ' write remaining bytes back to beginning of the buffer
- bw.Flush()
- bw.Close()
- End If
- br.Close()
- End Sub
- End Module
Add Comment
Please, Sign In to add comment