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