Socket programming
-
I am having difficulty communication through a socket. I am new to this, so bare with me. I am trying to pass string control protocols to a device like a display sign. Here is how I have connected to the sign. I think this has worked because I don't get an error. Plus, when I try to connect again it says that I am already connected.
Private endpoint as new IPEndPoint(ipaddress, port)
private socket as new socket(endpoint.addressFamily, SocketType.Stream, ProtocolType.Tcp)--Sub
socket.connect(endpoint)
--end sub The issue is when I try to send the protocol text string to the sign. I need help formatting the string. EXAMPLE: The documentation says to send <0x01>Z00<0x02>AA<0x1C>1<0x1D>B3This is where message is.<0x04> So I changed it to hex:
dim signmsg = This is where message is.
dim signstring = &H1.tostring + "Z00" + &H2.tostring + "AA" + &H28.tostring + "1" + &H29.tostring + "B3" + signmsg + &H4.toStringFirst, can you see if I am doing any thing wrong above. Second, What is the easiest way to get this data formatted correctly and sent to the socket. Will this work?
dim Buffer() as Byte = System.text.acsciiencoding.ascii.getbytes(signString)
To send I use
socket.send(Buffer, buffer.length, Sockets.SocketFlags.none)
But I get nothing from the sign. What am I doing wrong?
I can't think of anything cool and nerdy to say.
-
I am having difficulty communication through a socket. I am new to this, so bare with me. I am trying to pass string control protocols to a device like a display sign. Here is how I have connected to the sign. I think this has worked because I don't get an error. Plus, when I try to connect again it says that I am already connected.
Private endpoint as new IPEndPoint(ipaddress, port)
private socket as new socket(endpoint.addressFamily, SocketType.Stream, ProtocolType.Tcp)--Sub
socket.connect(endpoint)
--end sub The issue is when I try to send the protocol text string to the sign. I need help formatting the string. EXAMPLE: The documentation says to send <0x01>Z00<0x02>AA<0x1C>1<0x1D>B3This is where message is.<0x04> So I changed it to hex:
dim signmsg = This is where message is.
dim signstring = &H1.tostring + "Z00" + &H2.tostring + "AA" + &H28.tostring + "1" + &H29.tostring + "B3" + signmsg + &H4.toStringFirst, can you see if I am doing any thing wrong above. Second, What is the easiest way to get this data formatted correctly and sent to the socket. Will this work?
dim Buffer() as Byte = System.text.acsciiencoding.ascii.getbytes(signString)
To send I use
socket.send(Buffer, buffer.length, Sockets.SocketFlags.none)
But I get nothing from the sign. What am I doing wrong?
I can't think of anything cool and nerdy to say.
Hi Cory, have you looked at the content of signstring? IMO it is not what you want it to be. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
Hi Cory, have you looked at the content of signstring? IMO it is not what you want it to be. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
I also tried this that worked on a different project.
Dim sValue As String
Dim i As Integer = 0 Do While signString.Length > 0 'first I take each character using substring sValue = signString.Substring(0, 1).ToString() 'then convert character into ascii. sValue = Strings.Asc(sValue).ToString 'then convert ascii value into Hex Format sValue = Conversion.Hex(sValue) 'after converting remove the character. buffer(i) = Byte.Parse(sValue, Globalization.NumberStyles.AllowHexSpecifier) signString = signString.Substring(1, signString.Length - 1) i = i + 1 Loop
I can't think of anything cool and nerdy to say.
-
I also tried this that worked on a different project.
Dim sValue As String
Dim i As Integer = 0 Do While signString.Length > 0 'first I take each character using substring sValue = signString.Substring(0, 1).ToString() 'then convert character into ascii. sValue = Strings.Asc(sValue).ToString 'then convert ascii value into Hex Format sValue = Conversion.Hex(sValue) 'after converting remove the character. buffer(i) = Byte.Parse(sValue, Globalization.NumberStyles.AllowHexSpecifier) signString = signString.Substring(1, signString.Length - 1) i = i + 1 Loop
I can't think of anything cool and nerdy to say.
Seems like you ignored my previous reply. The latest code snippet is garbage, it may do what you need, it does what it does in a horrible way. It is clear you still don't really understand how to work with strings, characters, bytes, and hex. You should never have to convert to string and then parse back to numbers, that is non-sense. I recall we have had similar conversations in the past. I suggest you do not use strings at all, just have a byte array and fill it with what is required: the special values such as 1 and 2; and the ASCII value of the text characters you need, if any. Then send the byte array into the socket. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
Seems like you ignored my previous reply. The latest code snippet is garbage, it may do what you need, it does what it does in a horrible way. It is clear you still don't really understand how to work with strings, characters, bytes, and hex. You should never have to convert to string and then parse back to numbers, that is non-sense. I recall we have had similar conversations in the past. I suggest you do not use strings at all, just have a byte array and fill it with what is required: the special values such as 1 and 2; and the ASCII value of the text characters you need, if any. Then send the byte array into the socket. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
It is true that I have difficulty with sockets and strings/ascii/chr/bytes. But, you haven't given me any good examples. I learn better with examples. The documentation says <0x01>Z00<0x02>AA<0x1>1<0x1D>B3This is where message is.<0x04> So, can you give me an example of "suggest you do not use strings at all, just have a byte array and fill it with what is required:the special values such as 1 and 2; and the ASCII value of the text characters you need, if any. Then send the byte array into the socket." So is that, bytearray(0) = &h1 or 1 bytearray(1) = How to convert into ascii value Sorry, for these simple questions. I have gone around and around with this and have confused myself. Thank you for your help.
I can't think of anything cool and nerdy to say.
-
It is true that I have difficulty with sockets and strings/ascii/chr/bytes. But, you haven't given me any good examples. I learn better with examples. The documentation says <0x01>Z00<0x02>AA<0x1>1<0x1D>B3This is where message is.<0x04> So, can you give me an example of "suggest you do not use strings at all, just have a byte array and fill it with what is required:the special values such as 1 and 2; and the ASCII value of the text characters you need, if any. Then send the byte array into the socket." So is that, bytearray(0) = &h1 or 1 bytearray(1) = How to convert into ascii value Sorry, for these simple questions. I have gone around and around with this and have confused myself. Thank you for your help.
I can't think of anything cool and nerdy to say.
bytearray(i) = &h1
orbytearray(i) = 1
both would take care of <0x01>bytearray(i) = ASC("Z"c)
would take care of a letter "Z" You do know all this, you have used it all before. :)Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
It is true that I have difficulty with sockets and strings/ascii/chr/bytes. But, you haven't given me any good examples. I learn better with examples. The documentation says <0x01>Z00<0x02>AA<0x1>1<0x1D>B3This is where message is.<0x04> So, can you give me an example of "suggest you do not use strings at all, just have a byte array and fill it with what is required:the special values such as 1 and 2; and the ASCII value of the text characters you need, if any. Then send the byte array into the socket." So is that, bytearray(0) = &h1 or 1 bytearray(1) = How to convert into ascii value Sorry, for these simple questions. I have gone around and around with this and have confused myself. Thank you for your help.
I can't think of anything cool and nerdy to say.
By doing "&H1.ToString", you actually sent a "1" character, not the binary value 1. What you actually sent was:
(0x31) Z 0 0 (0x32) A A (0x31) 1
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008