Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Socket programming

Socket programming

Scheduled Pinned Locked Moved Visual Basic
helpquestiontutorial
7 Posts 3 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cory Kimble
    wrote on last edited by
    #1

    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.toString

    First, 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.

    L 1 Reply Last reply
    0
    • C Cory Kimble

      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.toString

      First, 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.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.


      C 1 Reply Last reply
      0
      • L Luc Pattyn

        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.


        C Offline
        C Offline
        Cory Kimble
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • C Cory Kimble

          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.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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.


          C 1 Reply Last reply
          0
          • L Luc Pattyn

            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.


            C Offline
            C Offline
            Cory Kimble
            wrote on last edited by
            #5

            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.

            L D 2 Replies Last reply
            0
            • C Cory Kimble

              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.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              bytearray(i) = &h1 or bytearray(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.


              1 Reply Last reply
              0
              • C Cory Kimble

                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.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups