Hi everyone!
I'm doing my project related to socket programming. The send and receive part I got problem.
The code is below :
Server side:
printf("Handling client %s\n",inet_ntoa(their_addr.sin_addr));
if (send(new_fd, "Menu\n", 5, 0) == -1) //(1)
perror("send");
if(send(new_fd, "1. A.mp3, 2. B.mp3, C.mp3\n", 26,0) == -1) //(2)
perror("send");
if(send(new_fd, "Which file to play?\n", 20, 0) == -1) //(3)
perror("send");
Client side :
buf[numbytes] = '\0';
printf("%s",buf);
printf("Enter your song number : "); //(4)
fflush(stdin);
gets(choice);
I mark the 4 lines for shorter writing.
Sometimes when server sends messages, the client can receive full of them(mean in the order (1) to (4) ). But sometimes it just receive the (1) line, then (4) and after that is (2) and (3). When this happens, I must close the server and client, run again, then it works.
My teacher said that this is caused by buffer. But exactly what I should do to make sure everytime the client will receive (1) to (4) respectively?
Thank for your help.