Hi,
I have a question about socket programming.
I have two pieces of code, one for client and one for server. I have send a string from client side to the server, but server cannot get the string and I don't know what is the problem. Would anyone help me.
client code
Code:
char location_req_msg[1024];
memset(location_req_msg, '0',sizeof(location_req_msg));
char* str="Hello World to binder from client !";
strcpy(location_req_msg,str );
printf("The string in the buffer before client send is:%s\n",location_req_msg); // and it shows correct value
//char location_response_msg[1024];
size_t len = 0;
printf("client_binder_sock %d \n" , client_binder_sock);
// also shows correct value
len = send(client_binder_sock, location_req_msg, strlen(str),0);
// also shows the correct length here
server code
Code:
connection = accept(sock, (struct sockaddr*)&binder_address, &length);
if (connection < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
while(connection)
{
n = recv(sock, readBuffer, 1024, 0);
if (n <= 0)
printf("%d",errno);
else {
printf("%s", "\nsuccess in client read!\n");
std::cout << "\n bytes recieved :" << n << std::endl ;
std::cout << readBuffer << std::endl;
memset(readBuffer, '0',sizeof(readBuffer));
}
connection = accept(sock, (struct sockaddr*)&binder_address, &length);
}
client or server did not show any error.
Please help me why server shows length -1 and there is not any string in this side.