We help IT Professionals succeed at work.

Binary TC{Client communication

esps
esps asked
on
Hi Experts,

I am writing a module that must listen on a port waiting for the connection from remote device, when connection is established, a string is made up (partially hardcoded) I then want to send this string in binary to the remote device and shuld get a response back.
It only work partially. I think my problem is with the bianary part.
Please have a look at the code I've done.
Would appreciate assistance
regards
Dim serverSocket As New TcpListener(GetIPAddress(), 2700)
        Dim requestCount As Integer
        Dim clientSocket As TcpClient
        serverSocket.Start()
        TextBox6.Text = "Server Started"
        clientSocket = serverSocket.AcceptTcpClient()
        TextBox6.Text = "Connected to MU"
        requestCount = 0
      Dim MyIFormatter As IFormatter = New BinaryFormatter()
        Dim serverStream As NetworkStream = clientSocket.GetStream()
        Dim aa As String = "55" & " " & Mid(Trim(TextBox2.Text), 1, 2) & " " & Mid(Trim(TextBox2.Text), 3, 2) & " " & Mid(Trim(TextBox5.Text), 1, 2) & " " & Mid(Trim(TextBox5.Text), 3, 2) & " " & "01 00 E1 FF 02 00 61 61 61 61 61 61 61 61 61 61 61 61 70 06 AA" 'The string that must be sent as binary
        Dim vCheckSum As Byte = DoChecksum(aa)
        Dim aaa As String = Mid(aa, 3, 73)
        Dim pp As Integer = computeCheckSum(aaa)
        Dim input As String = aa
        Dim sb As New System.Text.StringBuilder()

        Dim xorhex As String = "1B"
        Dim xornum As Integer = Integer.Parse(xorhex, Globalization.NumberStyles.HexNumber)

        For Each numhex As String In input.Split(" "c)
            Dim num As Integer = Integer.Parse(numhex, Globalization.NumberStyles.HexNumber)
            sb.Append((num Xor xornum).ToString("X2")).Append(" ")
        Next
        Dim MUString As New SerialData()
        MUString.TheString = aa
        MyIFormatter.Serialize(serverStream, MUString)

        serverStream.Flush()

        TextBox3.Text = aa & vbCrLf & decimalDisplay
        Dim inStream(10024) As Byte
        serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
        Dim returndata As String = _
        System.Text.Encoding.ASCII.GetString(inStream)
        TextBox6.Text = returndata
        serverStream.Close()
        clientSocket.Close()

Open in new window

Comment
Watch Question

Network Administrator, Network Consultant
Top Expert 2011
Commented:
Most Valuable Expert 2012
Top Expert 2014
Commented:
I am not sure how you are converting to binary. You would do the opposite of

System.Text.Encoding.ASCII.GetString(inStream)

and use

System.Text.Encoding.ASCII.GetBytes(StringToWrite)

to get a byte array and write it to socket.
Maen Abu-TabanjehNetwork Administrator, Network Consultant
Top Expert 2011

Commented:
is your problem solved?