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

Dani AI

Generated

The "Bad address" comes from handing recv a pointer that does not point to valid, writable memory. In your server you declared void *ptr; (and similarly dptr1) but never made it point anywhere. Sometimes you got lucky because the indeterminate value happened to be non-crashing, but it is undefined behavior. ’s fix works because &i is the address of a real object that recv can write into.

Two further essentials when receiving:

  • recv may return fewer bytes than you asked for; loop until you have them or the peer closes.
  • Convert integers to/from network byte order so the value is correct on all architectures.

Example for a 32-bit integer:

// receive exactly n bytes
ssize_t recv_all(int fd, void *buf, size_t n) {
    size_t off = 0;
    while (off < n) {
        ssize_t r = recv(fd, (char *)buf + off, n - off, 0);
        if (r == 0) return off;          // peer closed
        if (r < 0) { if (errno == EINTR) continue; return -1; }
        off += (size_t)r;
    }
    return (ssize_t)off;
}

// server side
uint32_t net;
if (recv_all(new_socket, &net, sizeof net) != sizeof net) { /* handle error */ }
int a = (int)ntohl(net);
printf("Value of a is: %d\n", a);

For structs, avoid sending the raw struct because of padding/alignment and endianness. Define a stable wire format (e.g., two fixed 255-byte char arrays followed by a uint32_t), copy strings safely, and send the integer in network byte order. On receive, recv_all into a properly sized local variable or allocated buffer, then ntohl the integer field. If you still see "Bad address," double-check that your destination pointer is non-NULL, points to accessible storage of the requested size, and that len is not negative or absurd. On Windows the same issue surfaces as WSAEFAULT.

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.