i’ve got a problem with client/server address book.
When i connect the first client all is okay. After, the server accept other connections from other clients but it doesn’t respond to any request (adding numbers etc…) what can i do? thank you
int optval;
socklen_t optlen = sizeof(optval);
char choice[MAX];
char buff[MAX]; /* dati di invio e ricezione */
char buff1[MAX];
char buffNome[MAX];
char buffCognome[MAX];
char buffTelefono[MAX];
char buffMail[MAX];
struct sockaddr_in server_addr; /* indirizzo del server */
struct sockaddr_in client_addr; /* indirizzo del client */
int sd_server, sd_client; /* i socket descriptor usati per identificare server e client */
if((sd_server = socket(AF_INET, SOCK_STREAM, 0)) < 0)
printf("Errore nella creazione del server\n");
optval = 1;
optlen = sizeof(optval);
if(setsockopt(sd_server, SOL_SOCKET, SO_REUSEADDR, &optval, optlen) < 0) {
perror("setsockopt()");
close(sd_server);
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET; /* la famiglia dei protocolli */
server_addr.sin_port = htons(1745); /* la porta in ascolto */
server_addr.sin_addr.s_addr = INADDR_ANY; /* dato che è un server bisogna associargli l'indirizzo della macchina su cui sta girando */
if(bind(sd_server, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
printf("Errore di binding\n");
listen (sd_server, 20);
int address_size = sizeof(client_addr);
struct elemento *lista = NULL;
struct elementoutente *listautente = NULL;
while (1) {
if((sd_client = accept(sd_server, (struct sockaddr *)&client_addr, &address_size)) < 0)
printf("Errore nella chiamata accept\n");
while(1){
.... CODE.....
}
close(sd_server);
You can get away with no thread/forking if you only process one client request and then return to accept loop. In other words, get rid of the inner while(1) loop. Make sure that your clients open socket, make one request, then close socket (that’s normal operation for HTTP 1.0).
This is quite inefficient, but it will get you going.