Connecting Tech Pros Worldwide Help | Site Map

Re: Byte Handling in Unix C

  #1  
Old September 5th, 2008, 02:15 AM
Andrew Poelstra
Guest
 
Posts: n/a
On Thu, 2008-09-04 at 12:05 -0700, Ashit Vora wrote:
Quote:
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).
Quote:
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.
Quote:
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?
Quote:
><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.
Quote:
and dan I again wanna construct the entire string as it is...
>
Come again?

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

  #2  
Old September 5th, 2008, 05:55 AM
Ashit Vora
Guest
 
Posts: n/a

re: Re: Byte Handling in Unix C


On Sep 4, 6:09*pm, Andrew Poelstra <apoels...@wpsoftware.netwrote:
Quote:
On Thu, 2008-09-04 at 12:05 -0700, Ashit Vora wrote:
Quote:
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).
>
Quote:
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.
>
Quote:
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?
>
Quote:
<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.
>
Quote:
and dan I again wanna construct the entire string as it is...
>
Come again?
>
--
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. :(
  #3  
Old September 5th, 2008, 11:55 AM
Jens Thoms Toerring
Guest
 
Posts: n/a

re: Re: Byte Handling in Unix C


Ashit Vora <a.k.vora@gmail.comwrote:
Quote:
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.hwhich 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?
Quote:
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.
Quote:
memcpy(buf,&type,2);
memcpy(&buf[2],&offset,4);
Do you realize that '&buf[2]' could be written as 'buf +2'?
Quote:
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?
Quote:
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.
Quote:
//total_buf_size is the sum of header plus data
total_buf_size=2+4+4+data_length;
Quote:
/*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);
}
}
/*************************************/
Quote:
Server's Code is
Quote:
/*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.
Quote:
}
printf("NOT in child process\n");
}
Quote:
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 ___ jt@toerring.de
\__________________________ http://toerring.de
  #4  
Old September 5th, 2008, 01:25 PM
Andrew Poelstra
Guest
 
Posts: n/a

re: Re: Byte Handling in Unix C


On Thu, 2008-09-04 at 21:47 -0700, Ashit Vora wrote:
Quote:
On Sep 4, 6:09 pm, Andrew Poelstra <apoels...@wpsoftware.netwrote:
Quote:
On Thu, 2008-09-04 at 12:05 -0700, Ashit Vora wrote:
Quote:
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).
Quote:
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.
Quote:
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?
Quote:
><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.
Quote:
and dan I again wanna construct the entire string as it is...
Come again?

--
Andrew Poelstra <apoels...@wpsoftware.com>
To email me, change .net to .com in the above address.
>
Please don't quote signatures.
Quote:
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.
Quote:
<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.
Quote:
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 <apoelstra@wpsoftware.com>
To email me, change .net to .com in the above address.

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::ios::binary? Steven T. Hatton answers 103 August 7th, 2005 03:05 PM
bytecode non-backcompatibility Maurice LING answers 33 July 19th, 2005 01:47 AM