heras
03-08-2008, 03:51 PM
[edit] Why must server > client[message]?
Hi,
My client tries to send 'ping' to my server.
client:
int main()
{
int sockfd;
struct sockaddr_in server_address;
char server_ip[16];
char *message = "ping";
//stuff
send(sockfd, message, strlen(message) + 1, 0);
server:
char buffer[smaller than 8];
//stuff
recv(new_fd, buffer, strlen(message);
printf("%s\n", buffer);
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?)
With
send(sockfd, message, strlen(message), 0);
without the [b]+ 1 the message gets sent and printed, plus some garbage characters. Why is this?
If the entire code is needed, which is not that big, please let me know.
Slightly off-topic:
What do you think about
if (connect(sockfd, (struct sockaddr *) &servaddr,
sizeof(servaddr)) < 0) {
fprintf(stderr, "connect error: %s\n", strerror(errno));
exit(EXIT_FAILURE);
versus
if ((connect(sockfd, (struct sockaddr *)&server_address, \
sizeof server_address)) == -1) {
perror("Connection error: ");
exit(EXIT_FAILURE);
? I think the question is how to do propper error handling. I see much examples but do not understand the difference.
Please note that I'm a beginner amongst beginners.
Thanks,
heras
Hi,
My client tries to send 'ping' to my server.
client:
int main()
{
int sockfd;
struct sockaddr_in server_address;
char server_ip[16];
char *message = "ping";
//stuff
send(sockfd, message, strlen(message) + 1, 0);
server:
char buffer[smaller than 8];
//stuff
recv(new_fd, buffer, strlen(message);
printf("%s\n", buffer);
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?)
With
send(sockfd, message, strlen(message), 0);
without the [b]+ 1 the message gets sent and printed, plus some garbage characters. Why is this?
If the entire code is needed, which is not that big, please let me know.
Slightly off-topic:
What do you think about
if (connect(sockfd, (struct sockaddr *) &servaddr,
sizeof(servaddr)) < 0) {
fprintf(stderr, "connect error: %s\n", strerror(errno));
exit(EXIT_FAILURE);
versus
if ((connect(sockfd, (struct sockaddr *)&server_address, \
sizeof server_address)) == -1) {
perror("Connection error: ");
exit(EXIT_FAILURE);
? I think the question is how to do propper error handling. I see much examples but do not understand the difference.
Please note that I'm a beginner amongst beginners.
Thanks,
heras