How to read an unsigned 32 bit value coming in via socket??

I

Isi Robayna

Hello,

I am trying to communicate with a legacy socket server. The first thing this
legacy server does upon getting a client connection is to send an ID back to
the client... I am trying to read that ID Value. By the way, that ID is an
uint (32 bit unsigned integer)

I have tried a number of different things to translate the buffer coming in
to the a number, and I cannot translate the number(ID) in the buffer to a
uint .....

Any help welcome.

Thanks,

Isi r.

Here is the code:

TcpClient myClientSocket;

try
{
myClientSocket = new TcpClient("127.0.0.1",9099);
}
catch (Exception ex)
{
Console.WriteLine("Error connecting socket:" + ex.ToString());
return;
}

NetworkStream networkStream = myClientSocket.GetStream();


try
{

byte[] myDataBuffer = new byte[4];

String responseData = String.Empty;

int bytes = networkStream.Read(myDataBuffer,0, myDataBuffer.Length);

uint myThreadID;

Array.Reverse(myDataBuffer); // reverse (little/big endian
issues) --not sure if I need this


responseData = System.Text.Encoding.ASCII.GetString(myDataBuffer,0,4);


myThreadID = Convert.ToUInt32 ( responseData ); // <=== always get
zero...

Console.WriteLine("Received: {0} {1}", responseData, myThreadID);

}
catch
{

}

myClientSocket.Close();
myClientSocket = null;

}
 
J

Jhon

Hi,
inline

Isi Robayna said:
Hello,

I am trying to communicate with a legacy socket server. The first thing this
legacy server does upon getting a client connection is to send an ID back to
the client... I am trying to read that ID Value. By the way, that ID is an
uint (32 bit unsigned integer)

Tcp is like a byte stream. So in order to receive and decode it, you should
know more about how the server send this uint.
I have tried a number of different things to translate the buffer coming in
to the a number, and I cannot translate the number(ID) in the buffer to a
uint .....

You should check the return value from receive to see if it actually
received the number of bytes expected. _Usually_ a short message is ended
with \r\n, in that case you must keep receiving until the data ends with
\r\n.

Now there are two ways I can come up how the server sends this uint:
1)
The server sends this as an asci string. This is how you treat it in your
code. But in this case you certainly do not rotate the bytes. If the
server sends it like a string, you should see a number when you display the
string after asci.encoding...

2)
The server breaks down the uint into 4 bytes, by shifting it three times to
the right. In that case the LSB is send first.
To restore the uint something like this should work:

uint id = ((uint)myDataBuffer[3]<<24) |
((uint)myDataBuffer[2]<<16) |
((uint)myDataBuffer[1]<<8) |
((uint)myDataBuffer[0]);

HTH
greetings

Any help welcome.

Thanks,

Isi r.

Here is the code:

TcpClient myClientSocket;

try
{
myClientSocket = new TcpClient("127.0.0.1",9099);
}
catch (Exception ex)
{
Console.WriteLine("Error connecting socket:" + ex.ToString());
return;
}

NetworkStream networkStream = myClientSocket.GetStream();


try
{

byte[] myDataBuffer = new byte[4];

String responseData = String.Empty;

int bytes = networkStream.Read(myDataBuffer,0, myDataBuffer.Length);

uint myThreadID;

Array.Reverse(myDataBuffer); // reverse (little/big endian
issues) --not sure if I need this


responseData = System.Text.Encoding.ASCII.GetString(myDataBuffer,0,4);


myThreadID = Convert.ToUInt32 ( responseData ); // <=== always get
zero...

Console.WriteLine("Received: {0} {1}", responseData, myThreadID);

}
catch
{

}

myClientSocket.Close();
myClientSocket = null;

}
 
J

Jack Hanebach

Isi said:
byte[] myDataBuffer = new byte[4];

String responseData = String.Empty;

int bytes = networkStream.Read(myDataBuffer,0,
myDataBuffer.Length);

uint myThreadID;

Array.Reverse(myDataBuffer); // reverse (little/big endian
issues) --not sure if I need this


responseData =
System.Text.Encoding.ASCII.GetString(myDataBuffer,0,4);


myThreadID = Convert.ToUInt32 ( responseData ); // <=== always
get zero...

Your buffer contains four bytes of uint value, not the string
representation of it. instead of stuffing it into string and using
Convert you can just add the bytes together (after shifting them into
place, of course :)
 
W

William Armstrong

Take a look at the BitConvertor class.
Specifically: BitConvertor.ToInt32()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top