hey guys! im struggling for the past couple of days with a socket problem.
im trying to master the basics in order to proceed and integrate this mechanism in my program which is supposed to run on a server.
basically the problem is weird and there's couple of parts to it that i quite cant understand, mainly in the client program.
in my client code i run an infinite while loop with 1 send() and 1 recv() - straightforward and simple; server is supposed to recv() and send() back to the client a string.
but here's what bothers me:
first of all when running client - the server receives only the first message; after that the strings wont be sent..
second - the while loop that is supposed to be infinite - terminates after 3 iterations..
any help related to this issue would be appreciatedCode:int main (int argc, char **argv) { int sock; struct sockaddr_in cudaserver; char buffer[BUFFSIZE]; unsigned int cudalen; int received = 0; msg temp_msg; if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { Die("Failed to create socket"); } memset(&cudaserver, 0, sizeof(cudaserver)); cudaserver.sin_family = AF_INET; cudaserver.sin_addr.s_addr = inet_addr(argv[1]); cudaserver.sin_port = htons(atoi(argv[2])); while (connect(sock,(struct sockaddr *) &cudaserver,sizeof(cudaserver)) < 0) { Die("Failed to connect with server"); } while (1) { printf("input the word to send to server\n"); scanf("%s",&buffer); cudalen = strlen(buffer); if (send(sock, buffer, cudalen, 0) != cudalen) { Die("Mismatch in number of sent bytes"); } while (received > 0) { int bytes = 0; if ((bytes = recv(sock, buffer, BUFFSIZE-1, 0)) < 1) { Die("Failed to receive bytes from server"); } received += bytes; buffer[bytes] = '\0'; fprintf(stdout, buffer); } } close (sock); exit(0); }
P.S. btw, gdb says that "Program received signal SIGPIPE, Broken pipe." (if that helps anyhow)



2Likes
LinkBack URL
About LinkBacks



