Unix Programming - passing integers between client and server

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2006 > passing integers between client and server





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author passing integers between client and server
sai

2006-04-02, 7:42 pm

Hi,
how can i send integers between client and server in tcp/ip socket
programming...
example: adding of 2 numbers.
Client takes inputs ( say x and y) and sends them to server ...
Server does the processing(z=x+y).. and sends the result to client.
i am able to pass string messages between client and server,but how can
i pass integers is my question.
Regards,
Sai

Steve Brown

2006-04-02, 7:42 pm

> i am able to pass string messages between client and server,but how can
> i pass integers is my question.


Personally I wouldn't even try to send the binary representation of the
integer,
I would convert them to a string and send that (most of the standard TCP/IP
protocols are text based too). It's much easier to debug things
that way. Okay a number like '1234' will take 4 bytes to represent instead
of 2 (assuming you've put it in a 16 bit integer), but if you
can measure the difference in most applications I'd be surprised.

So in the client use something like:
sprintf(stringtosend, "Add:%d:%d:\n",i1, i2);

Then at the server end split the string up into separate strings (strtok)
and then use atoi to convert the input data back to integers.

I would then process the decoded command and create a string to send back
the result with:

sprintf(replystring,"AddResult:%d:\n",resultinteger);

Using strings also has the advantage if you are working across platforms
because binary integers can be represented differently on different hardware
and so you'll just run into trouble if you try and send them without
realising this.

HTH


Barry Margolin

2006-04-02, 7:42 pm

In article <1143603045.887269.55200@g10g2000cwb.googlegroups.com>,
"sai" <aravallisai@yahoo.com> wrote:

> Hi,
> how can i send integers between client and server in tcp/ip socket
> programming...
> example: adding of 2 numbers.
> Client takes inputs ( say x and y) and sends them to server ...
> Server does the processing(z=x+y).. and sends the result to client.
> i am able to pass string messages between client and server,but how can
> i pass integers is my question.


Use htonl() or htons() to convert the integer to standard network byte
order, send it, and then use ntohl() or ntohs() on the receiving end to
convert it back to host byte order.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Robert Harris

2006-04-02, 7:42 pm

sai wrote:
> Hi,
> how can i send integers between client and server in tcp/ip socket
> programming...
> example: adding of 2 numbers.
> Client takes inputs ( say x and y) and sends them to server ...
> Server does the processing(z=x+y).. and sends the result to client.
> i am able to pass string messages between client and server,but how can
> i pass integers is my question.
> Regards,
> Sai
>

Client and server have to agree on the integer size and byte order.
There are conventions for agreeing them (e.g.XDR, described in RFC1832
and used by Sun RPC/Portmap) which can easily become overkill if you are
just passing a few bytes.

Robert
Rick Ingham

2006-04-02, 7:42 pm

Barry Margolin wrote:
> In article <1143603045.887269.55200@g10g2000cwb.googlegroups.com>,
> "sai" <aravallisai@yahoo.com> wrote:
>
>
>
>
> Use htonl() or htons() to convert the integer to standard network byte
> order, send it, and then use ntohl() or ntohs() on the receiving end to
> convert it back to host byte order.
>

This works for 16 and 32 bit integers. Are there functions/macros for
64-bit integers?

Just wondering.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com