[edit] Why must server[buffer] > client[message]?
Hi,
My client tries to send 'ping' to my server.
client:
server:Code:int main() { int sockfd; struct sockaddr_in server_address; char server_ip[16]; char *message = "ping"; //stuff send(sockfd, message, strlen(message) + 1, 0);
With a buffer smaller than 8 the server only prints a new line, even though I'm only expecting 'ping' + '\0', 5 characters. With 8 or more 'ping' is received and printed succesfully. Is there still some header that needs to be stripped at this point? (or other?)Code:char buffer[smaller than 8]; //stuff recv(new_fd, buffer, strlen(message); printf("%s\n", buffer);
With
without the + 1 the message gets sent and printed, plus some garbage characters. Why is this?Code:send(sockfd, message, strlen(message), 0);
If the entire code is needed, which is not that big, please let me know.
Slightly off-topic:
What do you think about
versusCode:if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { fprintf(stderr, "connect error: %s\n", strerror(errno)); exit(EXIT_FAILURE);
? I think the question is how to do propper error handling. I see much examples but do not understand the difference.Code:if ((connect(sockfd, (struct sockaddr *)&server_address, \ sizeof server_address)) == -1) { perror("Connection error: "); exit(EXIT_FAILURE);
Please note that I'm a beginner amongst beginners.
Thanks,
heras



LinkBack URL
About LinkBacks


