hi,
im getting recv:bad address error in the server code when im receiving the data from the client.i tried passing the structure and an integer alone ,but for both i got the same error.

for structures:
client:

struct Data
        {
                char data1[255];
                char data2[255];
                int val1;
       };
struct Data myData = { "Let us C", "YPK", 101 } ;
int ch = send(create_socket,&myData,sizeof(struct Data), 0);
struct Data
                {
                        char data1[255];
                        char data2[255];
                        int val1;
                };
void *dptr1;
struct Data *dptr;
struct Data *dptr = (struct Data*)(dptr1);
printf("The Elements of structure \n");
printf("Book- %s Author- %s Code- %d\n",dptr->data1,dptr->data2,dptr->val1);

for integer data:
client:

int a=5;
int ch = send(create_socket,&a,sizeof(int), 0);

server:

void *ptr;
 int *iptr;
int ch=recv(new_socket,ptr,sizeof(int),0);
iptr = (int*)(ptr); 
printf("Value of a is :%d\n",*iptr);

Recommended Answers

All 3 Replies

did you try:

int i=0;
int ch=recv(new_socket,(void*)&i,sizeof(int),0);
printf("Value of a is :%d\n", i);

did you try:

int i=0;
int ch=recv(new_socket,(void*)&i,sizeof(int),0);
printf("Value of a is :%d\n", i);

thanx programmersbook ,it worked !!

first of all sorry,as instead of posting this ques in C-Forum i posted in C++ ...

i cant understand what is wrong in my approach of receiving the datas in the recv(),as i declare one void pointer and typecast it to approp pointer before dereferencing and it worked for quite a no of times and then it was not workin..
so,is my way of getting datas using pointers is wrong?


if i go by ur way, can u plz xplain how come "&i" (where i is a local variable in server code) get the value from client side or how value is passed from client's send() to server's recv().

thanx !!

You welcome!

if you have it like this:

void *ptr;

and give it to send, send will complain, because behind ptr is no memory allocated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.