Send integer value by "send( )"
Author |
Message |
Jung WonHe #1 / 5
|
 Send integer value by "send( )"
Hello, I just learn unix network programming(and working... :( ) and send some data by send() function. There is no problem send (void *) type data using send() function. But.. how can I transfer int type value? For example unsigned int uiLen = 372; int *pLen = &uiLen; send(client_fd, (void *)pLen, sizeof(uiLen), 0); The value transfered at server is 372, but received value by java client is just 116. Is there any problem using "send()" function? Environment Server : FreeBSD(x86), c language Client : Java protocol : TCP/IP
|
Sun, 12 Sep 2004 15:54:17 GMT |
|
 |
Joona I Palast #2 / 5
|
 Send integer value by "send( )"
Quote: > Hello, > I just learn unix network programming(and working... :( ) and send some > data by send() function. > There is no problem send (void *) type data using send() function. > But.. how can I transfer int type value?
send() is not an ISO C function and therefore your question is off-topic for comp.lang.c. I suggest you ask in comp.unix.programmer. Quote: > For example > unsigned int uiLen = 372; > int *pLen = &uiLen; > send(client_fd, (void *)pLen, sizeof(uiLen), 0); > The value transfered at server is 372, but received value by java client is > just 116. > Is there any problem using "send()" function?
<OT> Not necessarily. Your C program and your Java program might be using different byte-by-byte encodings of the int values. To be safe, send and read one byte at a time, and then build the int values from the bytes. </OT> Quote: > Environment > Server : FreeBSD(x86), c language > Client : Java > protocol : TCP/IP
If you need to state which operating system you are using, there's a good chance you are off-topic for comp.lang.c. --
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++| | http://www.helsinki.fi/~palaste W++ B OP+ | \----------------------------------------- Finland rules! ------------/ "Hasta la Vista, Abie!" - Bart Simpson
|
Sun, 12 Sep 2004 16:29:11 GMT |
|
 |
Martin Ambuh #3 / 5
|
 Send integer value by "send( )"
Quote:
> I just learn unix network programming(and working... :( ) and send some > data by send() function. > There is no problem send (void *) type data using send() function. > But.. how can I transfer int type value? > For example > unsigned int uiLen = 372; > int *pLen = &uiLen; > send(client_fd, (void *)pLen, sizeof(uiLen), 0); > The value transfered at server is 372, but received value by java client is > just 116. > Is there any problem using "send()" function?
We really can't help you with network programming or your send() function here, because neither networks nor a send() function are anywhere in the definition of C or its standard libraries. Howerver, 372-116 == 256, which suggest that your function does not operate on values greater than 255 (0377 or 0xFF). This suggests that it operates on unsigned chars, not unsigned ints. If your unsigned char is 8 bits, they could never have a value 372. Perhaps on the receiving end you are not an the incoming values, and the next one might just hold the missing bit (0001 or 0x01). -- It is better that the grammarians should chide us than that the people should not understand us. - St. Augustine
|
Sun, 12 Sep 2004 18:03:42 GMT |
|
 |
Tigr #4 / 5
|
 Send integer value by "send( )"
Quote: > unsigned int uiLen = 372; > int *pLen = &uiLen; > send(client_fd, (void *)pLen, sizeof(uiLen), 0); > The value transfered at server is 372, but received value by java client is > just 116. > Is there any problem using "send()" function?
Look for htonl(3) and ntohl(3) functions. BTW, I'm not sure this question has anything in common with the C language itself. Cheers, Sergey
|
Sun, 12 Sep 2004 18:07:18 GMT |
|
 |
raghavendra. #5 / 5
|
 Send integer value by "send( )"
Quote:
> Hello, > I just learn unix network programming(and working... :( ) and send some > data by send() function. > There is no problem send (void *) type data using send() function. > But.. how can I transfer int type value? > For example > unsigned int uiLen = 372; > int *pLen = &uiLen; > send(client_fd, (void *)pLen, sizeof(uiLen), 0); > The value transfered at server is 372, but received value by java client is > just 116. > Is there any problem using "send()" function? > Environment > Server : FreeBSD(x86), c language > Client : Java > protocol : TCP/IP
well, it's something that depends on the machine. lets make it clear. once u asign 372 to some unsigned int variable, it is stored in following format higher byte lower byte 00000001 01110100 right. Now observe that the lower byte's magnitude is 116. now i strongly suspect that ur server and client are of diffrent architectures. Hence the value which was 372 in server is treated as 116 in the client machine. thats all raghavendra.sm
|
Sun, 12 Sep 2004 19:30:37 GMT |
|
|
|