Solved

TcpClient and Sending Integers, Doubles, etc

Posted on 2006-11-01
3
265 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 500 total points
ID: 17851703
Sorry,

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

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

732 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