-
Jan 8th, 2013, 11:40 AM
#1
Thread Starter
Registered User
send hex data to Ethernet or LAN using Visual basic 6.0
I wanted to send hex data to Ethernet or LAN using Visual basic 6.0.the hex data will be like AB7745683ACB76B34E5FF5E99EBC5F878A6BC8E9DF9876BCEA89
On the form i have a text box in which user will input this data .
How can i send this hex data to LAN or Ethernet using vb6.0
-
Jan 8th, 2013, 04:08 PM
#2
Re: send hex data to Ethernet or LAN using Visual basic 6.0
It will not be HEX data if enter into a textbox as shown. It would be a string containing those characters which when read as hex would look quite a lot different.
As for sending it to the LAN, a Lan does not really process data it just allows it to travel from one point to another. The most common means of sending data would be TCP which you can do using the Winsock control but there must be a target that will receive the data.
-
Jan 8th, 2013, 05:15 PM
#3
Re: send hex data to Ethernet or LAN using Visual basic 6.0
Yeah, not enough useful information to begin answering this.
While API calls may permit sending IP layer traffic there are generally security issues getting access to "raw sockets" and Microsoft blocked their use in XP with a security patch circa 2004. I don't think there is any API support for mucking below that (Ethernet MAC layer) and only a device driver would have such abilities. There are even fewer non-malware reasons to do this than raw IP.
From the question I'm not even sure how we should interpret "LAN" which might be talking about LAN Manager (MS Networking) services such as file and printer sharing and IPC.
But mostly likely this is a very naively expressed question asking about sending binary data entered as hexadecimal text over a TCP connection. This would really be two separate questions: hex text to binary data, and sending binary data over TCP (or possibly UDP).
-
Jan 8th, 2013, 10:43 PM
#4
Re: send hex data to Ethernet or LAN using Visual basic 6.0
See my signature, specifically Binary Server(or buddy paint, perhaps) and HexToAsc.
Software I use and highly recommend: Opera, Miranda IM, Peerblock, Winamp, Unlocker Assistant, JoyToKey, Virtual CloneDrive, Secunia PSI, ExplorerXP, GOM Player, Real Alternative, Quicktime Alternative,Sumatra PDF, and non-freeware: Photoshop and VB6().
My codebank: AllRGB, Rounded Rectangle(math), Binary Server, Buddy Paint, LoadPictureGDI+, System GUID/Volume Serial, HexToAsc, List all processes and their paths, quasiString matching
Strings(search, extraction, retrieval etc): Retrieve BBCode Link from HTML, RemoveBetween ()'s, strFindBetween(str1,str2), Insert text in HTML, HTML - GetSpanByID
-
Jan 9th, 2013, 01:27 AM
#5
Thread Starter
Registered User
Re: send hex data to Ethernet or LAN using Visual basic 6.0
Originally Posted by DataMiser
It will not be HEX data if enter into a textbox as shown. It would be a string containing those characters which when read as hex would look quite a lot different.
As for sending it to the LAN, a Lan does not really process data it just allows it to travel from one point to another. The most common means of sending data would be TCP which you can do using the Winsock control but there must be a target that will receive the data.
yes we have a target that will recieve the data.
and yes the data in the text box will be a string containg the hex data.
and yes winsock can be used.
but i m new to vb and tcp programming so i dont know how to use winsock and send that hex data in the text box. plz help.
-
Jan 9th, 2013, 02:45 AM
#6
Re: send hex data to Ethernet or LAN using Visual basic 6.0
Since the data is going to be entered by a User into a TextBox you'll need to perform some validation on it before convering to binary values which can then be sent.
Code:
Private Sub Command1_Click()
Dim boInvalid As Boolean
Dim intI As Integer
Dim intJ As Integer
Dim strHex As String
Dim bytToSend() As Byte
'
' Must be an even number of characters entered
'
If Len(txtdata.Text) Mod 2 = 0 Then
ReDim bytToSend((Len(txtdata.Text) \ 2) - 1)
intI = 1
Do
strHex = UCase(Mid$(txtdata.Text, intI, 2))
'
' Each character must be A thru F or 0 thru 9
'
For intJ = 1 To 2
Select Case Mid$(strHex, intJ, 1)
Case "A" To "F", "0" To "9"
Case Else
boInvalid = True
End Select
Next intJ
'
' If it's a valid pair of characters
' convert the hexadecimal representation to a binary value
' and store in the next element of the array
' If it's invalid, tell the user and stop processing
'
If Not boInvalid Then
bytToSend(intI \ 2) = Val("&H" & Mid$(txtdata.Text, intI, 2))
intI = intI + 2
Else
MsgBox "Invalid Hexadecimal Value: " & strHex
End If
Loop Until intI > Len(txtdata.Text) Or boInvalid
'
' If it was valid hexadeciomal data then send it
'
If Not boInvalid Then
Winsock1.SendData bytToSend
End If
Else
MsgBox "Invalid Data - Hexadecimal data must be an even number of characters"
End If
End Sub
Of course, this assumes that you've already made the connection to the other end.
-
Jan 9th, 2013, 12:35 PM
#7
Re: send hex data to Ethernet or LAN using Visual basic 6.0
Can you not just allow 0 - 9, A - F (upper and/or lower) in the text box and calculate on every two-characters entered?
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jan 9th, 2013, 02:08 PM
#8
Re: send hex data to Ethernet or LAN using Visual basic 6.0
Yes, you can, but you'd need to look at the TextBox Change event to trap Paste operations (my guess is that the user will probably be pasting the data from somewhere rather than typing it all in - not many users speak fluent Hexadecimal). You might have to use ReDim Preserve for the Byte Array (or use a Collection) if you do it that way, as you don't know the total size of the Input in the TextBox events. I'd go with the simpler 'batch' orientated approach and do the validation when the user clicks the 'Go do it' button.
At the end of the day it's a matter of style and how much code you want to write.
As a side issue, I'm not fond of the type of 'If some_condition Then do nothing, Else do something, EndIf' construct, as in the Select Case statements I used. With an If / Then statement it's easy to invert the logic to: 'If Not (some_condition) Then do something, EndIf'. I'm struggling to find a simple way of inverting the logic of the Case Statements
i.e.
instead of
Code:
Case "A" To "F", "0" To "9"
Case Else
boInvalid = True
something like:
Code:
Case Not ("A" To "F", "0" To "9")
boInvalid = True
which obviously doesn't work, and I can't find a combination of logical operators or Select Case that will work. 'Not', in this context, seems to require some brackets and Not("A") will not cast to a boolean so there's a Type Mismatch. There must be a way.........perhaps I can't see the Woods for the Trees.
I suppose
Code:
Case Chr(0) to Chr(Asc("0") - 1), Chr(Asc("9") + 1) To Chr(Asc("A") - 1), Chr(Asc("F") + 1) To Chr(255)
boInvalid = True
might work but even if it does, it doesn't look very clear.
-
Jan 9th, 2013, 02:13 PM
#9
Re: send hex data to Ethernet or LAN using Visual basic 6.0
Well the 'not very clear' is only for the programmer(s) and not the users. If the programmer(s) is/are programmers then I wouldn't worry about not very clear in this case as it looks clear to me and I'm not an expert programmer
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jan 9th, 2013, 02:22 PM
#10
Re: send hex data to Ethernet or LAN using Visual basic 6.0
That might be the case, but in loads of years programming I've learnt that there's always an 'elegant' way of doing things
-
Jan 9th, 2013, 02:39 PM
#11
Re: send hex data to Ethernet or LAN using Visual basic 6.0
Code:
Select Case True
Case Mid$(strHex, intJ, 1&) Like "[!0-9A-F]"
boInvalid = True
End Select
Last edited by Bonnie West; Jan 9th, 2013 at 03:29 PM.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Jan 9th, 2013, 03:02 PM
#12
Re: send hex data to Ethernet or LAN using Visual basic 6.0
OK, that seems to work but I don't understand the reason you hav "[" "!" and "]" in the test string.
Also, how do you prevent any character from being entered into a textbox?
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jan 9th, 2013, 03:28 PM
#13
Re: send hex data to Ethernet or LAN using Visual basic 6.0
Quoted from the Like Operator:
A group of one or more characters (charlist) enclosed in brackets ([ ]) can be used to match any single character in string and can include almost any character code, including digits.
An exclamation point (!) at the beginning of charlist means that a match is made if any character except the characters in charlist is found in string.
Code:
Private Sub Text1_GotFocus()
Text1.Locked = True 'Prevents pasting too
End Sub
Private Sub Text1_LostFocus()
Text1.Locked = False
End Sub
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Jan 9th, 2013, 04:00 PM
#14
Re: send hex data to Ethernet or LAN using Visual basic 6.0
That wasn't what I meant, Bonnie. I meant how to you prevent any character from being entered into a textbox as the user types in characters. Example: how to prevent user from typing in anything except numbers. I know your code example detects the characters but it doesn't stop the character from being entered. I need a way to allow only certain characters to be entered and if user types in an invalid character I want the code to simply prevent that character from being entered even if the user goes back and tries to insert it in the middle of the text.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jan 9th, 2013, 04:35 PM
#15
Re: send hex data to Ethernet or LAN using Visual basic 6.0
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKey0 To vbKey9, vbKeyBack
Case Else: KeyAscii = 0
End Select
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case True
Case Chr$(KeyAscii) Like "[!0-9]"
KeyAscii = 0
End Select
End Sub
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Jan 9th, 2013, 05:02 PM
#16
Re: send hex data to Ethernet or LAN using Visual basic 6.0
OK, that's it.
So, in response to Doogle's reply then why wouldn't this work instead of all that code?
Code:
Private Sub cmdSendHexString_Click()
If Len(Text1.Text) Mod 2 <> 0 Then
MsgBox "Must be even number of hex codes"
Text1.SetFocus
Else
'
' Code to send hex string or convert to actual hex
'
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case True
Case Chr$(KeyAscii) Like "[!0123456789AaBbCcDdEeFf]"
KeyAscii = 0
End Select
Exit Sub
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jan 9th, 2013, 06:17 PM
#17
Re: send hex data to Ethernet or LAN using Visual basic 6.0
As I mentioned earlier, what if I copy and paste into the TextBox ? The KeyPress event doesn't get triggered.
EDIT: and in the real world you'd have to cater for Backspace and Delete
Last edited by Doogle; Jan 9th, 2013 at 06:26 PM.
-
Jan 9th, 2013, 06:38 PM
#18
Re: send hex data to Ethernet or LAN using Visual basic 6.0
Oh, yeah, I forgot about you mentioning Copy/Paste. Oh, well, another plan foiled
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|