Client/Server using send() and recv()

Hi I am trying to do multiple send’s, but the server only recives the first send and displays the second send as empty or garbage:

Client code: (C++ code in Windows 2000 Professional)
char ch[3] = “AB”;
char ch2[2] = “w”;
char *ptr = ch;
char *ptr2 = ch2;
send(sd, ptr1, 3, 0);
send(sd, ptr2, 2, 0);

Server code: (C code on QNXOS)
char buff[3];
char buff2[2];
int len1,len2;
len1 = recv(ns,&buff,3,0);
len2 = recv(ns,&buff2,2,0);
printf(“Data1: %s\n”, &buff);
printf(“Data1: %\ns”, &buff2);
printf(“Len1: %i\n”, len1);
printf(“Len2: %i\n”, len2);

Output on Server:
Data1: AB
Data2: 2AB
Len1: 3
Len2: -1

NB: no idea how buff2 gets AB … infact the 2 is followed by the degree symbol and then AB. [smart garbage]

I would appreciate any help regarding the same.

Thanks
ashu

This is not a QNX issue.

For the server code it’s better:

char ch[] = “AB”;
char ch2[] = “w”;
send(sd, ch, sizeof ( ch ), 0);
send(sd, ch2, sizeof ( w ), 0);

You should check the return value of send…

For the client code:

char buff[3];
char buff2[2];
int len1,len2;
len1 = recv(ns,&buff,sizeof( buff),0);
len2 = recv(ns,&buff2,sizeof(buff2),0);
printf(“Data1: %s\n”, &buff);
printf(“Data1: %\ns”, &buff2);
printf(“Len1: %i\n”, len1);
printf(“Len2: %i\n”, len2);

As for buff2 containing AB if the recv failed (as indicated by -1) the contant of buff2 is left as is (garbaged)

I don’t see why in your code the second recv failed. However NEVER expect recv to be of same size as send, TCP/IP is a STREAMING protocol and not a PACKET protocol.

If send or recv return -1 you should check errno for more clue as to what is going on.

Hi,

I had tried that before, but as you had mentioned i re-tried that. It still gives me the same problem.

But I also realised a stange behavior … if my second send has 1460 as the sizefield … i can recived it at the server side. I knew this but didnt put it in the first post as I thought i was doing something wrong.

send(sd, ch, sizeof ( ch ), 0);
send(sd, ch2, 1460, 0); //basically more than 1460

So it does transfer the second send() only when the size is more than 1460 on client side. I did not change anything on the server side. This is weird ???

I would appreciate any help on this.