Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Byte Handling in Unix C

8 views
Skip to first unread message

Ashit Vora

unread,
Sep 4, 2008, 3:05:52 PM9/4/08
to
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

unread,
Sep 4, 2008, 3:50:20 PM9/4/08
to
Ashit Vora wrote:
> 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. <ot> If 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.

> 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. <ot> Or you could use writev(). </ot>

--
Eric....@sun.com

Ashit Vora

unread,
Sep 4, 2008, 4:26:16 PM9/4/08
to
> 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

unread,
Sep 4, 2008, 5:51:58 PM9/4/08
to
Ashit Vora wrote:
> [...]

> 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>

> The reason y 'm doing this is MY Prof wants it to be this way :(

I doubt it.

--
Eric....@sun.com

Andrew Poelstra

unread,
Sep 4, 2008, 9:09:32 PM9/4/08
to
On Thu, 2008-09-04 at 12:05 -0700, Ashit Vora wrote:
> Hi,
> I had one small query.
> I have a data in char pointer (actually 4 char pointers)

No, if anything you have an address in a char pointer (or
four addresses in four pointers).

> I wanna make one char pointer out of it (without terminating it with
> NULL. so cant use STRCPY).

What does this mean? I'm going to assume you mean "copy the data
pointed to, to contiguous places in memory" but this is not at all
clear.

> 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.
>

Well, this requires a third-party library (probably using a POSIX
implementation would be best), but basically if your send() function
allows you to send a single char at a time, you can do so easily.

If it requires you to send a C string, you will have to NUL-terminate
each character.

Why do you need to send the data one byte at a time?

><snip code>


> also 'm confused how to receive ONE BYTE AT A TIME at the server.

Probably your recv() function will allow you to specify a length.
Set this to 1.

> and dan I again wanna construct the entire string as it is...
>

Come again?

--
Andrew Poelstra <apoe...@wpsoftware.com>
To email me, change .net to .com in the above address.

Ashit Vora

unread,
Sep 5, 2008, 12:47:25 AM9/5/08
to
> Andrew Poelstra             <apoels...@wpsoftware.com>

> To email me, change .net to .com in the above address.

Client's Code
/*construct message*/
buf =(char*) malloc (100);

memset(buf, 0, 100);

memcpy(buf,&type,2);
memcpy(&buf[2],&offset,4);
memcpy(&buf[6],&data_length,4);
memcpy(&buf[10],&string,data_length);

//total_buf_size is the sum of header plus data
total_buf_size=2+4+4+data_length;
/*******************/


/*now sending the data BYTE AT A TIME*/
for(i=0;i<total_buf_size;i++){
if(send(client_sock,&buf[i],1,0)==-1){
perror("send");
exit(1);
}

}
/*************************************/


Server's Code is

/*now listen for client's request*/
while(1){
sin_size = sizeof their_addr;
if ((new_fd = accept(server_sock, (struct sockaddr *)&their_addr,
&sin_size)) == -1) {
perror("accept");
continue;
}
printf("server: got connection from %s\n",
inet_ntoa(their_addr.sin_addr));
if (!fork()) { // this is the child process

printf("IN child process\n");

//first read the packet header
for(i=0;i<10;i++){
if(recv(new_fd,&buf[i],1, 0)==-1){
perror("receive");
exit(1);
}
}

printf("Buf:: %s\n",buf);
//close(sockfd); // child doesn't need the listener
//if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
// perror("send");
//close(new_fd);
//exit(0);
}
//close(new_fd); // parent doesn't need this
printf("NOT in child process\n");
}
/*********************************/


When I checked the data received at the server (I have highlighted the
line)...
I get error BAD ADDRESS. I dont understand wat's wrong here. :(

Jens Thoms Toerring

unread,
Sep 5, 2008, 6:50:01 AM9/5/08
to
Ashit Vora <a.k....@gmail.com> wrote:
> Client's Code
> /*construct message*/
> buf =(char*) malloc (100);

Drop the cast. It helps for nothing but just keeps the
compiler from warning you if you forgot to include
<stdlib.h> which has the declaration of malloc().
And why don't you calculate 'total_buf_size' first
and then allocate exactly as much memory as you need?

> memset(buf, 0, 100);

It's rather likely that this won't do anything useful
and if it's really needed you could use calloc() in-
stea of malloc() to have the buffer zero-ed out.

> memcpy(buf,&type,2);
> memcpy(&buf[2],&offset,4);

Do you realize that '&buf[2]' could be written as 'buf +2'?

> memcpy(&buf[6],&data_length,4);

Looks like that are supposed to be some kind of integers,
right? So the first 10 bytes aren't anything that could
be interpreted as a string?

> memcpy(&buf[10],&string,data_length);

If 'string' is a string (or just a char array) then you
can drop the '&' in front of it.

> //total_buf_size is the sum of header plus data
> total_buf_size=2+4+4+data_length;

> /*now sending the data BYTE AT A TIME*/


> for(i=0;i<total_buf_size;i++){
> if(send(client_sock,&buf[i],1,0)==-1){
> perror("send");
> exit(1);
> }
> }
> /*************************************/

> Server's Code is

> /*now listen for client's request*/
> while(1){
> sin_size = sizeof their_addr;
> if ((new_fd = accept(server_sock, (struct sockaddr *)&their_addr,
> &sin_size)) == -1) {
> perror("accept");
> continue;
> }
> printf("server: got connection from %s\n",
> inet_ntoa(their_addr.sin_addr));
> if (!fork()) { // this is the child process
> printf("IN child process\n");
> //first read the packet header
> for(i=0;i<10;i++){
> if(recv(new_fd,&buf[i],1, 0)==-1){
> perror("receive");
> exit(1);
> }
> }
> printf("Buf:: %s\n",buf);

You have read in 10 bytes. What you did send as the first
10 bytes didn't look like a string at all, so using '%s'
to output these data looks highly suspicious. There may not
even be a zero byte anywhere within the data in which case
printf() would read past the end of the 'buf' array.

> }
> printf("NOT in child process\n");
> }

> When I checked the data received at the server (I have highlighted the


> line)...
> I get error BAD ADDRESS. I dont understand wat's wrong here. :(

Sorry, but I don't see anything "highlighted" and I have no idea
what a "BAD ADDRESS" error is supposed to be (and where you got
it from). If that is an error from calling one of the networking
functions then you better ask about this in comp.unix.programmer
(your program looks a lot as if it's for one of the UNIX variants)
since those aren't standard C functions but extension to C which
aren't on-topic here.
Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

Andrew Poelstra

unread,
Sep 5, 2008, 8:16:07 AM9/5/08
to

Please don't quote signatures.

> Client's Code
> /*construct message*/
> buf =(char*) malloc (100);
>

Don't cast the result of malloc(), don't use malloc() without
first including <stdlib.h>, don't call functions outside of a
function (main()?) or else this will not compile.

> <remaining code snipped>


> When I checked the data received at the server (I have highlighted the
> line)...

You have not highlighted anything. This is a text-based medium.

> I get error BAD ADDRESS. I dont understand wat's wrong here. :(

I highly doubt that's the error you got. If it was, please post
your actual code.

--
Andrew Poelstra <apoe...@wpsoftware.com>

0 new messages