The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
trying to cope with awk difficulties amatuer_lee_3 Shell Programming and Scripting 8 05-11-2008 04:46 PM
a simple chat program kelogs1347 High Level Programming 1 12-07-2006 07:59 AM
Proxy ARP Difficulties TheMaskedMan IP Networking 7 11-02-2005 10:14 AM
may be simple but i don't know -- Print current date from C program ls1429 High Level Programming 6 02-19-2002 01:50 AM
QUESTION...simple program? jj1814 High Level Programming 8 02-07-2002 01:04 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-17-2002
Mistwolf Mistwolf is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 1
Simple Network Program Difficulties

I'm trying to write 2 programs, client & server, that communicate with integers, however, all resources I have found on the net assume that you want to send and recieve information as a character array. I don't want to send my integers as characters, I want to send them as ints (casting them to characters makes them take more space). Does anyone know how to do this? Here is my working code for sending & receiving characters:

char rcvMessageChars[STRING_SIZE]; /* char message */
char sndMessageChars[STRING_SIZE];

write(newsockfd, sndMessageChars, STRING_SIZE);
msgLength = read(newsockfd, rcvMessageChars, STRING_SIZE);


Another quick question - is there any *easy* way to generate random integers in a certain range? i.e. 1-1000 instead of 1-2^16 ?

How about generating random lowercase characters a-z?

Thanks in advance for your help
  #2 (permalink)  
Old 03-17-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Re: Simple Network Program Difficulties

Quote:
Originally posted by Mistwolf
I don't want to send my integers as characters, I want to send them as ints (casting them to characters makes them take more space).
No it doesn't. If your int is, say, 4 bytes long, then casting will give you an array of 4 characters. But see this post for a discussion of the macros you should use.

Quote:

Another quick question - is there any *easy* way to generate random integers in a certain range? i.e. 1-1000 instead of 1-2^16 ?
If "oldrand" is a random number in the range 1 to 2^16 then use something like:
mymax = 1000;
newrand = oldrand*mymax/(2^16);

But take a look at the docs for your random number generator. Is the range 2^16 or 2^15? You want to get this right.

Quote:

How about generating random lowercase characters a-z?
Same problem really:
mymax = (int)'z' - (int)'a' + 1;
newrand = oldrand*mymax/(2^16);
ranchar = (char) ((int)'a' + newrand);
  #3 (permalink)  
Old 03-19-2002
AtleRamsli AtleRamsli is offline
Registered User
  
 

Join Date: Feb 2002
Location: Brabant, Belgium
Posts: 65
I wonder if you speak about two kinds of 'characters' lets say that an array contains

"ABCD" - those are four characters.
If you treat that as an integer you get the number
1145258561, or 0x44434241 - this is probaby what the original poster meant - _converted_ to characters, a number will take more space.
But any sequence of bytes can be seen as anything, char, float, char*, struct foo*,

So, when a network protocol sends 'characters'- you can make those characters mean anything you want, put two together, and you have a 16 bit short, etc.
But beware of how your machine stores integers!
The bytes above may have looked like
"BADC" on some machines to give the same number!
If I am not mistaken, I think the IBM PC is one of them ...

Look at
man htonl
man htons

Then you will understand why it just sends 'characters'- to make it an 'int'you use one of those functions, that puts the bytes in the right 'network'order, and put them back to local 'machine' order.


I often randomize characters with the % operator.

rand() % 10 gives 0-9

rand() % ('z'-'a') + 'a' is also an interesting contruct

Some fun: Remember that characters, int, floats and all that are really just bits.
If you want char is a certain range, maybe you can just chop off some bits!
0123456701234567
1001010011101011
0000111110000000 <- maybe you just want those
-----------------
0000010010000000

This would make it fast by just using one machine op,

AND byte, 000011111

Last edited by AtleRamsli; 03-19-2002 at 07:27 AM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0