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