I have A TCP Server-Client where I enter a number with the number of times that I want to send the word to the server. So I send correctly because I print the information on a loop(in client) but in the server shows infinitely because I put while(1) but if I don’t use just print it one time. I don’t want to send the number to the server.
If I enter the number 4 I want to print the word “hello” four times This is the server, now it’s printing infinitely obviously.
JavaScript
x
#define MAXPENDING 5 /* Maximum number of simultaneous connections */
#define BUFFSIZE 255 /* Size of message to be received */
void err_sys(char *mess) { perror(mess); exit(1); }
void handle_client(int sock) {
char buffer[BUFFSIZE];
int received = -1;
while (1) {
/* Read from socket */
read(sock, &buffer[0], BUFFSIZE);
printf("Message from client: %sn", buffer);
/* Write to socket */
write(sock, buffer, strlen(buffer) + 1);
/* Close socket */
close(sock);
}
}
int main(int argc, char *argv[]) {
struct sockaddr_in echoserver, echoclient;
int serversock, clientsock;
int result;
/* Check input arguments */
if (argc != 2) {
fprintf(stderr, "Usage: %s <port>n", argv[0]);
exit(1);
}
/* Create TCP socket */
serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serversock < 0) {
err_sys("Error socket");
}
/* Set information for sockaddr_in structure */
memset(&echoserver, 0, sizeof(echoserver)); /* we reset memory */
echoserver.sin_family = AF_INET; /* Internet/IP */
echoserver.sin_addr.s_addr = htonl(INADDR_ANY); /* ANY address */
echoserver.sin_port = htons(atoi(argv[1])); /* server port */
/* Bind socket */
result = bind(serversock, (struct sockaddr *) &echoserver, sizeof(echoserver));
if (result < 0) {
err_sys("Error bind");
}
/* Listen socket */
result = listen(serversock, MAXPENDING);
if (result < 0) {
err_sys("Error listen");
}
while (1) {
unsigned int clientlen = sizeof(echoclient);
/* Wait for a connection from a client */
clientsock = accept(serversock, (struct sockaddr *) &echoclient, &clientlen);
if (clientsock < 0) {
err_sys("Error accept");
}
fprintf(stdout, "Client: %sn", inet_ntoa(echoclient.sin_addr));
/* Call function to handle socket */
handle_client(clientsock);
}
}
This is the client
JavaScript
printf("Enter a number between 0 to 9[0 to exit]: ");
fgets(number, 100, stdin);
while(strtol(number,&pEnd,10)!=0){
while (!((strtol(number,&pEnd,10) <= 9) && (strtol(number,&pEnd,10) >= 0))) {
printf("[ERROR] The number you entered is out of rangen");
printf("Enter a number between 0 to 9[0 to exit]: ");
fgets(number, 100, stdin);
}
if(strtol(number,&pEnd,10)!=0){
printf("Enter a word: ");
fgets(buffer, 100, stdin);
for(int i=0;i<strtol(number,&pEnd,10);i++){
write(sock, buffer, strlen(buffer) + 1);
fprintf(stdout, " sent n");
read(sock, buffer, BUFFSIZE);
fprintf(stdout, " %s ...done n", buffer);
}
}
printf(" Enter a number between 0 to 9[0 to exit]: ");
fgets(number, 100, stdin);
}
Advertisement
Answer
There are multiple problems in you code:
- you do not test if
read()
succeeds. You should break from the loop if it fails. - you unconditionally close the socket in the body of the loop: the next
read
will fail and return -1 immediately, you will print a bogus message and iterate at nauseam. - you call
strlen()
andprintf()
with a buffer that might not be null terminated, potentially causing undefined behavior.
Here is a modified version:
JavaScript
void handle_client(int sock) {
char buffer[BUFFSIZE];
int received = -1;
if (sock < 0)
return;
for (;;) {
/* Read from socket */
received = read(sock, buffer, sizeof(buffer) - 1);
if (received <= 0)
break;
buffer[received] = '';
printf("Message from client: %sn", buffer);
/* Write to socket */
write(sock, buffer, received);
}
/* Close socket */
close(sock);
}