Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 4th, 2008, 08:15 PM
Ashit Vora
Guest
 
Posts: n/a
Default Byte Handling in Unix C

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
  #2  
Old September 4th, 2008, 08:55 PM
Eric Sosman
Guest
 
Posts: n/a
Default 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
  #3  
Old September 4th, 2008, 09:35 PM
Ashit Vora
Guest
 
Posts: n/a
Default 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 :(
  #4  
Old September 4th, 2008, 10:55 PM
Eric Sosman
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles