The UNIX and Linux Forums  

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 01:46 PM
a simple chat program kelogs1347 High Level Programming 1 12-07-2006 03:59 AM
Proxy ARP Difficulties TheMaskedMan IP Networking 7 11-02-2005 06:14 AM
may be simple but i don't know -- Print current date from C program ls1429 High Level Programming 6 02-18-2002 09:50 PM
QUESTION...simple program? jj1814 High Level Programming 8 02-07-2002 09:04 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-17-2002
Registered User
 

Join Date: Mar 2002
Posts: 1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-17-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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);
Reply With Quote
  #3 (permalink)  
Old 03-19-2002
Registered User
 

Join Date: Feb 2002
Location: Brabant, Belgium
Posts: 65
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
__________________
PS
All of the above is to be read as '... unless I am wrong'
ENDPS

Last edited by AtleRamsli; 03-19-2002 at 03:27 AM.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash for loop boot: cannot open kernel/sparcv9/unix command copy/move folder in unix curses.h cut command in unix daemon process export command in unix find grep find mtime find null character in a unix file glance unix grep multiple lines grep or grep recursive inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime perl array length ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com vi substitute while loop within while loop shell script


All times are GMT -7. The time now is 12:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101