> fflush(stdin);
> gets(choice);
Nevermind the network stuff, how about getting the basics right?
fflush(stdin);
gets()
> send(new_fd, "Menu\n", 5, 0)
0, 1, 2, 3, 4 are also valid return results. You can't just assume that if it wasn't an error (-1), that the only other possible result must have been 5. If it wasn't 5, then it's YOUR job to call send() again with the remaining data.
> buf[numbytes] = '\0';
Show us how you declared buff, and how you called recv() to fill that buffer.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
If you have
char buf[] = "hello";
int len = strlen( buff );
And you do n = send ( fd, buff, len, 0 );
You also need to do if ( n < len ) {
n = send( fd, buff[n], len-n, 0 );
}
Actually, use a while loop, as in
char buf[] = "hello";
int len = strlen( buf );
char *bp = buf;
while ( len > 0 ) {
n = send( fd, bp, len, 0 );
if ( n > 0 ) {
// at least partly successful, advance through the buffer
len -= n;
bp += n;
} else if ( n == 0 ) {
// connection closed
} else {
// an error
}
}
Over the loopback, or just over your LAN, you're unlikely to see this happen with small packets. But generally speaking, it's something you need to take care of.
The same is true of the recv(). The message may become fragmented, and it's up to you to reassemble it (say by looking for a \n).
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
I've no idea.
Being a stream, you either get stuff in the order you sent it, or you don't get it at all.
Like I said, make sure you're actually sending all the data with each send() call.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953