• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

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
       
0
cossy74
Asked:
cossy74
  • 2
1 Solution
 
joechinaCommented:
Can you try,
Client:
Modify
Byte[] data = BitConverter.GetBytes(SendingOne);

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

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

then write the data in to port.
0
 
joechinaCommented:
Sorry,

int SendingOne = BitConverter.ToInt32(i,0);
should be
int SendingOne = BitConverter.ToInt32(bytes,0);
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now