PDA

View Full Version : how to send a integer over socket?


dev
04-23-2003, 08:11 AM
hi all,

how can we send an int value using send?
when i do so by (send(c,(void *)&a,2,0)) , where a is int val and c is the connected socket, it doesnot send the right val, so should i use htons and ntohs?

dev

shiva
04-23-2003, 01:01 PM
Just use the send system call

/* This piece of code shall do the send */
int value=0;
send(fd,(char*)&value, sizeof(int));

where
fd ---> sockfd
value-> value of an int
sizeof the int to be spceified

-Shiva[/code]

dev
04-23-2003, 01:09 PM
(char *)&value --------->
&value will give the address of the variable value, will not it?, so how can it work?

emihaly
04-23-2003, 02:38 PM
int n;

n=555;

if(send(socket,&n,4,0) <4)
{
// we have problem, must use connect before it!??
}

close(socket);
-----

int n;
if(recv(socket,&n,4,0)) < 4)
{

cycle until got 4
}

close
print(n)

emihaly
04-23-2003, 02:40 PM
and in reality, this is my source code form my project.

.
.
.
.
if(ReadTimeOut(sp[i].socket))
{
rt=recv(sp[i].socket,&sp[i].prot_size,4,0);
if (rt < 4 || sp[i].prot_size > 200000) goto FINISH;
if (sp[i].prot_size > 100000)
sp[i].prot_size = sp[i].prot_size - 100000;
if (sp[i].prot_size > 75000) goto FINISH;

sp[i].sended = 0;
sp[i].last = time(NULL);
sp[i].flag = 75;
continue;
}
else
.
.
.

RobSeace
04-23-2003, 08:05 PM
Note: all of the above assumes the receiver is going to be
on the same architecture as you... If they're not, there might
be byte-ordering issues, so yes you SHOULD use htonl()
and ntohl()... Eg:


int32_t i;

i = htonl (real_val);
send (fd, &i, sizeof (i), 0);

/* and in the receiver... */

recv (fd, &i, sizeof (i), 0);
real_val = ntohl (i);

mlampkin
04-24-2003, 06:18 AM
The next question someone asks is how to send an array of integers over a socket... and I notice you jumped in and answered (Rob) before I could climb up on the ole soap box and start giving my speech(es) about coding for proper cross platform systems... lol. :-P

Michael

dev
04-24-2003, 08:45 AM
hi all,

i am a bit confused, some are sending sizeof(int) as 3rd parameter and some 4,
why 4? and that too loop till we read 4 bytes in recv?

dev

RobSeace
04-24-2003, 12:37 PM
Huh? Who has it as the 4th argument?? The 4th arg is flags,
so sizeof(int) wouldn't make a lot of sense there...

RobSeace
04-24-2003, 12:46 PM
Oh, wait, I think my coffee just kicked in, and I now understand
what you meant... ;-) You mean some people are using the value
"4" in place of "sizeof(int)"... Yeah... Well, that's just stupid and
wrong, too... ;-) People who hard-code magic values like that
really need to be beaten repeatedly with large, blunt objects, I
always say... ;-) Yes, on a 32-bit system sizeof(int) == 4... So,
it'll work fine... But, you still shouldn't do it... Also, you probably
don't really want to be using plain old "int"s, when sending values
around either, since sizeof(int) is explicitly undefined, and up to
the individual system to decide on... That's why my posted code
above explicitly used "int32_t"... (But, even then, I say it's just
ugly, confusing, and stupid to use a hard-coded "4" for the size,
even when you KNOW it's a fixed 32-bit value... Just use
sizeof(int32_t) (or, sizeof(variable)); it makes the code so much
clearer, as to what you're trying to do...)

dev
04-24-2003, 01:14 PM
hi Rob,

that was a really humrous reply, thanks.

tell me bout int32_t , is it available in gcc for red hat 7.2?
and how is it different from long?

dev

RobSeace
04-24-2003, 08:02 PM
Yeah, all the bit-size ints are defined in glibc headers, somewhere...
(<sys/types.h>, I believe...) Those are fairly standard, these
days... I'm not sure if ISO/ANSI/someone-else made them a
requirement at some point, but I suspect so... (Michael, the
spec-meister would probably know... ;-))

How is it different from a long? Well, on a 32-bit platform, not
at all... It's just a typedef for whatever the appropriate native
type is to represent the given fixed-size value... On 32-bit
platforms, that'd be either "int" or "long"; but, on 64-bit platforms,
a long would almost certainly be a 64-bit value, so int32_t would
be whatever the native 32-bit integer type is (probably just "int")...

emihaly
04-28-2003, 03:10 PM
i think, int have 4 bytes on all 32 bit systems. And for me is much better
write 4 like sizeof(int); Have less chars on paper when i debug code :)

Rob, you are perfect example, how someone can write 20 lines about one small sizeof(int) <> 4 :) Best for book authors , pay usd(s)/per pages
:D

RobSeace
04-28-2003, 07:01 PM
Well, it depends on what you mean by a 32-bit system, I
suppose... I certainly know of systems which are natively
32-bit, but for which you can still compile and run 16-bit code...
And, for those, "int" would almost certainly be 16-bit, not 32-bit... ;-)
(One such system I know of for sure, right offhand, is QNX4...)
But, yes, on a 32-bit system running only pure 32-bit code, then
yes, I think "int" will always be 32-bits (4 bytes)... But, still, it's
just plain wrong to assume so in any real code...

(You know, I think you may be right: I am starting to turn into
Michael! AAIIEE!! ;-))