Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Winsock receiving weird data

Status
Not open for further replies.

wakkara

Programmer
Jun 15, 2005
31
0
6
MX
Hi there

I am developing an aplication on VB6 with Winsock control.

It works this way.

I make a "listener" for the port 8300 of the server, wich is the machine y I am running the application.

When the data arrives, I diplay it on a textbox, in order to see what the client is sending.

The point is the client side is sending a paquet (which i CAN´T modificate) like this.

$$ + W + NUM (7byte) + command (2byte) + DATA + COMP(2byte) + \r\n

Wich means

$$ - is the beginning of the paquet
W - It has the Length of the paquet, an it on HEX
NUM - Is an Identifier (Number) of the client wich can be 7 bytes long and is sent on HEX form.

"The client.- the manufacturer claims that if the NUM contains a number like this 13612345678, it will show on the server side something like this: 0x13,0x61,0x023,0x45,0x67,0x8f,0xff"

The rest of the paquet .....

command, DATA or COMP area arriving as it should.


The Problem is that on the applications which is the server side i am receiving something like this.

$$VU%qˆÿÿ™U "DATA OMITTED" |0.8|2379|0000n¤

As you can see the $$ is arriving as the manufacturer claims it will arrive.

But the W or the NUM iit is showing weird values like VU%qˆÿÿ™U.

I think WINSOCK is deducing that the data is on HEX format and because the WINSOCK control only receive string or byte etc.... is twisting the HEX format.. (Well I think).

I would love to be able to read all the paquet as it should.

Please any help will be great.

 
Paquet" sounds like French for "packet" and is irrelevant in any case, since you don't deal with packets in TCP. TCP is an octet stream (roughly identical to a byte stream).

If you use GetData with a String variable, the available octects will be treated as "ANSI" characters using the system codepage and will be translated into UTF-16LE characters (VB6 characters) and transferred into your String variable.

This can scramble binary data.

You need to use GetData with a dynamic Byte array, not a String. Then you'll need to extract the subfields of your messages (not paquets - there are no paquets/packets) into String and binary values. Note that multibyte binary integers may be in network byte order, which is the opposite of the Intel x86 byte ordering for an Integer or Long as VB6 sees them. Of course we can't assume the "other end" obeys the TCP/IP rules of the road and may well be sending in Intel byte order.

Ideally you could do something like:
Code:
wsk.GetData strHead, vbString, 3 'Get $$W
wsk.GetData curNUM, vbCurrency, 7 'Get NUM
wsk.GetData intCommand, vbInteger, 2 'Get command
:
etc.
However the 3rd parameter (maxLen) only applies when String and Byte Array variables are used... and a Currency variable is 8 bytes (not 7).

So... You need to GetData the whole thing into a Byte Array and pick out what you want from there.
 
Thanks dilettante for taking the time to think on solving my problem.

I will try your solution

I manage to solve the problem with help of this code URL:
how i solve it?

As dilettante correctly pointed, is not a packet/paquet its just different data separated with "," or "|".

I Managed to substring the data it arrives, and convert it from hex to ascii with this code.
And there you go.


Private Function HexString(ByVal EvalString As String) As String
Dim intStrLen As Integer
Dim intLoop As Integer
Dim strHex As String

EvalString = Trim(EvalString)
intStrLen = Len(EvalString)
For intLoop = 1 To intStrLen
strHex = strHex & "" & Hex(Asc(Mid(EvalString, intLoop, 1)))
Next
HexString = strHex

End Function

So i think it comes on a HEX format, and it needed to be converted to a reading format.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top