Link to home
Start Free TrialLog in
Avatar of cossy74
cossy74

asked on

TcpClient and Sending Integers, Doubles, etc

Hi,

I can find lots of things show how to send strings across a TcpClient using network streams. But my question is how do i send a number, in particluar an integer.

ON THE Client Side I have this....
       int SendingOne = 1;
       Byte[] data = System.Text.Encoding.ASCII.GetBytes(BitConverter.GetBytes(SendingOne)); // THis doesnot work.... what should it be????
       stream.Write(data, 0, sizeof(int));

ON The Server Side I have this.....
        i = stream.Read(bytes, 0, sizeof(int));
        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
        int SendingOne = Convert.ToInt16(data);                       // This doesn't work
       
Avatar of joechina
joechina

Can you try,
Client:
Modify
Byte[] data = BitConverter.GetBytes(SendingOne);

Server:
Remove data = System.Text....
Modify
int SendingOne = BitConverter.ToInt32(i,0);
Client Side:

Byte[] byteData = System.Text.Encoding.Default.GetBytes(SendingOne);

then write the data in to port.
ASKER CERTIFIED SOLUTION
Avatar of joechina
joechina

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial