1

I have problem about socket to send multiple hex in one send to socket

Here the detail :

 Private Sub sendACK()
    Dim msgACK As String
    Dim sendBytes As Byte()
    tmpStr = ""
    list.Clear()
    msgACK = "33CC"
    For j = 1 To Len(msgACK)
        list.Add(Mid(msgACK, j, 2))
        j += 1
    Next

    For Each tmpStr In list
        sendBytes = HexToBytes(tmpStr)
        clientSocket.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, New System.AsyncCallback(AddressOf OnSend), clientSocket)
    Next
 End Sub

Public Function HexToBytes(ByVal s As String) As Byte()
    Dim bytes As String() = s.Split(" "c)
    Dim retval(bytes.Length - 1) As Byte
    For ix As Integer = 0 To bytes.Length - 1
        retval(ix) = Byte.Parse(bytes(ix), System.Globalization.NumberStyles.HexNumber)
    Next
    Return retval
End Function

Private Sub OnSend(ByVal ar As IAsyncResult)
    clientSocket = ar.AsyncState
    clientSocket.EndSend(ar)
    Thread.Sleep(100)
End Sub

==> List as arraylist

That code will be result :
Socket send 33
end send
Socket send CC
end send

============================
The program should be sending in one time like this :
Socket send 33 CC
end send

============================
is there any idea about convert string "33CC" into byte and then that program just sending 1 time in outter "for each" ?

thanks for reading and answering....
GBU

1 Answer 1

1

Your assumption is incorrect. It does indeed send both bytes at once. You are only calling BeginSend once and you are giving it both bytes, so if it is receiving them at all on the other end, then it is indeed sending them. In fact, there is no difference at all between sending them individually or sending them together. Since the socket works as a stream, the length of time between each byte being sent is largely irrelevant. There will be no way on the receiving end to know whether the two bytes were sent together or separately. All the receiving end will know is that the two bytes were sent in that order. If you think they are being sent as two separate "sends" on the receiving end, it sounds like you are making some invalid assumptions about how to read the data from the socket.

However, I should mention that the way you are sending the bytes, by first creating a hex string and then parsing it, is a bit silly. If you need to parse it as a string, because you are reading the hex values from a text file, or something, that's fine, but otherwise, you should just use hex byte literals in your code rather than strings, for instance:

Dim sendBytes As Byte() = {&H33, &HCC}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.