tcp protocols

false74

Well-known member
Joined
Aug 4, 2010
Messages
76
Programming Experience
Beginner
I am working on writing a program that needs to send a command to a specific port on the same computer, I have that working fine. There is another piece of software running that is listening to that port for some commands (read on).
VB.NET:
    Dim myTcpClient As New TcpClient
    myTcpClient.Connect("127.0.0.1", 61225)
    Dim networkStream As NetworkStream = myTcpClient.GetStream()
However when I try to send a command I get no response...
VB.NET:
        Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes("0x33")
        networkStream.Write(outStream, 0, outStream.Length)
        networkStream.Flush()

I have been given a list of protocols that I need to send to that port. What I have been told is that the size (in byes) is 1 and the value is "0x33" with no parameters.

I am completely new to sockets and sending data through tcp, can I be lead in the right direction or get some assistance?
 
This line:
VB.NET:
Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes("0x33")
is wrong. You are creating a String and then getting the Bytes for those characters. That's not what the instruction means. It means that you need to send one Byte and that Byte has the value 0x33, which means hexadecimal 33, which is decimal 51. That code should be:
VB.NET:
Dim outStream As Byte() = {51}
or:
VB.NET:
Dim outStream As Byte() = {&H33}
Note that '0x' is used to prefix hexadecimal numbers in C-based languages while VB uses '&H'.
 
By the way, 'outStream' is a bad variable name because it implies that it refers to a Stream. That variable actually refers to data that will be written to a Stream, so 'outData' would be more appropriate, and there are many other more appropriate options too. Always use names that describe the actual purpose of the data they contain.
 
Thanks mate, later on in the list it mentions some values with parameters. where would those be added to?
 
Last edited:
Works fine for me. Please don't let us know that "an error" occurred without letting us know what error occurred, i.e. at least the error message but the entire exception data is not a bad idea.
 
I don't want to make a entire new thread as my problem still relates to this: currently I have an array of bytes and an array of ints. I need to add &HFF to an int and put it into the array of bytes, however when I do this I get an "Overflow Exception... Arithmetic operation resulted in an overflow"

Here is my code that gives me the error:
VB.NET:
Dim index As Int32 = 0
Dim buffer(256) As Byte
Dim nParam() As Int32 = {1, 0}
Dim nParamCount as Int32 = nParam.Count
For i = 0 To nParamCount - 1 Step 1
            index += 1
            buffer(index) = CByte(nParam(i) + &HFF) 'ERROR RESULTS HERE
            index += 1
            buffer(index) = CByte((nParam(i) >> 8) + &HFF)
 Next
This code was translated from c++, so here was the original line:
VB.NET:
buffer[index++] = BYTE(nParam[i] & 0xFF);
 
That code is not adding the byte to the integer. It is performing a bitwise AND to mask out all but the low end byte. In VB, you perform a bitwise AND with the 'And' operator.
 
Back
Top