?
Solved

TcpClient and Sending Integers, Doubles, etc

Posted on 2006-11-01
3
Medium Priority
?
267 Views
Last Modified: 2010-08-05
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
Comment
Question by:cossy74
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Expert Comment

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

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

Expert Comment

by:bhagavan2uday
ID: 17850332
Client Side:

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

then write the data in to port.
0
 
LVL 13

Accepted Solution

by:
joechina earned 2000 total points
ID: 17851703
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question