hi guys
now I want to change the server so that it is able to serve more customers
simultaneously. as there is a process "generic "
initially creates the server and then creates the customer (at random intervals). Each
client requests the server to process a string, and has been answered,
ends. The creation of the customers that the process continues until the generator
it receives a SIGINT signal (eg, keyboard). At which point the
generator stops generating customers, when all customers have been completed,
sends a SIGUSR1 signal to the server that runs it close. The
communication between clients and servers must be made through a single socket.
....
I wrote up here guys ... but I do not know as I continue ..
please help me
code server :
thank you for help!!Code:/* upperserver.c : un server per maiuscolare linee di testo */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <ctype.h> #define SERVER_PORT 1313 #define LINESIZE 80 void upperlines (int in, int out) { char inputline[LINESIZE]; int len, i; while ((len = recv (in, inputline, LINESIZE, 0)) > 0) { sleep(10); for (i = 0; i < len; i++) inputline[i] = toupper (inputline[i]); send (out, inputline, len, 0); } } int main (unsigned argc, char **argv) { int sock, client_len, fd; struct sockaddr_in server, client; /* impostazione del transport end point */ if ((sock = socket (AF_INET, SOCK_STREAM, 0)) == -1) { perror ("chiamata alla system call socket fallita"); exit (1); } server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons (SERVER_PORT); /* binding dell'indirizzo al transport end point */ if (bind (sock, (struct sockaddr *) &server, sizeof server) == -1) { perror ("chiamata alla system call bind fallita"); exit (2); } listen (sock, 1); /* gestione delle connessioni dei client */ while (1) { client_len = sizeof (client); if ((fd = accept (sock, (struct sockaddr *) &client, &client_len)) < 0) { perror ("accepting connection"); exit (3); } fprintf (stderr, "Aperta connessione.\n"); send (fd, "Benvenuto all'UpperServer!\n", 27, 0); upperlines (fd, fd); close (fd); fprintf (stderr, "Chiusa connessione.\n"); } } ******************************************************************************** code client : #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define MAXLENGTH 80 #define SERVER_PORT 1313 main () { int sockfd; struct sockaddr_in server={AF_INET,htons(SERVER_PORT),INADDR_ANY}; int i = 0, len; char buf[MAXLENGTH], c; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("chiamata alla system call socket fallita"); exit(1); } /* connessione al server */ if (connect(sockfd, (struct sockaddr *) &server, sizeof server) == -1) { perror("connessione al server fallita"); exit(2); } /* ricezione e stampa a video del messaggio di benvenuto del server */ if (recv(sockfd, buf, 27, 0) > 0) { printf("%s", buf); } else { perror("Connessione al server interrotta"); exit(3); } /* acquisizione della stringa da standard input */ printf("\nDigita una stringa:"); while ((c = getchar()) != '\n' && i < MAXLENGTH) buf[i++] = c; buf[i] = '\0'; len = strlen(buf); /* invio e ricezione della stringa */ if (send(sockfd, buf, len, 0) == -1) { perror("Errore nell'invio della stringa al server"); exit(4); } if (recv(sockfd, buf, len, 0) > 0) { printf("%s\n", buf); } else { perror("Connessione al server interrotta"); exit(3); } /* chiusura della connessione */ close(sockfd); }



LinkBack URL
About LinkBacks


