Connecting Tech Pros Worldwide Help | Site Map

Byte Handling in Unix C

Ashit Vora
Guest
 
Posts: n/a
#1: Sep 4 '08
Hi,
I had one small query.
I have a data in char pointer (actually 4 char pointers)
I wanna make one char pointer out of it (without terminating it with
NULL. so cant use STRCPY).
I used memcpy() to do so.
Now I have to send that data to the server using socket prog but ONE
BYTE AT A TIME.

I wrote this code but donno if 'm going right way or not.


memset(buffer,0,bufsize);
memcpy(buffer,&type,2);
memcpy(&buffer[2],&offset,4);
memcpy(&buffer[6],&datalength,4);
memcpy(&buffer[10],data,datalength);


for(i=0;i<bufSize;i++)
{
if(send(sockfd,&buffer[i],1,0)==-1)
{
perror("send");
}

}


also 'm confused how to receive ONE BYTE AT A TIME at the server.
and dan I again wanna construct the entire string as it is...

Thanks
Eric Sosman
Guest
 
Posts: n/a
#2: Sep 4 '08

re: Byte Handling in Unix C


Ashit Vora wrote:
Quote:
Hi,
I had one small query.
I have a data in char pointer (actually 4 char pointers)
I wanna make one char pointer out of it (without terminating it with
NULL. so cant use STRCPY).
I used memcpy() to do so.
Now I have to send that data to the server using socket prog but ONE
BYTE AT A TIME.
>
I wrote this code but donno if 'm going right way or not.
>
>
memset(buffer,0,bufsize);
memcpy(buffer,&type,2);
memcpy(&buffer[2],&offset,4);
memcpy(&buffer[6],&datalength,4);
memcpy(&buffer[10],data,datalength);
>
>
for(i=0;i<bufSize;i++)
{
if(send(sockfd,&buffer[i],1,0)==-1)
{
perror("send");
}
>
}
This looks plausible, assuming the send() call transmits one
byte. <otIf it's the same send() I'm familiar with, you could
tell it to transmit the whole thing with just one call. </ot>

As written, you'll send extra zero bytes if bufsize>datalength+10;
did you really mean to do that? Also, there'll be big trouble if
bufsize<datalength+10 ... And it's a little surprising that when
an error occurs you report it but otherwise just plow straight
ahead as if nothing unusual had occurred.
Quote:
also 'm confused how to receive ONE BYTE AT A TIME at the server.
and dan I again wanna construct the entire string as it is...
You could just reverse the process: Read bufsize bytes and
plop them into a buffer, then memcpy the bytes from the buffer to
the individual variables.

For both the sending and the receiving, it might make more
sense to eliminate the buffer and do the I/O directly from or to
the individual variables. You could do this conveniently by
building a little table of pointers and lengths for each sub-
piece, with an outer loop that traverses the pieces and an inner
loop for each piece's bytes. <otOr you could use writev(). </ot>

--
Eric.Sosman@sun.com
Ashit Vora
Guest
 
Posts: n/a
#3: Sep 4 '08

re: Byte Handling in Unix C


On Sep 4, 12:50*pm, Eric Sosman <Eric.Sos...@sun.comwrote:
Quote:
Ashit Vora wrote:
Quote:
Hi,
I had one small query.
I have a data in char pointer (actually 4 char pointers)
I wanna make one char pointer out of it (without terminating it with
NULL. so cant use STRCPY).
I used memcpy() to do so.
Now I have to send that data to the server using socket prog but ONE
BYTE AT A TIME.
>
Quote:
I wrote this code but donno if 'm going right way or not.
>
Quote:
* * * * memset(buffer,0,bufsize);
* *memcpy(buffer,&type,2);
* *memcpy(&buffer[2],&offset,4);
* *memcpy(&buffer[6],&datalength,4);
* *memcpy(&buffer[10],data,datalength);
>
Quote:
* * * * for(i=0;i<bufSize;i++)
* *{
* * * * * *if(send(sockfd,&buffer[i],1,0)==-1)
* * * * * *{
* * * * * * * * * *perror("send");
* * * * * *}
>
Quote:
* *}
>
* * *This looks plausible, assuming the send() call transmits one
byte. *<otIf it's the same send() I'm familiar with, you could
tell it to transmit the whole thing with just one call. </ot>
>
* * *As written, you'll send extra zero bytes if bufsize>datalength+10;
did you really mean to do that? *Also, there'll be big trouble if
bufsize<datalength+10 ... *And it's a little surprising that when
an error occurs you report it but otherwise just plow straight
ahead as if nothing unusual had occurred.
>
Quote:
also 'm confused how to receive ONE BYTE AT A TIME at the server.
and dan I again wanna construct the entire string as it is...
>
* * *You could just reverse the process: Read bufsize bytes and
plop them into a buffer, then memcpy the bytes from the buffer to
the individual variables.
>
* * *For both the sending and the receiving, it might make more
sense to eliminate the buffer and do the I/O directly from or to
the individual variables. *You could do this conveniently by
building a little table of pointers and lengths for each sub-
piece, with an outer loop that traverses the pieces and an inner
loop for each piece's bytes. *<otOr you could use writev(). </ot>
>
--
Eric.Sos...@sun.com
Hi Eric,

It will never.
I should have added one more line to make it more clear...

buf_length=datalength+10

so I know inadvance how much data is to be send.

The reason behind sending one byte at a time is....
I dont wanna have a large buffer at the receiver end.

this is a case of simply sending a small data but in case I wanna send
10GB file, dan it should be better to send one byte at a time.
Let server receive and process one byte at a time.

The reason y 'm doing this is MY Prof wants it to be this way :(
Eric Sosman
Guest
 
Posts: n/a
#4: Sep 4 '08

re: Byte Handling in Unix C


Ashit Vora wrote:
Quote:
[...]
The reason behind sending one byte at a time is....
I dont wanna have a large buffer at the receiver end.
>
this is a case of simply sending a small data but in case I wanna send
10GB file, dan it should be better to send one byte at a time.
Let server receive and process one byte at a time.
You should probably take further questions on this matter to
a different forum: comp.unix.programmer, perhaps. Since C has no
built-in networking capabilities but relies on whatever the platform
provides (if anything), questions about how networking operates are
questions about the platforms, not about C.

<off-topic>

Doing a separate send() for each byte does not guarantee that
the bytes cross the wire one by one. The network stack may well
combine the bytes from many send() calls into a single transmission.
For details, visit another forum.

</off-topic>
Quote:
The reason y 'm doing this is MY Prof wants it to be this way :(
I doubt it.

--
Eric.Sosman@sun.com
Closed Thread


Similar C / C++ bytes