Solved

TcpClient and Sending Integers, Doubles, etc

Posted on 2006-11-01
3
259 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
  • 2
3 Comments
 
LVL 13

Expert Comment

by:joechina
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry,

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now