![]() |
|
|
|
|
|||||||
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
||||
|
Re: Simple Network Program Difficulties
Quote:
Quote:
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:
mymax = (int)'z' - (int)'a' + 1; newrand = oldrand*mymax/(2^16); ranchar = (char) ((int)'a' + newrand); |
|
|||
|
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. |
|||
| Google UNIX.COM |
| Thread Tools | |
| Display Modes | |
|
|